Commit 13a2514adc
Unregistered key
Layout: unified · split
src/message/mod.rs +46 −8
| @@ -19,6 +19,7 @@ use matrix_sdk::ruma::events::room::encrypted::{ | |||
| 19 | RoomEncryptedEvent, | 19 | RoomEncryptedEvent, |
| 20 | }; | 20 | }; |
| 21 | use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation; | 21 | use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation; |
| 22 | use matrix_sdk::ruma::events::room::message::sanitize::remove_plain_reply_fallback; | ||
| 22 | use matrix_sdk::ruma::events::room::message::{ | 23 | use matrix_sdk::ruma::events::room::message::{ |
| 23 | FormattedBody, | 24 | FormattedBody, |
| 24 | MessageFormat, | 25 | MessageFormat, |
| @@ -96,7 +97,9 @@ impl Messages { | |||
| 96 | pub fn insert_message(&mut self, key: MessageKey, msg: impl Into<Message>) { | 97 | pub fn insert_message(&mut self, key: MessageKey, msg: impl Into<Message>) { |
| 97 | let mut msg = msg.into(); | 98 | let mut msg = msg.into(); |
| 98 | if let MessageEvent::Original(ev, edits) = &mut msg.event { | 99 | if let MessageEvent::Original(ev, edits) = &mut msg.event { |
| 99 | strip_reply_fallback(&mut ev.content.msgtype); | 100 | if has_reply_fallback(ev.content.relates_to.as_ref()) { |
| 101 | strip_reply_fallback(&mut ev.content.msgtype); | ||
| 102 | } | ||
| 100 | for edit in edits.values_mut() { | 103 | for edit in edits.values_mut() { |
| 101 | strip_reply_fallback(&mut edit.msgtype); | 104 | strip_reply_fallback(&mut edit.msgtype); |
| 102 | } | 105 | } |
| @@ -372,18 +375,19 @@ fn redaction_reason_event(ev: SyncRoomRedactionEvent) -> Option<String> { | |||
| 372 | ev.content.reason | 375 | ev.content.reason |
| 373 | } | 376 | } |
| 374 | 377 | ||
| 378 | fn has_reply_fallback(relation: Option<&Relation<RoomMessageEventContentWithoutRelation>>) -> bool { | ||
| 379 | matches!( | ||
| 380 | relation, | ||
| 381 | Some(Relation::Reply(_) | Relation::Thread(Thread { in_reply_to: Some(_), .. })) | ||
| 382 | ) | ||
| 383 | } | ||
| 384 | |||
| 375 | pub fn strip_reply_fallback(msgtype: &mut MessageType) { | 385 | pub fn strip_reply_fallback(msgtype: &mut MessageType) { |
| 376 | let MessageType::Text(content) = msgtype else { | 386 | let MessageType::Text(content) = msgtype else { |
| 377 | return; | 387 | return; |
| 378 | }; | 388 | }; |
| 379 | 389 | ||
| 380 | if !content.body.starts_with('>') { | 390 | content.body = remove_plain_reply_fallback(&content.body).to_owned(); |
| 381 | return; | ||
| 382 | } | ||
| 383 | |||
| 384 | let new_body = content.body.lines().skip_while(|line| line.starts_with('>')).collect(); | ||
| 385 | |||
| 386 | content.body = new_body; | ||
| 387 | } | 391 | } |
| 388 | 392 | ||
| 389 | fn content_html(msgtype: &MessageType) -> Option<StyleTree> { | 393 | fn content_html(msgtype: &MessageType) -> Option<StyleTree> { |
| @@ -1690,6 +1694,7 @@ impl Display for Message { | |||
| 1690 | pub mod tests { | 1694 | pub mod tests { |
| 1691 | use super::*; | 1695 | use super::*; |
| 1692 | 1696 | ||
| 1697 | use matrix_sdk::ruma::events::relation::Reply; | ||
| 1693 | use matrix_sdk::ruma::events::room::ImageInfo; | 1698 | use matrix_sdk::ruma::events::room::ImageInfo; |
| 1694 | use matrix_sdk::ruma::events::room::message::{ | 1699 | use matrix_sdk::ruma::events::room::message::{ |
| 1695 | AudioInfo, | 1700 | AudioInfo, |
| @@ -1697,6 +1702,7 @@ pub mod tests { | |||
| 1697 | FileInfo, | 1702 | FileInfo, |
| 1698 | FileMessageEventContent, | 1703 | FileMessageEventContent, |
| 1699 | ImageMessageEventContent, | 1704 | ImageMessageEventContent, |
| 1705 | TextMessageEventContent, | ||
| 1700 | VideoInfo, | 1706 | VideoInfo, |
| 1701 | VideoMessageEventContent, | 1707 | VideoMessageEventContent, |
| 1702 | }; | 1708 | }; |
| @@ -1705,6 +1711,38 @@ pub mod tests { | |||
| 1705 | use crate::config::time_gutter_width; | 1711 | use crate::config::time_gutter_width; |
| 1706 | use crate::tests::*; | 1712 | use crate::tests::*; |
| 1707 | 1713 | ||
| 1714 | #[test] | ||
| 1715 | fn test_has_reply_fallback() { | ||
| 1716 | let root = OwnedEventId::try_from("$root:example.org").unwrap(); | ||
| 1717 | let target = OwnedEventId::try_from("$target:example.org").unwrap(); | ||
| 1718 | |||
| 1719 | let reply: Relation<RoomMessageEventContentWithoutRelation> = | ||
| 1720 | Relation::Reply(Reply::with_event_id(target.clone())); | ||
| 1721 | assert!(has_reply_fallback(Some(&reply))); | ||
| 1722 | |||
| 1723 | let thread_reply: Relation<RoomMessageEventContentWithoutRelation> = | ||
| 1724 | Relation::Thread(Thread::reply(root.clone(), target.clone())); | ||
| 1725 | assert!(has_reply_fallback(Some(&thread_reply))); | ||
| 1726 | |||
| 1727 | let thread_fallback: Relation<RoomMessageEventContentWithoutRelation> = | ||
| 1728 | Relation::Thread(Thread::plain(root.clone(), target)); | ||
| 1729 | assert!(has_reply_fallback(Some(&thread_fallback))); | ||
| 1730 | |||
| 1731 | let thread_without_fallback: Relation<RoomMessageEventContentWithoutRelation> = | ||
| 1732 | Relation::Thread(Thread::without_fallback(root)); | ||
| 1733 | assert!(!has_reply_fallback(Some(&thread_without_fallback))); | ||
| 1734 | assert!(!has_reply_fallback(None)); | ||
| 1735 | } | ||
| 1736 | |||
| 1737 | #[test] | ||
| 1738 | fn test_strip_reply_fallback() { | ||
| 1739 | let body = "> <@user:example.org> quoted text\n> more quoted text\n\nreply"; | ||
| 1740 | let mut msgtype = MessageType::Text(TextMessageEventContent::plain(body)); | ||
| 1741 | |||
| 1742 | strip_reply_fallback(&mut msgtype); | ||
| 1743 | assert_eq!(msgtype.body(), "reply"); | ||
| 1744 | } | ||
| 1745 | |||
| 1708 | #[test] | 1746 | #[test] |
| 1709 | fn test_mc_cmp() { | 1747 | fn test_mc_cmp() { |
| 1710 | let mc1 = MessageCursor::from(MSG1_KEY.clone()); | 1748 | let mc1 = MessageCursor::from(MSG1_KEY.clone()); |