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 regex::Regex;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use tokio::task::JoinSet;
use std::time::Instant; use std::time::Instant;
@ -121,28 +120,15 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
return; 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); let urls = get_urls_from_message(&text_content.body);
for url in urls { 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();
requests.spawn(reqwest_client.get(url).send());
}
while let Some(req) = requests.join_next().await { for url in urls {
match req.unwrap() { match reqwest_client.get(url).send().await {
Err(e) => warn!( Err(e) => warn!("Failed to fetch metadata for URL '{}' with error: '{:?}'", &url, e),
"Failed to fetch metadata for eventID '{}' and error: '{:?}'",
full_reply_event.event_id, e
),
Ok(req) => { Ok(req) => {
let url = req.url().clone();
match req.text().await { match req.text().await {
Err(e) => warn!( Err(e) => warn!("Failed to parse HTML for URL '{}' with error: '{:?}'", &url, e),
"Failed to parse HTML for URL '{}' in eventID '{}' with error: '{:?}'",
url, full_reply_event.event_id, e
),
Ok(resp) => { Ok(resp) => {
// beware, dirty HTML parsing code // beware, dirty HTML parsing code
let metadata = parse_metadata(&resp); 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 // Finally send the reply to the room
warn!( warn!("Sending embed for URL: '{}'", &url);
"Sending embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
if room.send(bot_reply).await.is_err() { if room.send(bot_reply).await.is_err() {
warn!( warn!("Failed to send embed for URL: '{}'", &url);
"Failed to send embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
} }
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed()); warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
// If we didn't get any metadata send a generic "No metadata" response // 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); .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
// Send the reply to the room // Send the reply to the room
warn!( warn!("Sending 'No metadata' embed for URL: '{}'", &url);
"Sending 'No metadata' embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
if room.send(bot_reply).await.is_err() { if room.send(bot_reply).await.is_err() {
warn!( warn!("Failed to send embed for URL: '{}'", &url);
"Failed to send embed for eventID '{}' with URL: '{}'",
full_reply_event.event_id, url
);
} }
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed()); warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
} }