Compare commits

...

4 commits

Author SHA1 Message Date
fce7dbf9be
Merge branch 'refactor' into 'master' 2024-07-10 21:38:14 +10:00
2dad7aa33a
Fix typo 2024-07-10 03:13:05 +10:00
be002a4e4b
Apply rustfmt 2024-07-10 03:07:20 +10:00
f335c0cbcf
Refactor embed module functions and fix lint error 2024-07-10 02:59:48 +10:00
2 changed files with 58 additions and 52 deletions

View file

@ -5,12 +5,12 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::warn; use log::warn;
use matrix_sdk::{ use matrix_sdk::{
RoomState,
room::Room, room::Room,
ruma::events::room::message::{ ruma::events::room::message::{
MessageType, OriginalSyncRoomMessageEvent, Relation, ForwardThread, AddMentions, RoomMessageEventContent, AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent, Relation,
RoomMessageEventContent,
}, },
Client, Client, RoomState,
}; };
use regex::Regex; use regex::Regex;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
@ -124,53 +124,59 @@ 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(); 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 { for url in urls {
if let Ok(req) = reqwest_client.get(url).send().await { match reqwest_client.get(url).send().await {
if let Ok(res) = req.text().await { Err(e) => warn!("Failed to fetch metadata for URL '{}' with error: '{:?}'", &url, e),
// beware, dirty HTML parsing code Ok(req) => {
let metadata = parse_metadata(&res); match req.text().await {
warn!("Ran fn parse_metadata after: '{:#?}'", fn_start.elapsed()); 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 // Build and send our message reply
if metadata.is_some() { if metadata.is_some() {
let embed = metadata.unwrap(); let embed = metadata.unwrap();
let bot_reply = RoomMessageEventContent::text_html( let bot_reply = RoomMessageEventContent::text_html(
&embed.title, &embed.title,
format!( format!(
"<blockquote> "<blockquote>
<h4>{}</h4> <h4>{}</h4>
<p>{}</p> <p>{}</p>
</blockquote>", </blockquote>",
&embed.title, &embed.description &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 // Finally send the reply to the room
warn!("Sending embed for URL: '{}'", &url); warn!("Sending embed for URL: '{}'", &url);
if room.send(bot_reply).await.is_err() { if room.send(bot_reply).await.is_err() {
warn!("Failed to send embed for URL: '{}'", &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
} else {
let bot_reply = RoomMessageEventContent::text_html(
"Couldn't parse metadata for URL",
"<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
)
.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",
"<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
)
.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);
} }
} }
}; }
} }

View file

@ -5,13 +5,12 @@ pub mod embeds;
use log::warn; use log::warn;
use matrix_sdk::{ use matrix_sdk::{
config::SyncSettings, config::SyncSettings,
RoomState,
room::Room, room::Room,
ruma::{ ruma::{
api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId, api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId,
OwnedRoomId, OwnedRoomId,
}, },
Client, ClientBuildError, Client, ClientBuildError, RoomState,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -76,7 +75,7 @@ pub async fn delete_old_encryption_devices(client: &Client, config: &Config) ->
uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()), uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()),
config.password.clone(), config.password.clone(),
); );
password.session = info.session.clone(); password.session.clone_from(&info.session);
client client
.delete_devices(&old_devices, Some(uiaa::AuthData::Password(password))) .delete_devices(&old_devices, Some(uiaa::AuthData::Password(password)))
.await?; .await?;
@ -95,13 +94,14 @@ pub async fn reject_stale_invites(client: &Client, config: &Config) {
for room in client.invited_rooms() { for room in client.invited_rooms() {
let room_name = room.name().unwrap_or_default(); let room_name = room.name().unwrap_or_default();
if !room.is_space() 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()) && config.room_ids.iter().any(|r| *r == room.room_id())
{ {
warn!("Got invite to room: '{}'", room_name); warn!("Got invite to room: '{}'", room_name);
room.join() room.join().await.expect("Failed to accept invite");
.await
.expect("Failed to accept invite");
warn!("Joined room: '{}'!", room_name); warn!("Joined room: '{}'!", room_name);
} else { } else {
warn!("Rejecting invite to room: '{}'", room_name); warn!("Rejecting invite to room: '{}'", room_name);