From f335c0cbcf0110ac2c9c3403e47b82284bbdfd0b Mon Sep 17 00:00:00 2001 From: froge Date: Wed, 10 Jul 2024 02:59:48 +1000 Subject: [PATCH 1/3] Refactor embed module functions and fix lint error --- src/embeds.rs | 86 ++++++++++++++++++++++++++------------------------- src/lib.rs | 2 +- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/embeds.rs b/src/embeds.rs index 5c46f88..67ddefb 100644 --- a/src/embeds.rs +++ b/src/embeds.rs @@ -124,53 +124,55 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie 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(); for url in urls { - if let Ok(req) = reqwest_client.get(url).send().await { - if let Ok(res) = req.text().await { - // beware, dirty HTML parsing code - let metadata = parse_metadata(&res); - warn!("Ran fn parse_metadata after: '{:#?}'", fn_start.elapsed()); + match reqwest_client.get(url).send().await { + Err(e) => warn!("Failed to fetch metadata for '{}' with error: '{:?}'", &url, e), + Ok(req) => { + match req.text().await { + 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); + warn!("Ran fn parse_metadata after: '{:#?}'", fn_start.elapsed()); - // Build and send our message reply - if metadata.is_some() { - let embed = metadata.unwrap(); - let bot_reply = RoomMessageEventContent::text_html( - &embed.title, - format!( - "
-

{}

-

{}

-
", - &embed.title, &embed.description - ), - ) - .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); + // Build and send our message reply + if metadata.is_some() { + let embed = metadata.unwrap(); + let bot_reply = RoomMessageEventContent::text_html( + &embed.title, + format!( + "
+

{}

+

{}

+
", + &embed.title, &embed.description + ), + ) + .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); - // Finally send the reply to the room - warn!("Sending embed for URL: '{}'", &url); - if room.send(bot_reply).await.is_err() { - warn!("Failed to send embed for URL: '{}'", &url); + // Finally send the reply to the room + warn!("Sending embed for URL: '{}'", &url); + if room.send(bot_reply).await.is_err() { + 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 + } else { + let bot_reply = RoomMessageEventContent::text_html( + "Couldn't parse metadata for URL", + "
Couldn't parse metadata for URL
", + ) + .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); + // Send the reply to the room + warn!("Sending 'No metadata' embed for URL: '{}'", &url); + if room.send(bot_reply).await.is_err() { + warn!("Failed to send embed for URL: '{}'", &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 - } else { - let bot_reply = RoomMessageEventContent::text_html( - "Couldn't parse metadata for URL", - "
Couldn't parse metadata for URL
", - ) - .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); - // Send the reply to the room - warn!("Sending 'No metadata' embed for URL: '{}'", &url); - if room.send(bot_reply).await.is_err() { - warn!("Failed to send embed for URL: '{}'", &url); - } - warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed()); } - } else { - warn!("Failed to parse HTML for URL: '{}'", &url); } - } else { - warn!("Failed to fetch metadata for '{}'", &url); } } - }; + } } diff --git a/src/lib.rs b/src/lib.rs index cf879f1..f288339 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,7 @@ pub async fn delete_old_encryption_devices(client: &Client, config: &Config) -> uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()), config.password.clone(), ); - password.session = info.session.clone(); + password.session.clone_from(&info.session); client .delete_devices(&old_devices, Some(uiaa::AuthData::Password(password))) .await?; From be002a4e4bfa751fedd5896b90db71a502111087 Mon Sep 17 00:00:00 2001 From: froge Date: Wed, 10 Jul 2024 03:07:20 +1000 Subject: [PATCH 2/3] Apply rustfmt --- src/embeds.rs | 14 +++++++++----- src/lib.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/embeds.rs b/src/embeds.rs index 67ddefb..6abece7 100644 --- a/src/embeds.rs +++ b/src/embeds.rs @@ -5,12 +5,12 @@ use lazy_static::lazy_static; use log::warn; use matrix_sdk::{ - RoomState, room::Room, ruma::events::room::message::{ - MessageType, OriginalSyncRoomMessageEvent, Relation, ForwardThread, AddMentions, RoomMessageEventContent, + AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent, Relation, + RoomMessageEventContent, }, - Client, + Client, RoomState, }; use regex::Regex; use scraper::{Html, Selector}; @@ -125,7 +125,7 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie for url in urls { match reqwest_client.get(url).send().await { - Err(e) => warn!("Failed to fetch metadata for '{}' with error: '{:?}'", &url, e), + Err(e) => warn!("Failed to fetch metadata for URL'{}' with error: '{:?}'", &url, e), Ok(req) => { match req.text().await { Err(e) => warn!("Failed to parse HTML for URL '{}' with error: '{:?}'", &url, e), @@ -147,7 +147,11 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie &embed.title, &embed.description ), ) - .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); + .make_reply_to( + &full_reply_event, + ForwardThread::Yes, + AddMentions::Yes, + ); // Finally send the reply to the room warn!("Sending embed for URL: '{}'", &url); diff --git a/src/lib.rs b/src/lib.rs index f288339..048a1ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,13 +5,12 @@ pub mod embeds; use log::warn; use matrix_sdk::{ config::SyncSettings, - RoomState, room::Room, ruma::{ api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId, OwnedRoomId, }, - Client, ClientBuildError, + Client, ClientBuildError, RoomState, }; use serde::{Deserialize, Serialize}; @@ -95,13 +94,14 @@ pub async fn reject_stale_invites(client: &Client, config: &Config) { for room in client.invited_rooms() { let room_name = room.name().unwrap_or_default(); if !room.is_space() - && !room.is_direct().await.expect("Failed to check if room is DM") + && !room + .is_direct() + .await + .expect("Failed to check if room is DM") && config.room_ids.iter().any(|r| *r == room.room_id()) { warn!("Got invite to room: '{}'", room_name); - room.join() - .await - .expect("Failed to accept invite"); + room.join().await.expect("Failed to accept invite"); warn!("Joined room: '{}'!", room_name); } else { warn!("Rejecting invite to room: '{}'", room_name); From 2dad7aa33a255d3be2b45867056d50c80111ff35 Mon Sep 17 00:00:00 2001 From: froge Date: Wed, 10 Jul 2024 03:13:05 +1000 Subject: [PATCH 3/3] Fix typo --- src/embeds.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/embeds.rs b/src/embeds.rs index 6abece7..7ae7cac 100644 --- a/src/embeds.rs +++ b/src/embeds.rs @@ -125,7 +125,7 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie for url in urls { match reqwest_client.get(url).send().await { - Err(e) => warn!("Failed to fetch metadata for URL'{}' with error: '{:?}'", &url, e), + Err(e) => warn!("Failed to fetch metadata for URL '{}' with error: '{:?}'", &url, e), Ok(req) => { match req.text().await { Err(e) => warn!("Failed to parse HTML for URL '{}' with error: '{:?}'", &url, e),