diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 0e6d65b4..5147a7c5 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -1,5 +1,10 @@ # [unreleased] +Improvements: + +- Calling `make_reply_to` or `make_reply_to_raw` with `AddMentions::Yes` no longer adds people + mentioned in the original message to mentions (only the sender of the original message) + # 0.27.0 The crate was split out of `ruma-common` again after `ruma-common 0.11.3`. diff --git a/crates/ruma-events/src/room/message.rs b/crates/ruma-events/src/room/message.rs index d4d28da6..5dae373a 100644 --- a/crates/ruma-events/src/room/message.rs +++ b/crates/ruma-events/src/room/message.rs @@ -2,7 +2,7 @@ //! //! [`m.room.message`]: https://spec.matrix.org/latest/client-server-api/#mroommessage -use std::{borrow::Cow, collections::BTreeSet}; +use std::borrow::Cow; use as_variant::as_variant; use ruma_common::{ @@ -158,6 +158,7 @@ impl RoomMessageEventContent { add_mentions: AddMentions, ) -> Self { let reply = self.make_reply_fallback(original_message.into()); + let original_event_id = original_message.event_id.clone(); let original_thread_id = if forward_thread == ForwardThread::Yes { original_message @@ -170,21 +171,10 @@ impl RoomMessageEventContent { None }; - let original_user_mentions = (add_mentions == AddMentions::Yes).then(|| { - original_message - .content - .mentions - .as_ref() - .map(|m| m.user_ids.clone()) - .unwrap_or_default() - }); + let sender_for_mentions = + (add_mentions == AddMentions::Yes).then_some(&*original_message.sender); - reply.make_reply_tweaks( - original_message.event_id.clone(), - original_thread_id, - original_user_mentions, - Some(&original_message.sender), - ) + reply.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions) } /// Turns `self` into a reply to the given raw event. @@ -223,8 +213,6 @@ impl RoomMessageEventContent { text: Option, #[serde(rename = "m.relates_to")] relates_to: Option, - #[serde(rename = "m.mentions")] - mentions: Option, } let sender = original_event.get_field::("sender").ok().flatten(); @@ -266,15 +254,8 @@ impl RoomMessageEventContent { None }; - let original_user_mentions = (add_mentions == AddMentions::Yes) - .then(|| content.and_then(|c| c.mentions).map(|m| m.user_ids).unwrap_or_default()); - - reply.make_reply_tweaks( - original_event_id, - original_thread_id, - original_user_mentions, - sender.as_deref(), - ) + let sender_for_mentions = sender.as_deref().filter(|_| add_mentions == AddMentions::Yes); + reply.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions) } #[track_caller] @@ -326,8 +307,7 @@ impl RoomMessageEventContent { mut self, original_event_id: OwnedEventId, original_thread_id: Option, - original_user_mentions: Option>, - original_sender: Option<&UserId>, + sender_for_mentions: Option<&UserId>, ) -> Self { let relates_to = if let Some(event_id) = original_thread_id { Relation::Thread(Thread::plain(event_id.to_owned(), original_event_id.to_owned())) @@ -336,10 +316,8 @@ impl RoomMessageEventContent { }; self.relates_to = Some(relates_to); - if let (Some(sender), Some(mut user_ids)) = (original_sender, original_user_mentions) { - // Add the sender. - user_ids.insert(sender.to_owned()); - self.mentions = Some(Mentions { user_ids, ..Default::default() }); + if let Some(sender) = sender_for_mentions { + self.mentions.get_or_insert_with(Mentions::new).user_ids.insert(sender.to_owned()); } self @@ -658,8 +636,7 @@ pub enum AddMentions { /// /// Set this if your client supports intentional mentions. /// - /// The sender of the original event will be added to the mentions of this message, along with - /// every user mentioned in the original event. + /// The sender of the original event will be added to the mentions of this message. Yes, /// Do not add intentional mentions to the reply. diff --git a/crates/ruma-events/tests/it/room_message.rs b/crates/ruma-events/tests/it/room_message.rs index 9f5d3aeb..2d512d16 100644 --- a/crates/ruma-events/tests/it/room_message.rs +++ b/crates/ruma-events/tests/it/room_message.rs @@ -467,20 +467,20 @@ fn reply_add_mentions() { .make_reply_to(&first_message, ForwardThread::Yes, AddMentions::Yes); let mentions = second_message.mentions.clone().unwrap(); - assert_eq!(mentions.user_ids, [friend.clone(), user.clone()].into()); + assert_eq!(mentions.user_ids, [user.clone()].into()); assert!(!mentions.room); second_message = second_message.add_mentions(Mentions::with_user_ids([user.clone(), other_friend.clone()])); let mentions = second_message.mentions.clone().unwrap(); - assert_eq!(mentions.user_ids, [friend.clone(), other_friend.clone(), user.clone()].into()); + assert_eq!(mentions.user_ids, [other_friend.clone(), user.clone()].into()); assert!(!mentions.room); second_message = second_message.add_mentions(Mentions::with_room_mention()); let mentions = second_message.mentions.unwrap(); - assert_eq!(mentions.user_ids, [friend, other_friend, user].into()); + assert_eq!(mentions.user_ids, [other_friend, user].into()); assert!(mentions.room); } @@ -690,7 +690,7 @@ fn reply_to_raw_add_mentions() { assert_eq!(text_msg.body, "This is **my** reply"); assert_eq!(text_msg.formatted.unwrap().body, "This is my reply"); - assert_eq!(reply.mentions.unwrap().user_ids, BTreeSet::from([user_id, other_user_id])); + assert_eq!(reply.mentions.unwrap().user_ids, BTreeSet::from([user_id])); } #[test]