| @@ -56,6 +56,12 @@ pub struct RenderedEvent { |
| 56 | 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 | 65 | impl RenderedEvent { |
| 60 | 66 | const MSG_TAGS: &'static [&'static str] = &["notify_message"]; |
| 61 | 67 | const SELF_TAGS: &'static [&'static str] = |
| @@ -81,6 +87,7 @@ impl RenderedEvent { |
| 81 | 87 | mut self, |
| 82 | 88 | event_id: &EventId, |
| 83 | 89 | sender: Option<&str>, |
| 90 | context: ReplyContext, |
| 84 | 91 | ) -> Self { |
| 85 | 92 | let reply_fallback = self.content.reply_fallback.take(); |
| 86 | 93 | let reply_sender = sender |
| @@ -99,6 +106,15 @@ impl RenderedEvent { |
| 99 | 106 | .unwrap_or_default(); |
| 100 | 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 | 118 | let mut context_lines = match reply_fallback { |
| 103 | 119 | Some(fallback) => { |
| 104 | 120 | let mut lines = vec![RenderedLine { |
| @@ -1631,18 +1647,21 @@ mod tests { |
| 1631 | 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()); |
| 1637 | | assert_eq!("Reply to Alice", rendered.content.lines[0].message); |
| 1656 | assert_eq!(1, rendered.content.lines.len()); |
| 1657 | assert_eq!("> Alice: reply body", rendered.content.lines[0].message); |
| 1638 | 1658 | assert!(rendered.content.lines[0] |
| 1639 | 1659 | .tags |
| 1640 | 1660 | .contains(&"matrix_reply".to_owned())); |
| 1641 | | assert_eq!("reply body", rendered.content.lines[1].message); |
| 1642 | 1661 | } |
| 1643 | 1662 | |
| 1644 | 1663 | #[test] |
| 1645 | | fn reply_context_falls_back_to_event_id() { |
| 1664 | fn inline_reply_context_falls_back_to_event_id() { |
| 1646 | 1665 | let event_id = |
| 1647 | 1666 | matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); |
| 1648 | 1667 | let rendered = RenderedEvent { |
| @@ -1653,16 +1672,15 @@ mod tests { |
| 1653 | 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 | 1677 | assert_eq!( |
| 1659 | | "Reply to $replyevent:example.org", |
| 1678 | "> $replyevent:example.org: reply body", |
| 1660 | 1679 | rendered.content.lines[0].message |
| 1661 | 1680 | ); |
| 1662 | 1681 | assert!(rendered.content.lines[0] |
| 1663 | 1682 | .tags |
| 1664 | 1683 | .contains(&"matrix_reply".to_owned())); |
| 1665 | | assert_eq!("reply body", rendered.content.lines[1].message); |
| 1666 | 1684 | } |
| 1667 | 1685 | |
| 1668 | 1686 | #[test] |
| @@ -1685,7 +1703,11 @@ mod tests { |
| 1685 | 1703 | prefix: "bob\t".to_owned(), |
| 1686 | 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 | 1712 | assert_eq!(3, rendered.content.lines.len()); |
| 1691 | 1713 | assert_eq!("Reply to Alice:", rendered.content.lines[0].message); |
| @@ -1716,7 +1738,7 @@ mod tests { |
| 1716 | 1738 | prefix: "bob\t".to_owned(), |
| 1717 | 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 | 1743 | assert_eq!( |
| 1722 | 1744 | "Reply to @alice:example.org:", |
| @@ -1745,7 +1767,11 @@ mod tests { |
| 1745 | 1767 | prefix: "bob\t".to_owned(), |
| 1746 | 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 | 1776 | let messages = rendered |
| 1751 | 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 | 1822 | #[test] |
| 1764 | 1823 | fn formatted_body_plain_text_keeps_block_structure() { |
| 1765 | 1824 | let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\ |