From 56ccda4dedb55988ddc4482d6bfb0a3c7f7f3d72 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 2 Oct 2024 16:20:12 +0200 Subject: [PATCH] timeline: apply Message edits in a single place --- .../src/timeline/event_handler.rs | 24 +++-------- .../timeline/event_item/content/message.rs | 40 +++++++++---------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/crates/matrix-sdk-ui/src/timeline/event_handler.rs b/crates/matrix-sdk-ui/src/timeline/event_handler.rs index fd7769393..83c45c71f 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_handler.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_handler.rs @@ -41,7 +41,6 @@ use ruma::{ AnySyncTimelineEvent, BundledMessageLikeRelations, EventContent, FullStateEventContent, MessageLikeEventType, StateEventType, SyncStateEvent, }, - html::RemoveReplyFallback, serde::Raw, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedTransactionId, OwnedUserId, RoomVersionId, }; @@ -58,8 +57,8 @@ use super::{ polls::PollState, reactions::FullReactionKey, util::{rfind_event_by_id, rfind_event_item}, - EventTimelineItem, InReplyToDetails, Message, OtherState, Sticker, TimelineDetails, - TimelineItem, TimelineItemContent, + EventTimelineItem, InReplyToDetails, OtherState, Sticker, TimelineDetails, TimelineItem, + TimelineItemContent, }; use crate::{ events::SyncTimelineEventWithoutContent, @@ -68,7 +67,6 @@ use crate::{ event_item::{ReactionInfo, ReactionStatus}, reactions::PendingReaction, }, - DEFAULT_SANITIZER_MODE, }; /// When adding an event, useful information related to the source of the event. @@ -601,25 +599,15 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> { return None; }; - let mut msgtype = replacement.new_content.msgtype; - - // Edit's content is never supposed to contain the reply fallback. - msgtype.sanitize(DEFAULT_SANITIZER_MODE, RemoveReplyFallback::No); - - let new_content = TimelineItemContent::Message(Message { - msgtype, - in_reply_to: msg.in_reply_to.clone(), - thread_root: msg.thread_root.clone(), - edited: true, - mentions: replacement.new_content.mentions, - }); - let edit_json = match &self.ctx.flow { Flow::Local { .. } => None, Flow::Remote { raw_event, .. } => Some(raw_event.clone()), }; - let mut new_item = item.with_content(new_content, edit_json); + let mut new_msg = msg.clone(); + new_msg.apply_edit(replacement.new_content); + + let mut new_item = item.with_content(TimelineItemContent::Message(new_msg), edit_json); if let EventTimelineItemKind::Remote(remote_event) = &item.kind { if let Flow::Remote { encryption_info, .. } = &self.ctx.flow { 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 3d11bb9da..6d04d72cf 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 @@ -62,8 +62,6 @@ impl Message { edit: Option, timeline_items: &Vector>, ) -> Self { - let edited = edit.is_some(); - let mut thread_root = None; let in_reply_to = c.relates_to.and_then(|relation| match relation { Relation::Reply { in_reply_to } => { @@ -78,27 +76,29 @@ impl Message { _ => None, }); - let (msgtype, mentions) = match edit { - Some(mut e) => { - // Edit's content is never supposed to contain the reply fallback. - e.msgtype.sanitize(DEFAULT_SANITIZER_MODE, RemoveReplyFallback::No); - (e.msgtype, e.mentions) - } + let remove_reply_fallback = + if in_reply_to.is_some() { RemoveReplyFallback::Yes } else { RemoveReplyFallback::No }; - None => { - let remove_reply_fallback = if in_reply_to.is_some() { - RemoveReplyFallback::Yes - } else { - RemoveReplyFallback::No - }; + let mut msgtype = c.msgtype; + msgtype.sanitize(DEFAULT_SANITIZER_MODE, remove_reply_fallback); - let mut msgtype = c.msgtype; - msgtype.sanitize(DEFAULT_SANITIZER_MODE, remove_reply_fallback); - (msgtype, c.mentions) - } - }; + let mut ret = + Self { msgtype, in_reply_to, thread_root, edited: false, mentions: c.mentions }; - Self { msgtype, in_reply_to, thread_root, edited, mentions } + if let Some(edit) = edit { + ret.apply_edit(edit); + } + + ret + } + + /// Apply an edit to the current message. + pub(crate) fn apply_edit(&mut self, mut new_content: RoomMessageEventContentWithoutRelation) { + // Edit's content is never supposed to contain the reply fallback. + new_content.msgtype.sanitize(DEFAULT_SANITIZER_MODE, RemoveReplyFallback::No); + self.msgtype = new_content.msgtype; + self.mentions = new_content.mentions; + self.edited = true; } /// Get the `msgtype`-specific data of this message.