forked from jmug/stoatchat
feat(january): fetch embeds for multiple links
This commit is contained in:
@@ -124,34 +124,56 @@ impl Embed {
|
|||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
|
||||||
// ! FIXME: allow multiple links
|
|
||||||
let mut finder = LinkFinder::new();
|
let mut finder = LinkFinder::new();
|
||||||
finder.kinds(&[LinkKind::Url]);
|
finder.kinds(&[LinkKind::Url]);
|
||||||
let links: Vec<_> = finder.links(&content).collect();
|
let links: Vec<_> = finder.links(&content).take(5).collect();
|
||||||
|
|
||||||
if links.len() == 0 {
|
if links.len() == 0 {
|
||||||
return Err(Error::LabelMe);
|
return Err(Error::LabelMe);
|
||||||
}
|
}
|
||||||
|
|
||||||
let link = &links[0];
|
let mut embeds: Vec<Embed> = Vec::new();
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
// ! FIXME: allow configuration of number of embeds
|
||||||
let result = client
|
// ! FIXME: batch request to january?
|
||||||
.get(&format!("{}/embed", *JANUARY_URL))
|
let mut link_index = 0;
|
||||||
.query(&[("url", link.as_str())])
|
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match result {
|
while link_index < links.len() {
|
||||||
Err(_) => return Err(Error::LabelMe),
|
let link = &links[link_index];
|
||||||
Ok(result) => match result.status() {
|
|
||||||
reqwest::StatusCode::OK => {
|
|
||||||
let res: Embed = result.json().await.map_err(|_| Error::InvalidOperation)?;
|
|
||||||
|
|
||||||
Ok(vec![res])
|
// Check if we did the same link already in for this message.
|
||||||
}
|
if link_index != 0 && links.iter().take(link_index).any(|x| x.as_str() == link.as_str()) {
|
||||||
_ => return Err(Error::LabelMe),
|
link_index = link_index + 1;
|
||||||
},
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let result = client
|
||||||
|
.get(&format!("{}/embed", *JANUARY_URL))
|
||||||
|
.query(&[("url", link.as_str())])
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
link_index = link_index + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let response = result.unwrap();
|
||||||
|
if response.status().is_success() {
|
||||||
|
let res: Embed = response.json().await.map_err(|_| Error::InvalidOperation)?;
|
||||||
|
|
||||||
|
embeds.push(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
link_index = link_index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent database update when no embeds are found
|
||||||
|
if embeds.len() > 0 {
|
||||||
|
Ok(embeds)
|
||||||
|
} else {
|
||||||
|
Err(Error::LabelMe)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user