Compare commits

..

No commits in common. "76c99deb210c448302801c9247fd83a10f920153" and "fce7dbf9bee1b0de531389c02f4be12da702c33e" have entirely different histories.

View file

@ -14,7 +14,6 @@ use matrix_sdk::{
};
use regex::Regex;
use scraper::{Html, Selector};
use tokio::task::JoinSet;
use std::time::Instant;
@ -121,28 +120,15 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
return;
};
let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap();
// Create and populate the JoinSet with async requests
// This allows us to await all the requests at once later
let mut requests = JoinSet::new();
let urls = get_urls_from_message(&text_content.body);
for url in urls {
requests.spawn(reqwest_client.get(url).send());
}
let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap();
while let Some(req) = requests.join_next().await {
match req.unwrap() {
Err(e) => warn!(
"Failed to fetch metadata for eventID '{}' and error: '{:?}'",
full_reply_event.event_id, e
),
for url in urls {
match reqwest_client.get(url).send().await {
Err(e) => warn!("Failed to fetch metadata for URL '{}' with error: '{:?}'", &url, e),
Ok(req) => {
let url = req.url().clone();
match req.text().await {
Err(e) => warn!(
"Failed to parse HTML for URL '{}' in eventID '{}' with error: '{:?}'",
url, full_reply_event.event_id, e
),
Err(e) => warn!("Failed to parse HTML for URL '{}' with error: '{:?}'", &url, e),
Ok(resp) => {
// beware, dirty HTML parsing code
let metadata = parse_metadata(&resp);
@ -168,15 +154,9 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
);
// Finally send the reply to the room
warn!(
"Sending embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
warn!("Sending embed for URL: '{}'", &url);
if room.send(bot_reply).await.is_err() {
warn!(
"Failed to send embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
warn!("Failed to send embed for URL: '{}'", &url);
}
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
// If we didn't get any metadata send a generic "No metadata" response
@ -187,15 +167,9 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
)
.make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
// Send the reply to the room
warn!(
"Sending 'No metadata' embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
warn!("Sending 'No metadata' embed for URL: '{}'", &url);
if room.send(bot_reply).await.is_err() {
warn!(
"Failed to send embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
warn!("Failed to send embed for URL: '{}'", &url);
}
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
}