From 85ecbb03e299e99dbb2bc7864136a40add71df72 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 7 Oct 2021 21:57:52 +0200 Subject: Update dependencies The biggest adaption was the update to matrix-sdk 0.4, as some types got shuffled around and the API is now imported from ruma. The relates_to.is_none() check seems to no longer work, as Relation::_Custom is used instead, so instead we now explicitely check if the message is neither a reply nor a replacement (on a related note, it is unknown to me whether we need to find the first original message or if we could also set a replaces on the last message in the chain). serenity, tokio and reqwest needed no API updates on the other hand. --- src/config.rs | 2 +- src/discord.rs | 2 +- src/matrix.rs | 52 +++++++++++++++++++++++++++------------------------- 3 files changed, 29 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index ade4d2b..a4bba44 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,7 +39,7 @@ pub struct Discord { #[derive(Debug, Clone, Deserialize)] pub struct Matrix { /// Matrix homeserver. - pub homeserver: String, + pub homeserver: reqwest::Url, /// Matrix username. pub username: String, /// Matrix password. diff --git a/src/discord.rs b/src/discord.rs index c98a6c7..3ab849c 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -91,7 +91,7 @@ impl EventHandler for Handler { pub fn post_link(discord_token: &str, channel_id: u64, log: &Log, link: &str) -> Result<()> { let link = link.to_owned(); let log = log.clone(); - let mut rt = Runtime::new()?; + let rt = Runtime::new()?; rt.block_on(async { let mut client = Client::builder(discord_token) diff --git a/src/matrix.rs b/src/matrix.rs index 5243db4..ce31fe3 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -10,18 +10,21 @@ use evtclib::Log; use log::{debug, info}; use tokio::runtime::Runtime; -use matrix_sdk::{ - api::r0::message::get_message_events, - events::room::message::{MessageEventContent, Relation, TextMessageEventContent}, - events::room::relationships::Replacement, +use reqwest::Url; + +use ruma::{ + api::client::r0::message::get_message_events, + events::room::message::{MessageEventContent, MessageType, Relation, Replacement}, events::{AnyMessageEvent, AnyMessageEventContent, AnyRoomEvent}, identifiers::{EventId, RoomId, UserId}, - Client, UInt, + MilliSecondsSinceUnixEpoch, UInt, }; +use matrix_sdk::Client; + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MatrixUser { - pub homeserver: String, + pub homeserver: Url, pub username: String, pub password: String, pub device_id: Option, @@ -52,11 +55,11 @@ const MESSAGE_CHUNK_COUNT: u16 = 3; /// This function blocks until all API calls have been made, that is until the message has reached /// the homeserver. pub fn post_link(user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Result<()> { - let mut rt = Runtime::new()?; + let rt = Runtime::new()?; let room_id = RoomId::try_from(room_id)?; rt.block_on(async { - let client = Client::new(&user.homeserver as &str)?; + let client = Client::new(user.homeserver)?; let my_data = client .login( &user.username, @@ -96,7 +99,10 @@ async fn find_message( room_id: &RoomId, ) -> Result> { let limit = UInt::try_from(MESSAGE_CHUNK_SIZE).unwrap(); - let time_limit = SystemTime::now() - Duration::from_secs(MAX_HOURS * 60 * 60); + let time_limit = MilliSecondsSinceUnixEpoch::from_system_time( + SystemTime::now() - Duration::from_secs(MAX_HOURS * 60 * 60), + ) + .expect("Our time limit is before the epoch"); let mut continue_from = String::new(); for chunk_nr in 0..MESSAGE_CHUNK_COUNT { @@ -109,14 +115,17 @@ async fn find_message( let mut request = get_message_events::Request::backward(room_id, &continue_from); request.limit = limit; - let response = client.room_messages(request).await?; + let response = client.send(request, None).await?; for raw_message in response.chunk { if let Ok(AnyRoomEvent::Message(AnyMessageEvent::RoomMessage(msg))) = raw_message.deserialize() { if &msg.sender == my_id && msg.origin_server_ts >= time_limit { - if let MessageEventContent::Text(text) = msg.content { - if text.relates_to.is_none() { + if let MessageType::Text(text) = msg.content.msgtype { + if !matches!( + msg.content.relates_to, + Some(Relation::Reply { .. } | Relation::Replacement(..)) + ) { return Ok(Some((msg.event_id, text.body))); } } @@ -139,11 +148,10 @@ async fn post_new(client: &Client, room_id: &RoomId, log: &Log, link: &str) -> R let body = logbag.render_plain(); let html = logbag.render_html(); - let text_message = TextMessageEventContent::html(body, html); client .room_send( room_id, - AnyMessageEventContent::RoomMessage(MessageEventContent::Text(text_message)), + AnyMessageEventContent::RoomMessage(MessageEventContent::text_html(body, html)), None, ) .await?; @@ -160,19 +168,13 @@ async fn update_message( new_text: &str, new_html: &str, ) -> Result<()> { - let mut message = TextMessageEventContent::html(new_text, new_html); - message.new_content = Some(Box::new(MessageEventContent::Text( - TextMessageEventContent::html(new_text, new_html), + let mut message = MessageEventContent::text_html(new_text, new_html); + message.relates_to = Some(Relation::Replacement(Replacement::new( + old_id.clone(), + Box::new(MessageEventContent::text_html(new_text, new_html)), ))); - message.relates_to = Some(Relation::Replacement(Replacement { - event_id: old_id.clone(), - })); client - .room_send( - room_id, - AnyMessageEventContent::RoomMessage(MessageEventContent::Text(message)), - None, - ) + .room_send(room_id, AnyMessageEventContent::RoomMessage(message), None) .await?; Ok(()) } -- cgit v1.2.3