events: Change AddMentions::Yes to only mention the original sender
This commit is contained in:
parent
935ac70e28
commit
5c570b1604
@ -1,5 +1,10 @@
|
|||||||
# [unreleased]
|
# [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
|
# 0.27.0
|
||||||
|
|
||||||
The crate was split out of `ruma-common` again after `ruma-common 0.11.3`.
|
The crate was split out of `ruma-common` again after `ruma-common 0.11.3`.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//!
|
//!
|
||||||
//! [`m.room.message`]: https://spec.matrix.org/latest/client-server-api/#mroommessage
|
//! [`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 as_variant::as_variant;
|
||||||
use ruma_common::{
|
use ruma_common::{
|
||||||
@ -158,6 +158,7 @@ impl RoomMessageEventContent {
|
|||||||
add_mentions: AddMentions,
|
add_mentions: AddMentions,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let reply = self.make_reply_fallback(original_message.into());
|
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 {
|
let original_thread_id = if forward_thread == ForwardThread::Yes {
|
||||||
original_message
|
original_message
|
||||||
@ -170,21 +171,10 @@ impl RoomMessageEventContent {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let original_user_mentions = (add_mentions == AddMentions::Yes).then(|| {
|
let sender_for_mentions =
|
||||||
original_message
|
(add_mentions == AddMentions::Yes).then_some(&*original_message.sender);
|
||||||
.content
|
|
||||||
.mentions
|
|
||||||
.as_ref()
|
|
||||||
.map(|m| m.user_ids.clone())
|
|
||||||
.unwrap_or_default()
|
|
||||||
});
|
|
||||||
|
|
||||||
reply.make_reply_tweaks(
|
reply.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions)
|
||||||
original_message.event_id.clone(),
|
|
||||||
original_thread_id,
|
|
||||||
original_user_mentions,
|
|
||||||
Some(&original_message.sender),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turns `self` into a reply to the given raw event.
|
/// Turns `self` into a reply to the given raw event.
|
||||||
@ -223,8 +213,6 @@ impl RoomMessageEventContent {
|
|||||||
text: Option<String>,
|
text: Option<String>,
|
||||||
#[serde(rename = "m.relates_to")]
|
#[serde(rename = "m.relates_to")]
|
||||||
relates_to: Option<crate::room::encrypted::Relation>,
|
relates_to: Option<crate::room::encrypted::Relation>,
|
||||||
#[serde(rename = "m.mentions")]
|
|
||||||
mentions: Option<Mentions>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let sender = original_event.get_field::<OwnedUserId>("sender").ok().flatten();
|
let sender = original_event.get_field::<OwnedUserId>("sender").ok().flatten();
|
||||||
@ -266,15 +254,8 @@ impl RoomMessageEventContent {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let original_user_mentions = (add_mentions == AddMentions::Yes)
|
let sender_for_mentions = sender.as_deref().filter(|_| 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, sender_for_mentions)
|
||||||
|
|
||||||
reply.make_reply_tweaks(
|
|
||||||
original_event_id,
|
|
||||||
original_thread_id,
|
|
||||||
original_user_mentions,
|
|
||||||
sender.as_deref(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
@ -326,8 +307,7 @@ impl RoomMessageEventContent {
|
|||||||
mut self,
|
mut self,
|
||||||
original_event_id: OwnedEventId,
|
original_event_id: OwnedEventId,
|
||||||
original_thread_id: Option<OwnedEventId>,
|
original_thread_id: Option<OwnedEventId>,
|
||||||
original_user_mentions: Option<BTreeSet<OwnedUserId>>,
|
sender_for_mentions: Option<&UserId>,
|
||||||
original_sender: Option<&UserId>,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let relates_to = if let Some(event_id) = original_thread_id {
|
let relates_to = if let Some(event_id) = original_thread_id {
|
||||||
Relation::Thread(Thread::plain(event_id.to_owned(), original_event_id.to_owned()))
|
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);
|
self.relates_to = Some(relates_to);
|
||||||
|
|
||||||
if let (Some(sender), Some(mut user_ids)) = (original_sender, original_user_mentions) {
|
if let Some(sender) = sender_for_mentions {
|
||||||
// Add the sender.
|
self.mentions.get_or_insert_with(Mentions::new).user_ids.insert(sender.to_owned());
|
||||||
user_ids.insert(sender.to_owned());
|
|
||||||
self.mentions = Some(Mentions { user_ids, ..Default::default() });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -658,8 +636,7 @@ pub enum AddMentions {
|
|||||||
///
|
///
|
||||||
/// Set this if your client supports intentional mentions.
|
/// 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
|
/// The sender of the original event will be added to the mentions of this message.
|
||||||
/// every user mentioned in the original event.
|
|
||||||
Yes,
|
Yes,
|
||||||
|
|
||||||
/// Do not add intentional mentions to the reply.
|
/// Do not add intentional mentions to the reply.
|
||||||
|
@ -467,20 +467,20 @@ fn reply_add_mentions() {
|
|||||||
.make_reply_to(&first_message, ForwardThread::Yes, AddMentions::Yes);
|
.make_reply_to(&first_message, ForwardThread::Yes, AddMentions::Yes);
|
||||||
|
|
||||||
let mentions = second_message.mentions.clone().unwrap();
|
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);
|
assert!(!mentions.room);
|
||||||
|
|
||||||
second_message =
|
second_message =
|
||||||
second_message.add_mentions(Mentions::with_user_ids([user.clone(), other_friend.clone()]));
|
second_message.add_mentions(Mentions::with_user_ids([user.clone(), other_friend.clone()]));
|
||||||
|
|
||||||
let mentions = second_message.mentions.clone().unwrap();
|
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);
|
assert!(!mentions.room);
|
||||||
|
|
||||||
second_message = second_message.add_mentions(Mentions::with_room_mention());
|
second_message = second_message.add_mentions(Mentions::with_room_mention());
|
||||||
|
|
||||||
let mentions = second_message.mentions.unwrap();
|
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);
|
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.body, "This is **my** reply");
|
||||||
assert_eq!(text_msg.formatted.unwrap().body, "This is <strong>my</strong> reply");
|
assert_eq!(text_msg.formatted.unwrap().body, "This is <strong>my</strong> 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]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user