From 9123e6fbe5cdc2d2ae16579d989d45398232f74c Mon Sep 17 00:00:00 2001 From: sinavir Date: Sun, 22 Sep 2024 23:51:52 +0200 Subject: [PATCH] fix(transformers): Add signal mention support --- src/transform.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/transform.rs b/src/transform.rs index ab92a27..c329a2d 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -10,6 +10,7 @@ struct SignalMessageBuilder<'a> { from: Option, message: Option, attachments_urls: Vec, + mentions: Vec<(usize, usize, String)>, // TODO Add type target: Option<&'a String>, } @@ -22,16 +23,28 @@ impl<'b> SignalMessageBuilder<'b> { self.from = Some(from); } - pub fn attach(&mut self, attach: Vec) { - self.attachments_urls.extend_from_slice(&attach); + pub fn attach(&mut self, attach: &Vec) { + self.attachments_urls.extend_from_slice(attach); + } + + pub fn mention(&mut self, mentions: &Vec<(usize, usize, String)>) { + self.mentions.extend_from_slice(mentions); } pub fn target(&mut self, channel: Option<&'b String>) { self.target = channel; } - pub fn build(self) -> Option<(&'b String, String)> + pub fn build(mut self) -> Option<(&'b String, String)> { + self.mentions.sort_by(|(pos1, _, _), (pos2, _, _)| pos1.cmp(pos2)); + self.message.as_mut().map(|message| { + let mut offset: usize = 0; + for (pos, len, name) in self.mentions.into_iter() { + message.replace_range((pos + offset)..(pos + offset + len), &name); + offset += len; + } + }); let attachments_text = self.attachments_urls.join(", "); (match (&self.message, attachments_text.len()) { (None, 0) => None, @@ -107,7 +120,7 @@ impl<'a> Bridge<'a> { log::warn!("droped attachment: {a}"); } } - result.attach(attachments_urls); + result.attach(&attachments_urls); }; if let Some(Value::String(text)) = message.get("message") { result.message(text.clone());