Commit 13a2514adc

13a2514adc8fc1832aa62c80f653eb5343df5770

parent: 46498c7565

Unregistered key

Asynonymous <asynonymous@gmail.com> · 2026-10-30 23:35 UTC
committer: <noreply@github.com>

Fix stripping reply fallback from messages (#795)

Co-authored-by: Asynonymous <Asynonymous@github.com>
Co-authored-by: vaw <git@nlih.de>

Layout: unified · split

src/message/mod.rs +46 −8
@@ -19,6 +19,7 @@ use matrix_sdk::ruma::events::room::encrypted::{
1919 RoomEncryptedEvent,
2020};
2121use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation;
22use matrix_sdk::ruma::events::room::message::sanitize::remove_plain_reply_fallback;
2223use matrix_sdk::ruma::events::room::message::{
2324 FormattedBody,
2425 MessageFormat,
@@ -96,7 +97,9 @@ impl Messages {
9697 pub fn insert_message(&mut self, key: MessageKey, msg: impl Into<Message>) {
9798 let mut msg = msg.into();
9899 if let MessageEvent::Original(ev, edits) = &mut msg.event {
99 strip_reply_fallback(&mut ev.content.msgtype);
100 if has_reply_fallback(ev.content.relates_to.as_ref()) {
101 strip_reply_fallback(&mut ev.content.msgtype);
102 }
100103 for edit in edits.values_mut() {
101104 strip_reply_fallback(&mut edit.msgtype);
102105 }
@@ -372,18 +375,19 @@ fn redaction_reason_event(ev: SyncRoomRedactionEvent) -> Option<String> {
372375 ev.content.reason
373376}
374377
378fn has_reply_fallback(relation: Option<&Relation<RoomMessageEventContentWithoutRelation>>) -> bool {
379 matches!(
380 relation,
381 Some(Relation::Reply(_) | Relation::Thread(Thread { in_reply_to: Some(_), .. }))
382 )
383}
384
375385pub fn strip_reply_fallback(msgtype: &mut MessageType) {
376386 let MessageType::Text(content) = msgtype else {
377387 return;
378388 };
379389
380 if !content.body.starts_with('>') {
381 return;
382 }
383
384 let new_body = content.body.lines().skip_while(|line| line.starts_with('>')).collect();
385
386 content.body = new_body;
390 content.body = remove_plain_reply_fallback(&content.body).to_owned();
387391}
388392
389393fn content_html(msgtype: &MessageType) -> Option<StyleTree> {
@@ -1690,6 +1694,7 @@ impl Display for Message {
16901694pub mod tests {
16911695 use super::*;
16921696
1697 use matrix_sdk::ruma::events::relation::Reply;
16931698 use matrix_sdk::ruma::events::room::ImageInfo;
16941699 use matrix_sdk::ruma::events::room::message::{
16951700 AudioInfo,
@@ -1697,6 +1702,7 @@ pub mod tests {
16971702 FileInfo,
16981703 FileMessageEventContent,
16991704 ImageMessageEventContent,
1705 TextMessageEventContent,
17001706 VideoInfo,
17011707 VideoMessageEventContent,
17021708 };
@@ -1705,6 +1711,38 @@ pub mod tests {
17051711 use crate::config::time_gutter_width;
17061712 use crate::tests::*;
17071713
1714 #[test]
1715 fn test_has_reply_fallback() {
1716 let root = OwnedEventId::try_from("$root:example.org").unwrap();
1717 let target = OwnedEventId::try_from("$target:example.org").unwrap();
1718
1719 let reply: Relation<RoomMessageEventContentWithoutRelation> =
1720 Relation::Reply(Reply::with_event_id(target.clone()));
1721 assert!(has_reply_fallback(Some(&reply)));
1722
1723 let thread_reply: Relation<RoomMessageEventContentWithoutRelation> =
1724 Relation::Thread(Thread::reply(root.clone(), target.clone()));
1725 assert!(has_reply_fallback(Some(&thread_reply)));
1726
1727 let thread_fallback: Relation<RoomMessageEventContentWithoutRelation> =
1728 Relation::Thread(Thread::plain(root.clone(), target));
1729 assert!(has_reply_fallback(Some(&thread_fallback)));
1730
1731 let thread_without_fallback: Relation<RoomMessageEventContentWithoutRelation> =
1732 Relation::Thread(Thread::without_fallback(root));
1733 assert!(!has_reply_fallback(Some(&thread_without_fallback)));
1734 assert!(!has_reply_fallback(None));
1735 }
1736
1737 #[test]
1738 fn test_strip_reply_fallback() {
1739 let body = "> <@user:example.org> quoted text\n> more quoted text\n\nreply";
1740 let mut msgtype = MessageType::Text(TextMessageEventContent::plain(body));
1741
1742 strip_reply_fallback(&mut msgtype);
1743 assert_eq!(msgtype.body(), "reply");
1744 }
1745
17081746 #[test]
17091747 fn test_mc_cmp() {
17101748 let mc1 = MessageCursor::from(MSG1_KEY.clone());