| 1 | use chrono::{DateTime, Utc}; |
| 2 | use serde::Deserialize; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::markdown::escape_html; |
| 6 | use crate::spans::html_to_spans; |
| 7 | use crate::stored::{AttachmentInfo, attachments, push_plain}; |
| 8 | |
| 9 | pub const FORWARDED_CONTENT_TYPE: &str = "forwardedMessageReference"; |
| 10 | pub(crate) const FORWARD_ITEMTYPE: &str = "schema.skype.com/Forward"; |
| 11 | |
| 12 | /// The original message carried by a Graph `forwardedMessageReference` attachment. |
| 13 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 14 | pub struct ForwardInfo { |
| 15 | pub attachment_id: Option<String>, |
| 16 | pub message_id: String, |
| 17 | pub conversation_id: Option<String>, |
| 18 | pub sender_id: Option<String>, |
| 19 | pub sender_name: Option<String>, |
| 20 | pub sent_at: Option<DateTime<Utc>>, |
| 21 | pub content_html: String, |
| 22 | } |
| 23 | |
| 24 | #[derive(Deserialize)] |
| 25 | #[serde(rename_all = "camelCase")] |
| 26 | struct ForwardContent { |
| 27 | original_message_id: Option<String>, |
| 28 | #[serde(default)] |
| 29 | original_message_content: String, |
| 30 | original_conversation_id: Option<String>, |
| 31 | original_sent_date_time: Option<DateTime<Utc>>, |
| 32 | original_message_sender: Option<graph::Sender>, |
| 33 | } |
| 34 | |
| 35 | impl ForwardInfo { |
| 36 | pub fn from_attachment(attachment: &AttachmentInfo) -> Option<ForwardInfo> { |
| 37 | if attachment.content_type.as_deref() != Some(FORWARDED_CONTENT_TYPE) { |
| 38 | return None; |
| 39 | } |
| 40 | let content: ForwardContent = serde_json::from_str(attachment.content.as_deref()?).ok()?; |
| 41 | let sender = content.original_message_sender.as_ref(); |
| 42 | Some(ForwardInfo { |
| 43 | message_id: content |
| 44 | .original_message_id |
| 45 | .or_else(|| attachment.id.clone())?, |
| 46 | attachment_id: attachment.id.clone(), |
| 47 | conversation_id: content.original_conversation_id, |
| 48 | sender_id: sender |
| 49 | .and_then(|sender| sender.user_id()) |
| 50 | .map(str::to_owned), |
| 51 | sender_name: sender |
| 52 | .and_then(|sender| sender.display_name()) |
| 53 | .map(str::to_owned), |
| 54 | sent_at: content.original_sent_date_time, |
| 55 | content_html: content.original_message_content, |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | pub fn text(&self) -> String { |
| 60 | let mut text = String::new(); |
| 61 | push_plain(&mut text, &html_to_spans(&self.content_html)); |
| 62 | text.trim().to_owned() |
| 63 | } |
| 64 | |
| 65 | pub fn html(&self, sender_name: Option<&str>) -> String { |
| 66 | let sender = sender_name |
| 67 | .or(self.sender_name.as_deref()) |
| 68 | .map(|name| format!("<strong>{}</strong>", escape_html(name))) |
| 69 | .unwrap_or_default(); |
| 70 | let time = self |
| 71 | .sent_at |
| 72 | .map(|time| { |
| 73 | let local = time.with_timezone(&chrono::Local); |
| 74 | format!(" <span>{}</span>", local.format("%d/%m/%Y %H:%M")) |
| 75 | }) |
| 76 | .unwrap_or_default(); |
| 77 | format!( |
| 78 | "<blockquote itemscope=\"\" itemtype=\"http://{FORWARD_ITEMTYPE}\" itemid=\"{}\">{sender}{time}{}</blockquote>", |
| 79 | escape_html(&self.message_id), |
| 80 | self.content_html, |
| 81 | ) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub fn forwards(record: &MessageRecord) -> Vec<ForwardInfo> { |
| 86 | if record.deleted { |
| 87 | return Vec::new(); |
| 88 | } |
| 89 | attachments(record) |
| 90 | .iter() |
| 91 | .filter_map(ForwardInfo::from_attachment) |
| 92 | .collect() |
| 93 | } |
| 94 | |
| 95 | pub fn forwarded_texts(record: &MessageRecord) -> Vec<String> { |
| 96 | forwards(record) |
| 97 | .iter() |
| 98 | .map(ForwardInfo::text) |
| 99 | .filter(|text| !text.is_empty()) |
| 100 | .collect() |
| 101 | } |
| 102 | |
| 103 | pub fn is_forwarded(record: &MessageRecord) -> bool { |
| 104 | !record.deleted && (record.body_html.contains(FORWARD_ITEMTYPE) || !forwards(record).is_empty()) |
| 105 | } |
| 106 | |
| 107 | /// `html` with each forward's `<attachment id>` tag replaced by its forward blockquote; a forward without a tag goes last. |
| 108 | pub(crate) fn inline_forwards( |
| 109 | record: &MessageRecord, |
| 110 | mut html: String, |
| 111 | sender_name: &dyn Fn(&str) -> Option<String>, |
| 112 | ) -> String { |
| 113 | for forward in forwards(record) { |
| 114 | let name = forward.sender_id.as_deref().and_then(sender_name); |
| 115 | let block = forward.html(name.as_deref()); |
| 116 | let tag = forward |
| 117 | .attachment_id |
| 118 | .as_deref() |
| 119 | .map(|id| format!("<attachment id=\"{id}\"></attachment>")) |
| 120 | .filter(|tag| html.contains(tag)); |
| 121 | match tag { |
| 122 | Some(tag) => html = html.replacen(&tag, &block, 1), |
| 123 | None => html.push_str(&block), |
| 124 | } |
| 125 | } |
| 126 | html |
| 127 | } |
| 128 | |
| 129 | #[cfg(test)] |
| 130 | mod tests { |
| 131 | use super::*; |
| 132 | use crate::mapping::message_record; |
| 133 | use crate::spans::Span; |
| 134 | use crate::stored::{copy_text, edit_preserved, linked_message_spans_with, readable_text}; |
| 135 | |
| 136 | const CHAT_ID: &str = "19:11111111-1111-4111-8111-111111111111_22222222-2222-4222-8222-222222222222@unq.gbl.spaces"; |
| 137 | const ORIGINAL_SENDER_ID: &str = "33333333-3333-4333-8333-333333333333"; |
| 138 | const ORIGINAL_TEXT: &str = "Keep it short and make sure everyone gets the key part"; |
| 139 | |
| 140 | fn forwarded_record() -> MessageRecord { |
| 141 | let message: graph::Message = |
| 142 | serde_json::from_str(include_str!("../tests/fixtures/forwarded_message.json")).unwrap(); |
| 143 | message_record(CHAT_ID, &message).unwrap() |
| 144 | } |
| 145 | |
| 146 | fn plain(spans: &[Span]) -> String { |
| 147 | let mut text = String::new(); |
| 148 | push_plain(&mut text, spans); |
| 149 | text |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn a_graph_forward_keeps_the_original_message() { |
| 154 | let record = forwarded_record(); |
| 155 | assert!(is_forwarded(&record)); |
| 156 | let found = forwards(&record); |
| 157 | assert_eq!(found.len(), 1); |
| 158 | let forward = &found[0]; |
| 159 | assert_eq!(forward.message_id, "1792149081221"); |
| 160 | assert_eq!(forward.attachment_id.as_deref(), Some("1792149081221")); |
| 161 | assert_eq!(forward.sender_id.as_deref(), Some(ORIGINAL_SENDER_ID)); |
| 162 | assert_eq!(forward.sender_name, None); |
| 163 | assert_eq!( |
| 164 | forward.conversation_id.as_deref(), |
| 165 | Some("19:5555555555554555a5555555555555aa@thread.v2") |
| 166 | ); |
| 167 | assert_eq!( |
| 168 | forward.sent_at, |
| 169 | Some("2026-10-16T11:11:21.221Z".parse().unwrap()) |
| 170 | ); |
| 171 | assert!(forward.text().starts_with(ORIGINAL_TEXT)); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn the_forward_replaces_its_attachment_tag_with_a_quote_naming_the_original_sender() { |
| 176 | let record = forwarded_record(); |
| 177 | let spans = linked_message_spans_with(&record, &|_| None, &|user_id| { |
| 178 | (user_id == ORIGINAL_SENDER_ID).then(|| "Ada Original".to_owned()) |
| 179 | }); |
| 180 | let [Span::BlockQuote(children)] = spans.as_slice() else { |
| 181 | panic!("expected one forward blockquote, got {spans:?}"); |
| 182 | }; |
| 183 | let text = plain(children); |
| 184 | assert!(text.starts_with("Ada Original"), "{text}"); |
| 185 | assert!(text.contains(ORIGINAL_TEXT), "{text}"); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn the_forwarded_text_is_copied_but_not_part_of_the_own_text() { |
| 190 | let record = forwarded_record(); |
| 191 | assert_eq!(readable_text(&record), ""); |
| 192 | assert!(copy_text(&record).starts_with(ORIGINAL_TEXT)); |
| 193 | assert_eq!(forwarded_texts(&record).len(), 1); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn an_edit_keeps_the_forward_attachment() { |
| 198 | let preserved = edit_preserved(&forwarded_record()); |
| 199 | assert_eq!(preserved.kept.len(), 1); |
| 200 | assert_eq!( |
| 201 | preserved.kept[0].content_type.as_deref(), |
| 202 | Some(FORWARDED_CONTENT_TYPE) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn a_deleted_forward_is_not_forwarded() { |
| 208 | let mut record = forwarded_record(); |
| 209 | record.deleted = true; |
| 210 | assert!(!is_forwarded(&record)); |
| 211 | assert!(forwards(&record).is_empty()); |
| 212 | } |
| 213 | } |