events: Move replacement body text generation to MessageType

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

View File

@ -359,42 +359,7 @@ impl RoomMessageEventContent {
}, },
}); });
let empty_formatted_body = || FormattedBody::html(String::new()); self.msgtype.make_replacement_body();
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),
}
};
// Add replacement fallback.
*body = format!("* {body}");
if let Some(f) = formatted {
assert_eq!(
f.format,
MessageFormat::Html,
"make_replacement can't handle non-HTML formatted messages"
);
f.body = format!("* {}", f.body);
}
// Add reply fallback if needed. // Add reply fallback if needed.
if let Some(original_message) = replied_to_message { if let Some(original_message) = replied_to_message {
@ -887,6 +852,45 @@ impl MessageType {
); );
} }
} }
fn make_replacement_body(&mut self) {
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),
}
};
// Add replacement fallback.
*body = format!("* {body}");
if let Some(f) = formatted {
assert_eq!(
f.format,
MessageFormat::Html,
"make_replacement can't handle non-HTML formatted messages"
);
f.body = format!("* {}", f.body);
}
}
} }
impl From<MessageType> for RoomMessageEventContent { impl From<MessageType> for RoomMessageEventContent {