From a875f39648cb34b6233260af1478878a92a55835 Mon Sep 17 00:00:00 2001 From: ftilde Date: Sat, 11 Sep 2021 23:31:15 +0200 Subject: [PATCH] events: Fix quoting of multiline fallback messages Previously, a '> ' was prepended only to the first line of the quoted message. --- crates/ruma-events/src/room/message.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/ruma-events/src/room/message.rs b/crates/ruma-events/src/room/message.rs index 3f3484ec..81f42813 100644 --- a/crates/ruma-events/src/room/message.rs +++ b/crates/ruma-events/src/room/message.rs @@ -978,6 +978,7 @@ fn get_plain_quote_fallback(original_message: &MessageEvent) -> String { format!("> <{:?}> {}", original_message.sender, content.body) } } + .replace('\n', "\n> ") } #[allow(clippy::nonstandard_macro_braces)] @@ -1190,11 +1191,13 @@ fn formatted_or_plain_body<'a>(formatted: &'a Option, body: &'a s #[cfg(test)] mod tests { + use std::convert::TryFrom; + use matches::assert_matches; - use ruma_identifiers::event_id; + use ruma_identifiers::{event_id, EventId, RoomId, UserId}; use serde_json::{from_value as from_json_value, json}; - use super::{InReplyTo, MessageEventContent, MessageType, Relation}; + use super::{InReplyTo, MessageEvent, MessageEventContent, MessageType, Relation}; #[test] fn deserialize_reply() { @@ -1218,4 +1221,20 @@ mod tests { } if event_id == ev_id ); } + + #[test] + fn plain_quote_fallback_multiline() { + let sender = UserId::try_from("@alice:example.com").unwrap(); + assert_eq!( + super::get_plain_quote_fallback(&MessageEvent { + content: MessageEventContent::text_plain("multi\nline"), + event_id: EventId::new(sender.server_name()), + sender, + origin_server_ts: ruma_common::MilliSecondsSinceUnixEpoch::now(), + room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), + unsigned: crate::Unsigned::new(), + }), + "> <@alice:example.com> multi\n> line" + ); + } }