events: Add RoomMessageEventContentWithoutRelation::add_mentions

This commit is contained in:
Jonas Platte 2023-10-09 16:23:51 +02:00
parent 440a563355
commit e7ed90ad0f
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
4 changed files with 19 additions and 7 deletions

View File

@ -10,6 +10,7 @@ Improvements:
`RoomMessageEventContent`: `RoomMessageEventContent`:
- `make_reply_to` - `make_reply_to`
- `make_reply_to_raw` - `make_reply_to_raw`
- `add_mentions`
# 0.27.0 # 0.27.0

View File

@ -272,6 +272,11 @@ impl Mentions {
pub fn with_room_mention() -> Self { pub fn with_room_mention() -> Self {
Self { room: true, ..Default::default() } Self { room: true, ..Default::default() }
} }
fn add(&mut self, mentions: Self) {
self.user_ids.extend(mentions.user_ids);
self.room |= mentions.room;
}
} }
// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of // Wrapper around `Box<str>` that cannot be used in a meaningful way outside of

View File

@ -348,13 +348,7 @@ impl RoomMessageEventContent {
/// ///
/// [mentions]: https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions /// [mentions]: https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions
pub fn add_mentions(mut self, mentions: Mentions) -> Self { pub fn add_mentions(mut self, mentions: Mentions) -> Self {
if let Some(m) = &mut self.mentions { self.mentions.get_or_insert_with(Mentions::new).add(mentions);
m.user_ids.extend(mentions.user_ids);
m.room |= mentions.room;
} else {
self.mentions = Some(mentions);
}
self self
} }

View File

@ -213,6 +213,18 @@ impl RoomMessageEventContentWithoutRelation {
self.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions) self.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions)
} }
/// Add the given [mentions] to this event.
///
/// If no [`Mentions`] was set on this events, this sets it. Otherwise, this updates the current
/// mentions by extending the previous `user_ids` with the new ones, and applies a logical OR to
/// the values of `room`.
///
/// [mentions]: https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions
pub fn add_mentions(mut self, mentions: Mentions) -> Self {
self.mentions.get_or_insert_with(Mentions::new).add(mentions);
self
}
fn make_reply_tweaks( fn make_reply_tweaks(
mut self, mut self,
original_event_id: OwnedEventId, original_event_id: OwnedEventId,