Commit 7a761479c8

7a761479c8df23926de3de0e0f3fbd0a960c029d

parent: 339b71ff0c

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-22 02:41 UTC
committer: <strk@kbt.io>

fix: render reply fallback as quoted context

Layout: unified · split

src/render.rs +75 −33
@@ -91,17 +91,6 @@ impl RenderedEvent {
9191 .and_then(|fallback| fallback.sender.clone())
9292 })
9393 .unwrap_or_else(|| event_id.as_str().to_owned());
94 let message = match reply_fallback {
95 Some(fallback) => {
96 format!(
97 "Reply to {}: {}",
98 reply_sender,
99 compact_reply_preview(&fallback.body)
100 )
101 }
102 None => format!("Reply to {}", reply_sender),
103 };
104
10594 let mut tags = self
10695 .content
10796 .lines
@@ -110,7 +99,29 @@ impl RenderedEvent {
11099 .unwrap_or_default();
111100 tags.push("matrix_reply".to_owned());
112101
113 self.content.lines.insert(0, RenderedLine { tags, message });
102 let mut context_lines = match reply_fallback {
103 Some(fallback) => {
104 let mut lines = vec![RenderedLine {
105 tags: tags.clone(),
106 message: format!("Reply to {}:", reply_sender),
107 }];
108
109 lines.extend(reply_quote_lines(&fallback.body).map(
110 |message| RenderedLine {
111 tags: tags.clone(),
112 message,
113 },
114 ));
115
116 lines
117 }
118 None => vec![RenderedLine {
119 tags,
120 message: format!("Reply to {}", reply_sender),
121 }],
122 };
123
124 self.content.lines.splice(0..0, context_lines.drain(..));
114125
115126 self
116127 }
@@ -1206,22 +1217,16 @@ fn anchor_uri_to_string(uri: &AnchorUri) -> Option<String> {
12061217 }
12071218}
12081219
1209fn compact_reply_preview(body: &str) -> String {
1210 const MAX_PREVIEW_CHARS: usize = 160;
1211
1212 let body = collapse_whitespace(body);
1220fn reply_quote_lines(body: &str) -> impl Iterator<Item = String> + '_ {
1221 body.lines().map(|line| {
1222 let line = line.trim();
12131223
1214 if body.chars().count() <= MAX_PREVIEW_CHARS {
1215 return body;
1216 }
1217
1218 let mut preview = body
1219 .chars()
1220 .take(MAX_PREVIEW_CHARS.saturating_sub(3))
1221 .collect::<String>();
1222 preview.push_str("...");
1223
1224 preview
1224 if line.is_empty() {
1225 ">".to_owned()
1226 } else {
1227 format!("> {}", line)
1228 }
1229 })
12251230}
12261231
12271232fn collapse_whitespace(body: &str) -> String {
@@ -1661,7 +1666,7 @@ mod tests {
16611666 }
16621667
16631668 #[test]
1664 fn reply_context_uses_formatted_reply_fallback_preview() {
1669 fn reply_context_uses_formatted_reply_fallback_quote() {
16651670 let event_id =
16661671 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org");
16671672 let body = "\
@@ -1682,12 +1687,13 @@ mod tests {
16821687 }
16831688 .add_reply_context(&event_id, Some("Alice"));
16841689
1685 assert_eq!(2, rendered.content.lines.len());
1690 assert_eq!(3, rendered.content.lines.len());
1691 assert_eq!("Reply to Alice:", rendered.content.lines[0].message);
16861692 assert_eq!(
1687 "Reply to Alice: Previous *message* text",
1688 rendered.content.lines[0].message
1693 "> Previous *message* text",
1694 rendered.content.lines[1].message
16891695 );
1690 assert_eq!("new message", rendered.content.lines[1].message);
1696 assert_eq!("new message", rendered.content.lines[2].message);
16911697 }
16921698
16931699 #[test]
@@ -1713,9 +1719,45 @@ mod tests {
17131719 .add_reply_context(&event_id, None);
17141720
17151721 assert_eq!(
1716 "Reply to @alice:example.org: Previous message",
1722 "Reply to @alice:example.org:",
17171723 rendered.content.lines[0].message
17181724 );
1725 assert_eq!("> Previous message", rendered.content.lines[1].message);
1726 }
1727
1728 #[test]
1729 fn reply_context_quotes_multiline_reply_fallback() {
1730 let event_id =
1731 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org");
1732 let body = "\
1733 <mx-reply>\
1734 <blockquote>\
1735 <a href=\"https://matrix.to/#/!room:example.org/$replyevent:example.org\">In reply to</a> \
1736 <a href=\"https://matrix.to/#/@alice:example.org\">@alice:example.org</a>\
1737 <br>\
1738 <p>line one</p><p>line two</p>\
1739 </blockquote>\
1740 </mx-reply>\
1741 <p>new message</p>";
1742 let content = TextMessageEventContent::html("fallback", body);
1743 let rendered = RenderedEvent {
1744 message_timestamp: 0,
1745 prefix: "bob\t".to_owned(),
1746 content: content.render(&()),
1747 }
1748 .add_reply_context(&event_id, Some("Alice"));
1749
1750 let messages = rendered
1751 .content
1752 .lines
1753 .iter()
1754 .map(|line| line.message.as_str())
1755 .collect::<Vec<_>>();
1756
1757 assert_eq!(
1758 vec!["Reply to Alice:", "> line one", "> line two", "new message"],
1759 messages
1760 );
17191761 }
17201762
17211763 #[test]