| @@ -41,7 +41,7 @@ use matrix_sdk::{ |
| 41 | 41 | }; |
| 42 | 42 | use ruma_html::{ |
| 43 | 43 | matrix::{AnchorUri, MatrixElement}, |
| 44 | | Html, NodeData, NodeRef, |
| 44 | Html, NodeData, NodeRef, SanitizerConfig, |
| 45 | 45 | }; |
| 46 | 46 | |
| 47 | 47 | use weechat::{Prefix, Weechat}; |
| @@ -82,6 +82,26 @@ impl RenderedEvent { |
| 82 | 82 | event_id: &EventId, |
| 83 | 83 | sender: Option<&str>, |
| 84 | 84 | ) -> Self { |
| 85 | let reply_fallback = self.content.reply_fallback.take(); |
| 86 | let reply_sender = sender |
| 87 | .map(ToOwned::to_owned) |
| 88 | .or_else(|| { |
| 89 | reply_fallback |
| 90 | .as_ref() |
| 91 | .and_then(|fallback| fallback.sender.clone()) |
| 92 | }) |
| 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 | |
| 85 | 105 | let mut tags = self |
| 86 | 106 | .content |
| 87 | 107 | .lines |
| @@ -90,16 +110,7 @@ impl RenderedEvent { |
| 90 | 110 | .unwrap_or_default(); |
| 91 | 111 | tags.push("matrix_reply".to_owned()); |
| 92 | 112 | |
| 93 | | self.content.lines.insert( |
| 94 | | 0, |
| 95 | | RenderedLine { |
| 96 | | tags, |
| 97 | | message: format!( |
| 98 | | "Reply to {}", |
| 99 | | sender.unwrap_or_else(|| event_id.as_str()) |
| 100 | | ), |
| 101 | | }, |
| 102 | | ); |
| 113 | self.content.lines.insert(0, RenderedLine { tags, message }); |
| 103 | 114 | |
| 104 | 115 | self |
| 105 | 116 | } |
| @@ -117,6 +128,30 @@ pub struct RenderedLine { |
| 117 | 128 | pub struct RenderedContent { |
| 118 | 129 | /// The collection of lines that the event has. |
| 119 | 130 | pub lines: Vec<RenderedLine>, |
| 131 | reply_fallback: Option<ReplyFallback>, |
| 132 | } |
| 133 | |
| 134 | impl RenderedContent { |
| 135 | fn new(lines: Vec<RenderedLine>) -> Self { |
| 136 | Self { |
| 137 | lines, |
| 138 | reply_fallback: None, |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | fn with_reply_fallback( |
| 143 | mut self, |
| 144 | reply_fallback: Option<ReplyFallback>, |
| 145 | ) -> Self { |
| 146 | self.reply_fallback = reply_fallback; |
| 147 | self |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[derive(Clone, Debug)] |
| 152 | struct ReplyFallback { |
| 153 | sender: Option<String>, |
| 154 | body: String, |
| 120 | 155 | } |
| 121 | 156 | |
| 122 | 157 | /// Trait allowing events to be rendered for Weechat. |
| @@ -266,7 +301,7 @@ impl Render for EmoteMessageEventContent { |
| 266 | 301 | tags: self.tags(), |
| 267 | 302 | }; |
| 268 | 303 | |
| 269 | | RenderedContent { lines: vec![line] } |
| 304 | RenderedContent::new(vec![line]) |
| 270 | 305 | } |
| 271 | 306 | } |
| 272 | 307 | |
| @@ -294,7 +329,7 @@ impl Render for LocationMessageEventContent { |
| 294 | 329 | tags: self.tags(), |
| 295 | 330 | }; |
| 296 | 331 | |
| 297 | | RenderedContent { lines: vec![line] } |
| 332 | RenderedContent::new(vec![line]) |
| 298 | 333 | } |
| 299 | 334 | } |
| 300 | 335 | |
| @@ -324,7 +359,7 @@ impl Render for NoticeMessageEventContent { |
| 324 | 359 | tags: self.tags(), |
| 325 | 360 | }; |
| 326 | 361 | |
| 327 | | RenderedContent { lines: vec![line] } |
| 362 | RenderedContent::new(vec![line]) |
| 328 | 363 | } |
| 329 | 364 | } |
| 330 | 365 | |
| @@ -352,7 +387,7 @@ impl Render for ServerNoticeMessageEventContent { |
| 352 | 387 | tags: self.tags(), |
| 353 | 388 | }; |
| 354 | 389 | |
| 355 | | RenderedContent { lines: vec![line] } |
| 390 | RenderedContent::new(vec![line]) |
| 356 | 391 | } |
| 357 | 392 | } |
| 358 | 393 | |
| @@ -493,7 +528,7 @@ impl<C: HasUrlOrFile> Render for C { |
| 493 | 528 | tags: self.tags(), |
| 494 | 529 | }; |
| 495 | 530 | |
| 496 | | RenderedContent { lines: vec![line] } |
| 531 | RenderedContent::new(vec![line]) |
| 497 | 532 | } |
| 498 | 533 | } |
| 499 | 534 | |
| @@ -517,7 +552,7 @@ impl Render for RoomEncryptedEventContent { |
| 517 | 552 | tags: self.tags(), |
| 518 | 553 | }; |
| 519 | 554 | |
| 520 | | RenderedContent { lines: vec![line] } |
| 555 | RenderedContent::new(vec![line]) |
| 521 | 556 | } |
| 522 | 557 | } |
| 523 | 558 | |
| @@ -541,7 +576,7 @@ impl Render for RedactedSyncMessageLikeEvent<RedactedRoomMessageEventContent> { |
| 541 | 576 | tags: self.tags(), |
| 542 | 577 | }; |
| 543 | 578 | |
| 544 | | RenderedContent { lines: vec![line] } |
| 579 | RenderedContent::new(vec![line]) |
| 545 | 580 | } |
| 546 | 581 | } |
| 547 | 582 | |
| @@ -590,9 +625,7 @@ macro_rules! render_start_content { |
| 590 | 625 | // We auto accept emoji verifications that start |
| 591 | 626 | // from a verification request, so don't print |
| 592 | 627 | // anything. |
| 593 | | return RenderedContent { |
| 594 | | lines: vec![], |
| 595 | | } |
| 628 | return RenderedContent::new(vec![]); |
| 596 | 629 | } else { |
| 597 | 630 | format!( |
| 598 | 631 | "You have started an interactive emoji \ |
| @@ -617,9 +650,7 @@ macro_rules! render_start_content { |
| 617 | 650 | // We auto accept emoji verifications that start |
| 618 | 651 | // from a verification request, so don't print |
| 619 | 652 | // anything. |
| 620 | | return RenderedContent { |
| 621 | | lines: vec![], |
| 622 | | } |
| 653 | return RenderedContent::new(vec![]); |
| 623 | 654 | } |
| 624 | 655 | } |
| 625 | 656 | } |
| @@ -636,12 +667,10 @@ macro_rules! render_start_content { |
| 636 | 667 | _ => unreachable!(), |
| 637 | 668 | }; |
| 638 | 669 | |
| 639 | | RenderedContent { |
| 640 | | lines: vec![RenderedLine { |
| 670 | RenderedContent::new(vec![RenderedLine { |
| 641 | 671 | message, |
| 642 | 672 | tags: self.tags(), |
| 643 | | }], |
| 644 | | } |
| 673 | }]) |
| 645 | 674 | } |
| 646 | 675 | } |
| 647 | 676 | }; |
| @@ -686,12 +715,10 @@ macro_rules! render_request_content { |
| 686 | 715 | } |
| 687 | 716 | }; |
| 688 | 717 | |
| 689 | | RenderedContent { |
| 690 | | lines: vec![RenderedLine { |
| 691 | | message, |
| 692 | | tags: self.tags(), |
| 693 | | }], |
| 694 | | } |
| 718 | RenderedContent::new(vec![RenderedLine { |
| 719 | message, |
| 720 | tags: self.tags(), |
| 721 | }]) |
| 695 | 722 | } |
| 696 | 723 | } |
| 697 | 724 | }; |
| @@ -723,12 +750,10 @@ macro_rules! render_ready_content { |
| 723 | 750 | ) |
| 724 | 751 | }; |
| 725 | 752 | |
| 726 | | RenderedContent { |
| 727 | | lines: vec![RenderedLine { |
| 728 | | message, |
| 729 | | tags: self.tags(), |
| 730 | | }], |
| 731 | | } |
| 753 | RenderedContent::new(vec![RenderedLine { |
| 754 | message, |
| 755 | tags: self.tags(), |
| 756 | }]) |
| 732 | 757 | } |
| 733 | 758 | } |
| 734 | 759 | }; |
| @@ -782,7 +807,7 @@ macro_rules! render_key_content { |
| 782 | 807 | }) |
| 783 | 808 | .collect(); |
| 784 | 809 | |
| 785 | | RenderedContent { lines } |
| 810 | RenderedContent::new(lines) |
| 786 | 811 | } |
| 787 | 812 | } |
| 788 | 813 | }; |
| @@ -795,7 +820,10 @@ fn render_formatted_message_body( |
| 795 | 820 | body: &str, |
| 796 | 821 | tags: Vec<String>, |
| 797 | 822 | ) -> RenderedContent { |
| 798 | | render_plain_message_body(&formatted_body_to_plain_text(body), tags) |
| 823 | let body = formatted_message_body_to_plain_text(body); |
| 824 | |
| 825 | render_plain_message_body(&body.text, tags) |
| 826 | .with_reply_fallback(body.reply_fallback) |
| 799 | 827 | } |
| 800 | 828 | |
| 801 | 829 | fn render_plain_message_body(body: &str, tags: Vec<String>) -> RenderedContent { |
| @@ -807,11 +835,30 @@ fn render_plain_message_body(body: &str, tags: Vec<String>) -> RenderedContent { |
| 807 | 835 | }) |
| 808 | 836 | .collect(); |
| 809 | 837 | |
| 810 | | RenderedContent { lines } |
| 838 | RenderedContent::new(lines) |
| 811 | 839 | } |
| 812 | 840 | |
| 813 | 841 | fn formatted_body_to_plain_text(body: &str) -> String { |
| 842 | formatted_message_body_to_plain_text(body).text |
| 843 | } |
| 844 | |
| 845 | struct FormattedMessageBody { |
| 846 | text: String, |
| 847 | reply_fallback: Option<ReplyFallback>, |
| 848 | } |
| 849 | |
| 850 | fn formatted_message_body_to_plain_text(body: &str) -> FormattedMessageBody { |
| 814 | 851 | let html = Html::parse(body); |
| 852 | html.sanitize_with(&SanitizerConfig::compat()); |
| 853 | |
| 854 | let mut reply_fallback = None; |
| 855 | |
| 856 | for node in html.children() { |
| 857 | if reply_fallback.is_none() { |
| 858 | reply_fallback = render_reply_fallback(&node); |
| 859 | } |
| 860 | } |
| 861 | |
| 815 | 862 | html.sanitize(); |
| 816 | 863 | |
| 817 | 864 | let mut output = String::new(); |
| @@ -821,7 +868,10 @@ fn formatted_body_to_plain_text(body: &str) -> String { |
| 821 | 868 | render_html_node(&node, &mut output, &mut context); |
| 822 | 869 | } |
| 823 | 870 | |
| 824 | | trim_newlines(output) |
| 871 | FormattedMessageBody { |
| 872 | text: trim_newlines(output), |
| 873 | reply_fallback, |
| 874 | } |
| 825 | 875 | } |
| 826 | 876 | |
| 827 | 877 | #[derive(Clone, Default)] |
| @@ -963,6 +1013,158 @@ fn render_html_node( |
| 963 | 1013 | } |
| 964 | 1014 | } |
| 965 | 1015 | |
| 1016 | fn render_reply_fallback(node: &NodeRef) -> Option<ReplyFallback> { |
| 1017 | let reply_node = find_first_matrix_reply(node)?; |
| 1018 | let blockquote = find_first_blockquote(&reply_node)?; |
| 1019 | let body = render_reply_fallback_body(&blockquote); |
| 1020 | |
| 1021 | if body.is_empty() { |
| 1022 | return None; |
| 1023 | } |
| 1024 | |
| 1025 | Some(ReplyFallback { |
| 1026 | sender: render_reply_fallback_sender(&blockquote), |
| 1027 | body, |
| 1028 | }) |
| 1029 | } |
| 1030 | |
| 1031 | fn find_first_matrix_reply(node: &NodeRef) -> Option<NodeRef> { |
| 1032 | if is_matrix_element(node, |element| { |
| 1033 | matches!(element, MatrixElement::MatrixReply) |
| 1034 | }) { |
| 1035 | return Some(node.clone()); |
| 1036 | } |
| 1037 | |
| 1038 | node.children() |
| 1039 | .find_map(|child| find_first_matrix_reply(&child)) |
| 1040 | } |
| 1041 | |
| 1042 | fn find_first_blockquote(node: &NodeRef) -> Option<NodeRef> { |
| 1043 | if is_matrix_element(node, |element| { |
| 1044 | matches!(element, MatrixElement::Blockquote) |
| 1045 | }) { |
| 1046 | return Some(node.clone()); |
| 1047 | } |
| 1048 | |
| 1049 | node.children() |
| 1050 | .find_map(|child| find_first_blockquote(&child)) |
| 1051 | } |
| 1052 | |
| 1053 | fn is_matrix_element<F>(node: &NodeRef, predicate: F) -> bool |
| 1054 | where |
| 1055 | F: FnOnce(MatrixElement) -> bool, |
| 1056 | { |
| 1057 | match node.data() { |
| 1058 | NodeData::Element(element) => predicate(element.to_matrix().element), |
| 1059 | _ => false, |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | fn render_reply_fallback_sender(blockquote: &NodeRef) -> Option<String> { |
| 1064 | let mut anchors = Vec::new(); |
| 1065 | |
| 1066 | collect_anchor_text_before_first_br(blockquote, &mut anchors); |
| 1067 | |
| 1068 | anchors |
| 1069 | .into_iter() |
| 1070 | .filter_map(|anchor| { |
| 1071 | let anchor = collapse_whitespace(&anchor); |
| 1072 | (!anchor.is_empty() && !anchor.eq_ignore_ascii_case("in reply to")) |
| 1073 | .then_some(anchor) |
| 1074 | }) |
| 1075 | .next_back() |
| 1076 | } |
| 1077 | |
| 1078 | fn collect_anchor_text_before_first_br( |
| 1079 | node: &NodeRef, |
| 1080 | anchors: &mut Vec<String>, |
| 1081 | ) -> bool { |
| 1082 | match node.data() { |
| 1083 | NodeData::Element(element) |
| 1084 | if matches!(element.to_matrix().element, MatrixElement::Br) => |
| 1085 | { |
| 1086 | return true; |
| 1087 | } |
| 1088 | NodeData::Element(element) |
| 1089 | if matches!(element.to_matrix().element, MatrixElement::A(_)) => |
| 1090 | { |
| 1091 | let text = plain_text_children(node); |
| 1092 | if !text.trim().is_empty() { |
| 1093 | anchors.push(text); |
| 1094 | } |
| 1095 | } |
| 1096 | _ => {} |
| 1097 | } |
| 1098 | |
| 1099 | for child in node.children() { |
| 1100 | if collect_anchor_text_before_first_br(&child, anchors) { |
| 1101 | return true; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | false |
| 1106 | } |
| 1107 | |
| 1108 | fn plain_text_children(node: &NodeRef) -> String { |
| 1109 | let mut text = String::new(); |
| 1110 | |
| 1111 | collect_plain_text(node, &mut text); |
| 1112 | |
| 1113 | text |
| 1114 | } |
| 1115 | |
| 1116 | fn collect_plain_text(node: &NodeRef, output: &mut String) { |
| 1117 | match node.data() { |
| 1118 | NodeData::Text(text) => output.push_str(&text.borrow()), |
| 1119 | _ => { |
| 1120 | for child in node.children() { |
| 1121 | collect_plain_text(&child, output); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | fn render_reply_fallback_body(blockquote: &NodeRef) -> String { |
| 1128 | let mut output = String::new(); |
| 1129 | let mut context = HtmlRenderContext::default(); |
| 1130 | let mut after_intro = false; |
| 1131 | |
| 1132 | for child in blockquote.children() { |
| 1133 | render_after_first_br( |
| 1134 | &child, |
| 1135 | &mut output, |
| 1136 | &mut context, |
| 1137 | &mut after_intro, |
| 1138 | ); |
| 1139 | } |
| 1140 | |
| 1141 | trim_newlines(output) |
| 1142 | } |
| 1143 | |
| 1144 | fn render_after_first_br( |
| 1145 | node: &NodeRef, |
| 1146 | output: &mut String, |
| 1147 | context: &mut HtmlRenderContext, |
| 1148 | after_intro: &mut bool, |
| 1149 | ) { |
| 1150 | if !*after_intro { |
| 1151 | if is_matrix_element(node, |element| { |
| 1152 | matches!(element, MatrixElement::Br) |
| 1153 | }) { |
| 1154 | *after_intro = true; |
| 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | for child in node.children() { |
| 1159 | render_after_first_br(&child, output, context, after_intro); |
| 1160 | } |
| 1161 | |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | render_html_node(node, output, context); |
| 1166 | } |
| 1167 | |
| 966 | 1168 | fn render_html_children( |
| 967 | 1169 | node: &NodeRef, |
| 968 | 1170 | output: &mut String, |
| @@ -1004,6 +1206,28 @@ fn anchor_uri_to_string(uri: &AnchorUri) -> Option<String> { |
| 1004 | 1206 | } |
| 1005 | 1207 | } |
| 1006 | 1208 | |
| 1209 | fn compact_reply_preview(body: &str) -> String { |
| 1210 | const MAX_PREVIEW_CHARS: usize = 160; |
| 1211 | |
| 1212 | let body = collapse_whitespace(body); |
| 1213 | |
| 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 |
| 1225 | } |
| 1226 | |
| 1227 | fn collapse_whitespace(body: &str) -> String { |
| 1228 | body.split_whitespace().collect::<Vec<_>>().join(" ") |
| 1229 | } |
| 1230 | |
| 1007 | 1231 | fn prefix_lines(text: String, prefix: &str) -> String { |
| 1008 | 1232 | text.lines() |
| 1009 | 1233 | .map(|line| { |
| @@ -1397,12 +1621,10 @@ mod tests { |
| 1397 | 1621 | let rendered = RenderedEvent { |
| 1398 | 1622 | message_timestamp: 0, |
| 1399 | 1623 | prefix: "alice\t".to_owned(), |
| 1400 | | content: RenderedContent { |
| 1401 | | lines: vec![RenderedLine { |
| 1402 | | tags: vec!["matrix_text".to_owned()], |
| 1403 | | message: "reply body".to_owned(), |
| 1404 | | }], |
| 1405 | | }, |
| 1624 | content: RenderedContent::new(vec![RenderedLine { |
| 1625 | tags: vec!["matrix_text".to_owned()], |
| 1626 | message: "reply body".to_owned(), |
| 1627 | }]), |
| 1406 | 1628 | } |
| 1407 | 1629 | .add_reply_context(&event_id, Some("Alice")); |
| 1408 | 1630 | |
| @@ -1421,12 +1643,10 @@ mod tests { |
| 1421 | 1643 | let rendered = RenderedEvent { |
| 1422 | 1644 | message_timestamp: 0, |
| 1423 | 1645 | prefix: "alice\t".to_owned(), |
| 1424 | | content: RenderedContent { |
| 1425 | | lines: vec![RenderedLine { |
| 1426 | | tags: vec!["matrix_text".to_owned()], |
| 1427 | | message: "reply body".to_owned(), |
| 1428 | | }], |
| 1429 | | }, |
| 1646 | content: RenderedContent::new(vec![RenderedLine { |
| 1647 | tags: vec!["matrix_text".to_owned()], |
| 1648 | message: "reply body".to_owned(), |
| 1649 | }]), |
| 1430 | 1650 | } |
| 1431 | 1651 | .add_reply_context(&event_id, None); |
| 1432 | 1652 | |
| @@ -1440,6 +1660,64 @@ mod tests { |
| 1440 | 1660 | assert_eq!("reply body", rendered.content.lines[1].message); |
| 1441 | 1661 | } |
| 1442 | 1662 | |
| 1663 | #[test] |
| 1664 | fn reply_context_uses_formatted_reply_fallback_preview() { |
| 1665 | let event_id = |
| 1666 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| 1667 | let body = "\ |
| 1668 | <mx-reply>\ |
| 1669 | <blockquote>\ |
| 1670 | <a href=\"https://matrix.to/#/!room:example.org/$replyevent:example.org\">In reply to</a> \ |
| 1671 | <a href=\"https://matrix.to/#/@alice:example.org\">@alice:example.org</a>\ |
| 1672 | <br>\ |
| 1673 | <p>Previous <strong>message</strong> text</p>\ |
| 1674 | </blockquote>\ |
| 1675 | </mx-reply>\ |
| 1676 | <p>new message</p>"; |
| 1677 | let content = TextMessageEventContent::html("fallback", body); |
| 1678 | let rendered = RenderedEvent { |
| 1679 | message_timestamp: 0, |
| 1680 | prefix: "bob\t".to_owned(), |
| 1681 | content: content.render(&()), |
| 1682 | } |
| 1683 | .add_reply_context(&event_id, Some("Alice")); |
| 1684 | |
| 1685 | assert_eq!(2, rendered.content.lines.len()); |
| 1686 | assert_eq!( |
| 1687 | "Reply to Alice: Previous *message* text", |
| 1688 | rendered.content.lines[0].message |
| 1689 | ); |
| 1690 | assert_eq!("new message", rendered.content.lines[1].message); |
| 1691 | } |
| 1692 | |
| 1693 | #[test] |
| 1694 | fn reply_context_uses_reply_fallback_sender_when_local_sender_is_unknown() { |
| 1695 | let event_id = |
| 1696 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| 1697 | let body = "\ |
| 1698 | <mx-reply>\ |
| 1699 | <blockquote>\ |
| 1700 | <a href=\"https://matrix.to/#/!room:example.org/$replyevent:example.org\">In reply to</a> \ |
| 1701 | <a href=\"https://matrix.to/#/@alice:example.org\">@alice:example.org</a>\ |
| 1702 | <br>\ |
| 1703 | Previous message\ |
| 1704 | </blockquote>\ |
| 1705 | </mx-reply>\ |
| 1706 | <p>new message</p>"; |
| 1707 | let content = TextMessageEventContent::html("fallback", body); |
| 1708 | let rendered = RenderedEvent { |
| 1709 | message_timestamp: 0, |
| 1710 | prefix: "bob\t".to_owned(), |
| 1711 | content: content.render(&()), |
| 1712 | } |
| 1713 | .add_reply_context(&event_id, None); |
| 1714 | |
| 1715 | assert_eq!( |
| 1716 | "Reply to @alice:example.org: Previous message", |
| 1717 | rendered.content.lines[0].message |
| 1718 | ); |
| 1719 | } |
| 1720 | |
| 1443 | 1721 | #[test] |
| 1444 | 1722 | fn formatted_body_plain_text_keeps_block_structure() { |
| 1445 | 1723 | let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\ |