events: Move reply fallback generation to MessageType

… from RoomMessageEventContent.
This commit is contained in:
Jonas Platte 2023-10-09 15:12:52 +02:00
parent 5c570b1604
commit 9b94117bf2
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C

View File

@ -152,12 +152,12 @@ impl RoomMessageEventContent {
/// Panics if `self` has a `formatted_body` with a format other than HTML. /// Panics if `self` has a `formatted_body` with a format other than HTML.
#[track_caller] #[track_caller]
pub fn make_reply_to( pub fn make_reply_to(
self, mut self,
original_message: &OriginalRoomMessageEvent, original_message: &OriginalRoomMessageEvent,
forward_thread: ForwardThread, forward_thread: ForwardThread,
add_mentions: AddMentions, add_mentions: AddMentions,
) -> Self { ) -> Self {
let reply = self.make_reply_fallback(original_message.into()); self.msgtype.add_reply_fallback(original_message.into());
let original_event_id = original_message.event_id.clone(); 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 {
@ -174,7 +174,7 @@ impl RoomMessageEventContent {
let sender_for_mentions = let sender_for_mentions =
(add_mentions == AddMentions::Yes).then_some(&*original_message.sender); (add_mentions == AddMentions::Yes).then_some(&*original_message.sender);
reply.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)
} }
/// Turns `self` into a reply to the given raw event. /// Turns `self` into a reply to the given raw event.
@ -196,7 +196,7 @@ impl RoomMessageEventContent {
/// Panics if `self` has a `formatted_body` with a format other than HTML. /// Panics if `self` has a `formatted_body` with a format other than HTML.
#[track_caller] #[track_caller]
pub fn make_reply_to_raw( pub fn make_reply_to_raw(
self, mut self,
original_event: &Raw<AnySyncTimelineEvent>, original_event: &Raw<AnySyncTimelineEvent>,
original_event_id: OwnedEventId, original_event_id: OwnedEventId,
room_id: &RoomId, room_id: &RoomId,
@ -228,7 +228,7 @@ impl RoomMessageEventContent {
}); });
// Only apply fallback if we managed to deserialize raw event. // Only apply fallback if we managed to deserialize raw event.
let reply = if let (Some(sender), Some((content, body))) = (&sender, content_body) { if let (Some(sender), Some((content, body))) = (&sender, content_body) {
let is_reply = let is_reply =
matches!(content.relates_to, Some(crate::room::encrypted::Relation::Reply { .. })); matches!(content.relates_to, Some(crate::room::encrypted::Relation::Reply { .. }));
let data = OriginalEventData { let data = OriginalEventData {
@ -241,10 +241,8 @@ impl RoomMessageEventContent {
sender, sender,
}; };
self.make_reply_fallback(data) self.msgtype.add_reply_fallback(data);
} else { }
self
};
let original_thread_id = if forward_thread == ForwardThread::Yes { let original_thread_id = if forward_thread == ForwardThread::Yes {
relates_to relates_to
@ -255,52 +253,7 @@ impl RoomMessageEventContent {
}; };
let sender_for_mentions = sender.as_deref().filter(|_| add_mentions == AddMentions::Yes); 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) self.make_reply_tweaks(original_event_id, original_thread_id, sender_for_mentions)
}
#[track_caller]
fn make_reply_fallback(mut self, original_event: OriginalEventData<'_>) -> Self {
let empty_formatted_body = || FormattedBody::html(String::new());
let (body, formatted) = {
match &mut self.msgtype {
MessageType::Emote(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Notice(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Text(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Audio(m) => (&mut m.body, None),
MessageType::File(m) => (&mut m.body, None),
MessageType::Image(m) => (&mut m.body, None),
MessageType::Location(m) => (&mut m.body, None),
MessageType::ServerNotice(m) => (&mut m.body, None),
MessageType::Video(m) => (&mut m.body, None),
MessageType::VerificationRequest(m) => (&mut m.body, None),
MessageType::_Custom(m) => (&mut m.body, None),
}
};
if let Some(f) = formatted {
assert_eq!(
f.format,
MessageFormat::Html,
"make_reply_to can't handle non-HTML formatted messages"
);
let formatted_body = &mut f.body;
(*body, *formatted_body) = reply::plain_and_formatted_reply_body(
body.as_str(),
(!formatted_body.is_empty()).then_some(formatted_body.as_str()),
original_event,
);
}
self
} }
fn make_reply_tweaks( fn make_reply_tweaks(
@ -891,6 +844,49 @@ impl MessageType {
} }
} }
} }
#[track_caller]
fn add_reply_fallback(&mut self, original_event: OriginalEventData<'_>) {
let empty_formatted_body = || FormattedBody::html(String::new());
let (body, formatted) = {
match self {
MessageType::Emote(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Notice(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Text(m) => {
(&mut m.body, Some(m.formatted.get_or_insert_with(empty_formatted_body)))
}
MessageType::Audio(m) => (&mut m.body, None),
MessageType::File(m) => (&mut m.body, None),
MessageType::Image(m) => (&mut m.body, None),
MessageType::Location(m) => (&mut m.body, None),
MessageType::ServerNotice(m) => (&mut m.body, None),
MessageType::Video(m) => (&mut m.body, None),
MessageType::VerificationRequest(m) => (&mut m.body, None),
MessageType::_Custom(m) => (&mut m.body, None),
}
};
if let Some(f) = formatted {
assert_eq!(
f.format,
MessageFormat::Html,
"can't add reply fallback to non-HTML formatted messages"
);
let formatted_body = &mut f.body;
(*body, *formatted_body) = reply::plain_and_formatted_reply_body(
body.as_str(),
(!formatted_body.is_empty()).then_some(formatted_body.as_str()),
original_event,
);
}
}
} }
impl From<MessageType> for RoomMessageEventContent { impl From<MessageType> for RoomMessageEventContent {