From 7c6a5927e4fd9b7565f2c49dddd1f03c6e2e1583 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 14 Jan 2025 15:06:45 +0100 Subject: [PATCH] chore: Update to latest Ruma version. --- Cargo.lock | 16 +-- Cargo.toml | 4 +- .../src/machine/tests/mod.rs | 4 +- .../timeline/event_item/content/message.rs | 10 -- .../src/timeline/event_item/mod.rs | 1 - crates/matrix-sdk-ui/src/timeline/mod.rs | 49 +++---- crates/matrix-sdk/src/room/edit.rs | 131 +++--------------- 7 files changed, 51 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac2f7d71988..89e373c02e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4787,7 +4787,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.12.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "assign", "js_int", @@ -4803,7 +4803,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.20.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "as_variant", "assign", @@ -4826,7 +4826,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.15.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "as_variant", "base64 0.22.1", @@ -4858,7 +4858,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.30.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "as_variant", "indexmap 2.6.0", @@ -4883,7 +4883,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.11.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "http", "js_int", @@ -4897,7 +4897,7 @@ dependencies = [ [[package]] name = "ruma-html" version = "0.4.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "as_variant", "html5ever", @@ -4909,7 +4909,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.10.1" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "js_int", "thiserror 2.0.3", @@ -4918,7 +4918,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.15.0" -source = "git+https://github.com/ruma/ruma?rev=b266343136e8470a7d040efc207e16af0c20d374#b266343136e8470a7d040efc207e16af0c20d374" +source = "git+https://github.com/ruma/ruma?rev=5c9ef9f425d8f8b330e46d215c683bf799a28d50#5c9ef9f425d8f8b330e46d215c683bf799a28d50" dependencies = [ "cfg-if", "proc-macro-crate", diff --git a/Cargo.toml b/Cargo.toml index 78ec2e40895..16a6226515a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,7 @@ proptest = { version = "1.5.0", default-features = false, features = ["std"] } rand = "0.8.5" reqwest = { version = "0.12.4", default-features = false } rmp-serde = "1.3.0" -ruma = { git = "https://github.com/ruma/ruma", rev = "b266343136e8470a7d040efc207e16af0c20d374", features = [ +ruma = { git = "https://github.com/ruma/ruma", rev = "5c9ef9f425d8f8b330e46d215c683bf799a28d50", features = [ "client-api-c", "compat-upload-signatures", "compat-user-id", @@ -72,7 +72,7 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "b266343136e8470a7d040efc20 "unstable-msc4140", "unstable-msc4171", ] } -ruma-common = { git = "https://github.com/ruma/ruma", rev = "b266343136e8470a7d040efc207e16af0c20d374" } +ruma-common = { git = "https://github.com/ruma/ruma", rev = "5c9ef9f425d8f8b330e46d215c683bf799a28d50" } serde = "1.0.151" serde_html_form = "0.2.0" serde_json = "1.0.91" diff --git a/crates/matrix-sdk-crypto/src/machine/tests/mod.rs b/crates/matrix-sdk-crypto/src/machine/tests/mod.rs index 09ea5107254..027a7b68499 100644 --- a/crates/matrix-sdk-crypto/src/machine/tests/mod.rs +++ b/crates/matrix-sdk-crypto/src/machine/tests/mod.rs @@ -1323,8 +1323,8 @@ async fn test_unsigned_decryption() { // Encrypt a second message, an edit. let second_message_text = "This is the ~~original~~ edited message"; - let second_message_content = RoomMessageEventContent::text_plain(second_message_text) - .make_replacement(first_message, None); + let second_message_content = + RoomMessageEventContent::text_plain(second_message_text).make_replacement(first_message); let second_message_encrypted_content = alice.encrypt_room_event(room_id, second_message_content).await.unwrap(); diff --git a/crates/matrix-sdk-ui/src/timeline/event_item/content/message.rs b/crates/matrix-sdk-ui/src/timeline/event_item/content/message.rs index 09ee071f923..8f90151b193 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_item/content/message.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_item/content/message.rs @@ -145,16 +145,6 @@ impl Message { self.mentions.as_ref() } - pub(in crate::timeline) fn to_content(&self) -> RoomMessageEventContent { - // Like the `impl From for RoomMessageEventContent` below, but - // takes &self and only copies what's needed. - let relates_to = make_relates_to( - self.thread_root.clone(), - self.in_reply_to.as_ref().map(|details| details.event_id.clone()), - ); - assign!(RoomMessageEventContent::new(self.msgtype.clone()), { relates_to }) - } - pub(in crate::timeline) fn with_in_reply_to(&self, in_reply_to: InReplyToDetails) -> Self { Self { in_reply_to: Some(in_reply_to), ..self.clone() } } diff --git a/crates/matrix-sdk-ui/src/timeline/event_item/mod.rs b/crates/matrix-sdk-ui/src/timeline/event_item/mod.rs index 47918eba1e1..74bec6e338c 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_item/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_item/mod.rs @@ -566,7 +566,6 @@ impl EventTimelineItem { Ok(RepliedToInfo { event_id: event_id.to_owned(), sender: self.sender().to_owned(), - timestamp: self.timestamp(), content: reply_content, }) } diff --git a/crates/matrix-sdk-ui/src/timeline/mod.rs b/crates/matrix-sdk-ui/src/timeline/mod.rs index 7219925278a..d115f693b23 100644 --- a/crates/matrix-sdk-ui/src/timeline/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/mod.rs @@ -39,10 +39,10 @@ use ruma::{ events::{ poll::unstable_start::{NewUnstablePollStartEventContent, UnstablePollStartEventContent}, receipt::{Receipt, ReceiptThread}, + relation::Thread, room::{ message::{ - AddMentions, ForwardThread, OriginalRoomMessageEvent, - RoomMessageEventContentWithoutRelation, + AddMentions, ForwardThread, ReplyMetadata, RoomMessageEventContentWithoutRelation, }, pinned_events::RoomPinnedEventsEventContent, }, @@ -50,7 +50,7 @@ use ruma::{ SyncMessageLikeEvent, }, serde::Raw, - EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomVersionId, UserId, + EventId, OwnedEventId, OwnedUserId, RoomVersionId, UserId, }; use thiserror::Error; use tracing::{error, instrument, trace, warn}; @@ -104,8 +104,6 @@ pub struct RepliedToInfo { event_id: OwnedEventId, /// The sender of the event to reply to. sender: OwnedUserId, - /// The timestamp of the event to reply to. - timestamp: MilliSecondsSinceUnixEpoch, /// The content of the event to reply to. content: ReplyContent, } @@ -343,7 +341,8 @@ impl Timeline { replied_to_info: RepliedToInfo, forward_thread: ForwardThread, ) -> Result<(), RoomSendQueueError> { - let event_id = replied_to_info.event_id; + let event_id = &replied_to_info.event_id; + let sender = &replied_to_info.sender; // [The specification](https://spec.matrix.org/v1.10/client-server-api/#user-and-room-mentions) says: // @@ -352,32 +351,21 @@ impl Timeline { // // If the replied to event has been written by the current user, let's toggle to // `AddMentions::No`. - let mention_the_sender = if self.room().own_user_id() == replied_to_info.sender { - AddMentions::No - } else { - AddMentions::Yes + let mention_the_sender = + if self.room().own_user_id() == sender { AddMentions::No } else { AddMentions::Yes }; + + let thread = match replied_to_info.content { + ReplyContent::Message(message) => message + .thread_root() + .map(|thread_root| Thread::plain(thread_root.clone(), event_id.clone())), + ReplyContent::Raw(_raw_event) => None, }; - let content = match replied_to_info.content { - ReplyContent::Message(msg) => { - let event = OriginalRoomMessageEvent { - event_id: event_id.to_owned(), - sender: replied_to_info.sender, - origin_server_ts: replied_to_info.timestamp, - room_id: self.room().room_id().to_owned(), - content: msg.to_content(), - unsigned: Default::default(), - }; - content.make_reply_to(&event, forward_thread, mention_the_sender) - } - ReplyContent::Raw(raw_event) => content.make_reply_to_raw( - &raw_event, - event_id.to_owned(), - self.room().room_id(), - forward_thread, - mention_the_sender, - ), - }; + let content = content.make_reply_to( + ReplyMetadata::new(event_id, sender, thread.as_ref()), + forward_thread, + mention_the_sender, + ); self.send(content.into()).await?; @@ -425,7 +413,6 @@ impl Timeline { Ok(RepliedToInfo { event_id: event_id.to_owned(), sender: sync_event.sender().to_owned(), - timestamp: sync_event.origin_server_ts(), content: reply_content, }) } diff --git a/crates/matrix-sdk/src/room/edit.rs b/crates/matrix-sdk/src/room/edit.rs index 850e70d3884..ae1e80173f4 100644 --- a/crates/matrix-sdk/src/room/edit.rs +++ b/crates/matrix-sdk/src/room/edit.rs @@ -24,17 +24,16 @@ use ruma::{ UnstablePollStartEventContent, }, room::message::{ - FormattedBody, MessageType, Relation, ReplacementMetadata, RoomMessageEventContent, + FormattedBody, MessageType, ReplacementMetadata, RoomMessageEventContent, RoomMessageEventContentWithoutRelation, }, - AnyMessageLikeEvent, AnyMessageLikeEventContent, AnySyncMessageLikeEvent, - AnySyncTimelineEvent, AnyTimelineEvent, Mentions, MessageLikeEvent, - OriginalMessageLikeEvent, SyncMessageLikeEvent, + AnyMessageLikeEventContent, AnySyncMessageLikeEvent, AnySyncTimelineEvent, Mentions, + SyncMessageLikeEvent, }, - EventId, RoomId, UserId, + EventId, UserId, }; use thiserror::Error; -use tracing::{debug, instrument, trace, warn}; +use tracing::{debug, instrument, trace}; use crate::Room; @@ -121,7 +120,7 @@ impl Room { event_id: &EventId, new_content: EditedContent, ) -> Result { - make_edit_event(self, self.room_id(), self.own_user_id(), event_id, new_content).await + make_edit_event(self, self.own_user_id(), event_id, new_content).await } } @@ -157,7 +156,6 @@ impl EventSource for &Room { async fn make_edit_event( source: S, - room_id: &RoomId, own_user_id: &UserId, event_id: &EventId, new_content: EditedContent, @@ -188,14 +186,10 @@ async fn make_edit_event( }); }; - let mentions = original.content.mentions.clone(); - let replied_to_original_room_msg = - extract_replied_to(source, room_id, original.content.relates_to).await; + let mentions = original.content.mentions; - let replacement = new_content.make_replacement( - ReplacementMetadata::new(event_id.to_owned(), mentions), - replied_to_original_room_msg.as_ref(), - ); + let replacement = new_content + .make_replacement(ReplacementMetadata::new(event_id.to_owned(), mentions)); Ok(replacement.into()) } @@ -212,9 +206,6 @@ async fn make_edit_event( }; let original_mentions = original.content.mentions.clone(); - let replied_to_original_room_msg = - extract_replied_to(source, room_id, original.content.relates_to.clone()).await; - let mut prev_content = original.content; if !update_media_caption(&mut prev_content, caption, formatted_caption, mentions) { @@ -224,10 +215,8 @@ async fn make_edit_event( }); } - let replacement = prev_content.make_replacement( - ReplacementMetadata::new(event_id.to_owned(), original_mentions), - replied_to_original_room_msg.as_ref(), - ); + let replacement = prev_content + .make_replacement(ReplacementMetadata::new(event_id.to_owned(), original_mentions)); Ok(replacement.into()) } @@ -315,45 +304,6 @@ pub(crate) fn update_media_caption( } } -/// Try to find the original replied-to event content, in a best-effort manner. -async fn extract_replied_to( - source: S, - room_id: &RoomId, - relates_to: Option>, -) -> Option> { - let replied_to_sync_timeline_event = if let Some(Relation::Reply { in_reply_to }) = relates_to { - source - .get_event(&in_reply_to.event_id) - .await - .map_err(|err| { - warn!("couldn't fetch the replied-to event, when editing: {err}"); - err - }) - .ok() - } else { - None - }; - - replied_to_sync_timeline_event - .and_then(|sync_timeline_event| { - sync_timeline_event - .raw() - .deserialize() - .map_err(|err| warn!("unable to deserialize replied-to event: {err}")) - .ok() - }) - .and_then(|event| { - if let AnyTimelineEvent::MessageLike(AnyMessageLikeEvent::RoomMessage( - MessageLikeEvent::Original(original), - )) = event.into_full_event(room_id.to_owned()) - { - Some(original) - } else { - None - } - }) -} - #[cfg(test)] mod tests { use std::collections::BTreeMap; @@ -367,7 +317,7 @@ mod tests { room::message::{MessageType, Relation, RoomMessageEventContentWithoutRelation}, AnyMessageLikeEventContent, AnySyncTimelineEvent, Mentions, }, - owned_mxc_uri, owned_user_id, room_id, + owned_mxc_uri, owned_user_id, serde::Raw, user_id, EventId, OwnedEventId, }; @@ -415,18 +365,11 @@ mod tests { ), ); - let room_id = room_id!("!galette:saucisse.bzh"); let new_content = RoomMessageEventContentWithoutRelation::text_plain("the edit"); assert_matches!( - make_edit_event( - cache, - room_id, - own_user_id, - event_id, - EditedContent::RoomMessage(new_content), - ) - .await, + make_edit_event(cache, own_user_id, event_id, EditedContent::RoomMessage(new_content),) + .await, Err(EditError::StateEvent) ); } @@ -443,19 +386,12 @@ mod tests { f.text_msg("hi").event_id(event_id).sender(user_id!("@other:saucisse.bzh")).into(), ); - let room_id = room_id!("!galette:saucisse.bzh"); let own_user_id = user_id!("@me:saucisse.bzh"); let new_content = RoomMessageEventContentWithoutRelation::text_plain("the edit"); assert_matches!( - make_edit_event( - cache, - room_id, - own_user_id, - event_id, - EditedContent::RoomMessage(new_content), - ) - .await, + make_edit_event(cache, own_user_id, event_id, EditedContent::RoomMessage(new_content),) + .await, Err(EditError::NotAuthor) ); } @@ -472,18 +408,12 @@ mod tests { f.text_msg("hi").event_id(event_id).sender(own_user_id).into(), ); - let room_id = room_id!("!galette:saucisse.bzh"); let new_content = RoomMessageEventContentWithoutRelation::text_plain("the edit"); - let edit_event = make_edit_event( - cache, - room_id, - own_user_id, - event_id, - EditedContent::RoomMessage(new_content), - ) - .await - .unwrap(); + let edit_event = + make_edit_event(cache, own_user_id, event_id, EditedContent::RoomMessage(new_content)) + .await + .unwrap(); assert_let!(AnyMessageLikeEventContent::RoomMessage(msg) = &edit_event); // This is the fallback text, for clients not supporting edits. @@ -506,11 +436,8 @@ mod tests { f.text_msg("hello world").event_id(event_id).sender(own_user_id).into(), ); - let room_id = room_id!("!galette:saucisse.bzh"); - let err = make_edit_event( cache, - room_id, own_user_id, event_id, EditedContent::MediaCaption { @@ -544,11 +471,8 @@ mod tests { .into(), ); - let room_id = room_id!("!galette:saucisse.bzh"); - let edit_event = make_edit_event( cache, - room_id, own_user_id, event_id, EditedContent::MediaCaption { @@ -606,11 +530,8 @@ mod tests { cache.events.insert(event_id.to_owned(), event); - let room_id = room_id!("!galette:saucisse.bzh"); - let edit_event = make_edit_event( cache, - room_id, own_user_id, event_id, // Remove the caption by setting it to None. @@ -662,15 +583,12 @@ mod tests { cache.events.insert(event_id.to_owned(), event); - let room_id = room_id!("!galette:saucisse.bzh"); - // Add an intentional mention in the caption. let mentioned_user_id = owned_user_id!("@crepe:saucisse.bzh"); let edit_event = { let mentions = Mentions::with_user_ids([mentioned_user_id.clone()]); make_edit_event( cache, - room_id, own_user_id, event_id, EditedContent::MediaCaption { @@ -731,12 +649,10 @@ mod tests { .into(), ); - let room_id = room_id!("!galette:saucisse.bzh"); let new_content = RoomMessageEventContentWithoutRelation::text_plain("uh i mean hi too"); let edit_event = make_edit_event( cache, - room_id, own_user_id, resp_event_id, EditedContent::RoomMessage(new_content), @@ -746,12 +662,7 @@ mod tests { assert_let!(AnyMessageLikeEventContent::RoomMessage(msg) = &edit_event); // This is the fallback text, for clients not supporting edits. - assert_eq!( - msg.body(), - r#"> <@steb:saucisse.bzh> hi - -* uh i mean hi too"# - ); + assert_eq!(msg.body(), r#"* uh i mean hi too"#); assert_let!(Some(Relation::Replacement(repl)) = &msg.relates_to); assert_eq!(repl.event_id, resp_event_id);