| @@ -56,6 +56,12 @@ pub struct RenderedEvent { |
| 56 | pub content: RenderedContent, |
56 | pub content: RenderedContent, |
| 57 | } |
57 | } |
| 58 | |
58 | |
| |
59 | #[derive(Debug, Clone, Copy, Eq, PartialEq)] |
| |
60 | pub enum ReplyContext { |
| |
61 | Inline, |
| |
62 | Full, |
| |
63 | } |
| |
64 | |
| 59 | impl RenderedEvent { |
65 | impl RenderedEvent { |
| 60 | const MSG_TAGS: &'static [&'static str] = &["notify_message"]; |
66 | const MSG_TAGS: &'static [&'static str] = &["notify_message"]; |
| 61 | const SELF_TAGS: &'static [&'static str] = |
67 | const SELF_TAGS: &'static [&'static str] = |
| @@ -81,6 +87,7 @@ impl RenderedEvent { |
| 81 | mut self, |
87 | mut self, |
| 82 | event_id: &EventId, |
88 | event_id: &EventId, |
| 83 | sender: Option<&str>, |
89 | sender: Option<&str>, |
| |
90 | context: ReplyContext, |
| 84 | ) -> Self { |
91 | ) -> Self { |
| 85 | let reply_fallback = self.content.reply_fallback.take(); |
92 | let reply_fallback = self.content.reply_fallback.take(); |
| 86 | let reply_sender = sender |
93 | let reply_sender = sender |
| @@ -99,6 +106,15 @@ impl RenderedEvent { |
| 99 | .unwrap_or_default(); |
106 | .unwrap_or_default(); |
| 100 | tags.push("matrix_reply".to_owned()); |
107 | tags.push("matrix_reply".to_owned()); |
| 101 | |
108 | |
| |
109 | if context == ReplyContext::Inline { |
| |
110 | if let Some(line) = self.content.lines.first_mut() { |
| |
111 | line.tags = tags; |
| |
112 | line.message = format!("> {}: {}", reply_sender, line.message); |
| |
113 | } |
| |
114 | |
| |
115 | return self; |
| |
116 | } |
| |
117 | |
| 102 | let mut context_lines = match reply_fallback { |
118 | let mut context_lines = match reply_fallback { |
| 103 | Some(fallback) => { |
119 | Some(fallback) => { |
| 104 | let mut lines = vec![RenderedLine { |
120 | let mut lines = vec![RenderedLine { |
| @@ -1631,18 +1647,21 @@ mod tests { |
| 1631 | message: "reply body".to_owned(), |
1647 | message: "reply body".to_owned(), |
| 1632 | }]), |
1648 | }]), |
| 1633 | } |
1649 | } |
| 1634 | .add_reply_context(&event_id, Some("Alice")); |
1650 | .add_reply_context( |
| |
1651 | &event_id, |
| |
1652 | Some("Alice"), |
| |
1653 | ReplyContext::Inline, |
| |
1654 | ); |
| 1635 | |
1655 | |
| 1636 | assert_eq!(2, rendered.content.lines.len()); |
1656 | assert_eq!(1, rendered.content.lines.len()); |
| 1637 | assert_eq!("Reply to Alice", rendered.content.lines[0].message); |
1657 | assert_eq!("> Alice: reply body", rendered.content.lines[0].message); |
| 1638 | assert!(rendered.content.lines[0] |
1658 | assert!(rendered.content.lines[0] |
| 1639 | .tags |
1659 | .tags |
| 1640 | .contains(&"matrix_reply".to_owned())); |
1660 | .contains(&"matrix_reply".to_owned())); |
| 1641 | assert_eq!("reply body", rendered.content.lines[1].message); |
| |
| 1642 | } |
1661 | } |
| 1643 | |
1662 | |
| 1644 | #[test] |
1663 | #[test] |
| 1645 | fn reply_context_falls_back_to_event_id() { |
1664 | fn inline_reply_context_falls_back_to_event_id() { |
| 1646 | let event_id = |
1665 | let event_id = |
| 1647 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
1666 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| 1648 | let rendered = RenderedEvent { |
1667 | let rendered = RenderedEvent { |
| @@ -1653,16 +1672,15 @@ mod tests { |
| 1653 | message: "reply body".to_owned(), |
1672 | message: "reply body".to_owned(), |
| 1654 | }]), |
1673 | }]), |
| 1655 | } |
1674 | } |
| 1656 | .add_reply_context(&event_id, None); |
1675 | .add_reply_context(&event_id, None, ReplyContext::Inline); |
| 1657 | |
1676 | |
| 1658 | assert_eq!( |
1677 | assert_eq!( |
| 1659 | "Reply to $replyevent:example.org", |
1678 | "> $replyevent:example.org: reply body", |
| 1660 | rendered.content.lines[0].message |
1679 | rendered.content.lines[0].message |
| 1661 | ); |
1680 | ); |
| 1662 | assert!(rendered.content.lines[0] |
1681 | assert!(rendered.content.lines[0] |
| 1663 | .tags |
1682 | .tags |
| 1664 | .contains(&"matrix_reply".to_owned())); |
1683 | .contains(&"matrix_reply".to_owned())); |
| 1665 | assert_eq!("reply body", rendered.content.lines[1].message); |
| |
| 1666 | } |
1684 | } |
| 1667 | |
1685 | |
| 1668 | #[test] |
1686 | #[test] |
| @@ -1685,7 +1703,11 @@ mod tests { |
| 1685 | prefix: "bob\t".to_owned(), |
1703 | prefix: "bob\t".to_owned(), |
| 1686 | content: content.render(&()), |
1704 | content: content.render(&()), |
| 1687 | } |
1705 | } |
| 1688 | .add_reply_context(&event_id, Some("Alice")); |
1706 | .add_reply_context( |
| |
1707 | &event_id, |
| |
1708 | Some("Alice"), |
| |
1709 | ReplyContext::Full, |
| |
1710 | ); |
| 1689 | |
1711 | |
| 1690 | assert_eq!(3, rendered.content.lines.len()); |
1712 | assert_eq!(3, rendered.content.lines.len()); |
| 1691 | assert_eq!("Reply to Alice:", rendered.content.lines[0].message); |
1713 | assert_eq!("Reply to Alice:", rendered.content.lines[0].message); |
| @@ -1716,7 +1738,7 @@ mod tests { |
| 1716 | prefix: "bob\t".to_owned(), |
1738 | prefix: "bob\t".to_owned(), |
| 1717 | content: content.render(&()), |
1739 | content: content.render(&()), |
| 1718 | } |
1740 | } |
| 1719 | .add_reply_context(&event_id, None); |
1741 | .add_reply_context(&event_id, None, ReplyContext::Full); |
| 1720 | |
1742 | |
| 1721 | assert_eq!( |
1743 | assert_eq!( |
| 1722 | "Reply to @alice:example.org:", |
1744 | "Reply to @alice:example.org:", |
| @@ -1745,7 +1767,11 @@ mod tests { |
| 1745 | prefix: "bob\t".to_owned(), |
1767 | prefix: "bob\t".to_owned(), |
| 1746 | content: content.render(&()), |
1768 | content: content.render(&()), |
| 1747 | } |
1769 | } |
| 1748 | .add_reply_context(&event_id, Some("Alice")); |
1770 | .add_reply_context( |
| |
1771 | &event_id, |
| |
1772 | Some("Alice"), |
| |
1773 | ReplyContext::Full, |
| |
1774 | ); |
| 1749 | |
1775 | |
| 1750 | let messages = rendered |
1776 | let messages = rendered |
| 1751 | .content |
1777 | .content |
| @@ -1760,6 +1786,39 @@ mod tests { |
| 1760 | ); |
1786 | ); |
| 1761 | } |
1787 | } |
| 1762 | |
1788 | |
| |
1789 | #[test] |
| |
1790 | fn inline_reply_context_keeps_reply_on_one_line_with_fallback() { |
| |
1791 | let event_id = |
| |
1792 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| |
1793 | let body = "\ |
| |
1794 | <mx-reply>\ |
| |
1795 | <blockquote>\ |
| |
1796 | <a href=\"https://matrix.to/#/!room:example.org/$replyevent:example.org\">In reply to</a> \ |
| |
1797 | <a href=\"https://matrix.to/#/@alice:example.org\">@alice:example.org</a>\ |
| |
1798 | <br>\ |
| |
1799 | Previous message\ |
| |
1800 | </blockquote>\ |
| |
1801 | </mx-reply>\ |
| |
1802 | <p>new message</p>"; |
| |
1803 | let content = TextMessageEventContent::html("fallback", body); |
| |
1804 | let rendered = RenderedEvent { |
| |
1805 | message_timestamp: 0, |
| |
1806 | prefix: "bob\t".to_owned(), |
| |
1807 | content: content.render(&()), |
| |
1808 | } |
| |
1809 | .add_reply_context( |
| |
1810 | &event_id, |
| |
1811 | Some("Alice"), |
| |
1812 | ReplyContext::Inline, |
| |
1813 | ); |
| |
1814 | |
| |
1815 | assert_eq!(1, rendered.content.lines.len()); |
| |
1816 | assert_eq!("> Alice: new message", rendered.content.lines[0].message); |
| |
1817 | assert!(rendered.content.lines[0] |
| |
1818 | .tags |
| |
1819 | .contains(&"matrix_reply".to_owned())); |
| |
1820 | } |
| |
1821 | |
| 1763 | #[test] |
1822 | #[test] |
| 1764 | fn formatted_body_plain_text_keeps_block_structure() { |
1823 | fn formatted_body_plain_text_keeps_block_structure() { |
| 1765 | let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\ |
1824 | let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\ |