From 393be9be5a9ef6f36ab37235fb9ad6a3305e9fef Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Thu, 3 Oct 2024 16:02:06 +0300 Subject: [PATCH] Remove strip_rich_reply_fallback_text in favor of remove_plain_reply_fallback from ruma events No need to reinvent the wheel. --- src/conversation/matrix/utils/mod.rs | 31 +++----------------------- src/conversation/matrix/utils/tests.rs | 7 ------ 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/conversation/matrix/utils/mod.rs b/src/conversation/matrix/utils/mod.rs index 4c845a5..8496690 100644 --- a/src/conversation/matrix/utils/mod.rs +++ b/src/conversation/matrix/utils/mod.rs @@ -9,7 +9,8 @@ use mxlink::matrix_sdk::{ ruma::events::{ relation::Thread, room::message::{ - MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent, + sanitize::remove_plain_reply_fallback, MessageType, OriginalSyncRoomMessageEvent, + Relation, RoomMessageEventContent, }, AnyMessageLikeEvent, AnyMessageLikeEventContent, AnyTimelineEvent, MessageLikeEvent, }, @@ -229,7 +230,7 @@ pub fn convert_matrix_native_event_to_matrix_message( let text = if is_reply { // For regular replies, we need to strip the fallback-for-rich replies part. // See: https://spec.matrix.org/v1.11/client-server-api/#fallbacks-for-rich-replies - strip_rich_reply_fallback_text(&text) + remove_plain_reply_fallback(&text).to_owned() } else { text }; @@ -453,32 +454,6 @@ fn is_event_mentioning_bot( } } -/// Strips the rich reply fallback text from the given text. -/// See: https://spec.matrix.org/v1.11/client-server-api/#fallbacks-for-rich-replies -/// -/// Example: -/// ```rust,ignore -/// let text = "> <@admin:example.com> What's the difference between Matrix and XMPP?\n\nAnswer me"; -/// let stripped_text = strip_rich_reply_fallback_text(text); -/// assert_eq!(stripped_text, "Answer me"); -/// ``` -fn strip_rich_reply_fallback_text(text: &str) -> String { - let lines = text.lines(); - let mut stripped_lines = Vec::new(); - let mut encountered_non_prefix = false; - - for line in lines { - if !encountered_non_prefix && line.starts_with("> ") { - continue; - } else { - encountered_non_prefix = true; - stripped_lines.push(line); - } - } - - stripped_lines.join("\n").trim().to_owned() -} - fn timeline_event_to_detailed_message_payload( timeline_event_id: &OwnedEventId, timeline_event: TimelineEvent, diff --git a/src/conversation/matrix/utils/tests.rs b/src/conversation/matrix/utils/tests.rs index 6ed1e27..f479cf2 100644 --- a/src/conversation/matrix/utils/tests.rs +++ b/src/conversation/matrix/utils/tests.rs @@ -296,13 +296,6 @@ async fn process_matrix_messages() { } } -#[test] -fn strip_rich_reply_fallback_text() { - let text = "> <@admin:example.com> What's the difference between Matrix and XMPP?\n\nAnswer me"; - let stripped_text = super::strip_rich_reply_fallback_text(text); - assert_eq!(stripped_text, "Answer me"); -} - #[test] fn create_list_of_bot_user_prefixes_to_strip() { let bot_user_id =