Commit 7a761479c8
Unsigned
Layout: unified · split
src/render.rs +75 −33
| @@ -91,17 +91,6 @@ impl RenderedEvent { | |||
| 91 | .and_then(|fallback| fallback.sender.clone()) | 91 | .and_then(|fallback| fallback.sender.clone()) |
| 92 | }) | 92 | }) |
| 93 | .unwrap_or_else(|| event_id.as_str().to_owned()); | 93 | .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 | |||
| 105 | let mut tags = self | 94 | let mut tags = self |
| 106 | .content | 95 | .content |
| 107 | .lines | 96 | .lines |
| @@ -110,7 +99,29 @@ impl RenderedEvent { | |||
| 110 | .unwrap_or_default(); | 99 | .unwrap_or_default(); |
| 111 | tags.push("matrix_reply".to_owned()); | 100 | tags.push("matrix_reply".to_owned()); |
| 112 | 101 | ||
| 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(..)); | ||
| 114 | 125 | ||
| 115 | self | 126 | self |
| 116 | } | 127 | } |
| @@ -1206,22 +1217,16 @@ fn anchor_uri_to_string(uri: &AnchorUri) -> Option<String> { | |||
| 1206 | } | 1217 | } |
| 1207 | } | 1218 | } |
| 1208 | 1219 | ||
| 1209 | fn compact_reply_preview(body: &str) -> String { | 1220 | fn reply_quote_lines(body: &str) -> impl Iterator<Item = String> + '_ { |
| 1210 | const MAX_PREVIEW_CHARS: usize = 160; | 1221 | body.lines().map(|line| { |
| 1211 | 1222 | let line = line.trim(); | |
| 1212 | let body = collapse_whitespace(body); | ||
| 1213 | 1223 | ||
| 1214 | if body.chars().count() <= MAX_PREVIEW_CHARS { | 1224 | if line.is_empty() { |
| 1215 | return body; | 1225 | ">".to_owned() |
| 1216 | } | 1226 | } else { |
| 1217 | 1227 | format!("> {}", line) | |
| 1218 | let mut preview = body | 1228 | } |
| 1219 | .chars() | 1229 | }) |
| 1220 | .take(MAX_PREVIEW_CHARS.saturating_sub(3)) | ||
| 1221 | .collect::<String>(); | ||
| 1222 | preview.push_str("..."); | ||
| 1223 | |||
| 1224 | preview | ||
| 1225 | } | 1230 | } |
| 1226 | 1231 | ||
| 1227 | fn collapse_whitespace(body: &str) -> String { | 1232 | fn collapse_whitespace(body: &str) -> String { |
| @@ -1661,7 +1666,7 @@ mod tests { | |||
| 1661 | } | 1666 | } |
| 1662 | 1667 | ||
| 1663 | #[test] | 1668 | #[test] |
| 1664 | fn reply_context_uses_formatted_reply_fallback_preview() { | 1669 | fn reply_context_uses_formatted_reply_fallback_quote() { |
| 1665 | let event_id = | 1670 | let event_id = |
| 1666 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); | 1671 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| 1667 | let body = "\ | 1672 | let body = "\ |
| @@ -1682,12 +1687,13 @@ mod tests { | |||
| 1682 | } | 1687 | } |
| 1683 | .add_reply_context(&event_id, Some("Alice")); | 1688 | .add_reply_context(&event_id, Some("Alice")); |
| 1684 | 1689 | ||
| 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); | ||
| 1686 | assert_eq!( | 1692 | assert_eq!( |
| 1687 | "Reply to Alice: Previous *message* text", | 1693 | "> Previous *message* text", |
| 1688 | rendered.content.lines[0].message | 1694 | rendered.content.lines[1].message |
| 1689 | ); | 1695 | ); |
| 1690 | assert_eq!("new message", rendered.content.lines[1].message); | 1696 | assert_eq!("new message", rendered.content.lines[2].message); |
| 1691 | } | 1697 | } |
| 1692 | 1698 | ||
| 1693 | #[test] | 1699 | #[test] |
| @@ -1713,9 +1719,45 @@ mod tests { | |||
| 1713 | .add_reply_context(&event_id, None); | 1719 | .add_reply_context(&event_id, None); |
| 1714 | 1720 | ||
| 1715 | assert_eq!( | 1721 | assert_eq!( |
| 1716 | "Reply to @alice:example.org: Previous message", | 1722 | "Reply to @alice:example.org:", |
| 1717 | rendered.content.lines[0].message | 1723 | rendered.content.lines[0].message |
| 1718 | ); | 1724 | ); |
| 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 | ); | ||
| 1719 | } | 1761 | } |
| 1720 | 1762 | ||
| 1721 | #[test] | 1763 | #[test] |