| 1 | use mcp::{ |
| 2 | FileInfo, ForwardedInfo, ImageInfo, ImageKind, LinkInfo, MentionInfo, MessageExtras, QuoteInfo, ReactionInfo, |
| 3 | }; |
| 4 | use store::MessageRecord; |
| 5 | use teams_core::MentionTarget; |
| 6 | |
| 7 | pub fn message_extras(record: &MessageRecord, my_user_id: Option<&str>) -> MessageExtras { |
| 8 | MessageExtras { |
| 9 | text: teams_core::readable_text(record), |
| 10 | images: teams_core::images(record) |
| 11 | .into_iter() |
| 12 | .map(|image| ImageInfo { |
| 13 | kind: image_kind(image.kind()), |
| 14 | image_id: image.id, |
| 15 | alt: image.alt, |
| 16 | }) |
| 17 | .collect(), |
| 18 | reactions: teams_core::reaction_summaries(record, my_user_id) |
| 19 | .into_iter() |
| 20 | .map(|summary| ReactionInfo { |
| 21 | emoji: summary.emoji, |
| 22 | count: summary.count, |
| 23 | mine: summary.mine, |
| 24 | }) |
| 25 | .collect(), |
| 26 | mentions: teams_core::user_mention_inputs(record) |
| 27 | .into_iter() |
| 28 | .map(|input| MentionInfo { |
| 29 | user_id: match input.target { |
| 30 | MentionTarget::User { user_id } => Some(user_id), |
| 31 | _ => None, |
| 32 | }, |
| 33 | name: input.text, |
| 34 | }) |
| 35 | .collect(), |
| 36 | files: teams_core::files(record) |
| 37 | .into_iter() |
| 38 | .map(|file| FileInfo { |
| 39 | name: file.name, |
| 40 | size: file.size, |
| 41 | file_type: Some(file_type(file.kind).to_owned()), |
| 42 | link: file.open_url, |
| 43 | }) |
| 44 | .collect(), |
| 45 | quote: teams_core::message_quote(record).map(|quote| QuoteInfo { |
| 46 | message_id: quote.message_id, |
| 47 | sender: quote.sender_name, |
| 48 | text: quote.preview, |
| 49 | }), |
| 50 | forwarded: teams_core::forwards(record) |
| 51 | .into_iter() |
| 52 | .map(|forward| ForwardedInfo { |
| 53 | text: forward.text(), |
| 54 | message_id: forward.message_id, |
| 55 | conversation_id: forward.conversation_id, |
| 56 | sender: forward.sender_name, |
| 57 | sender_id: forward.sender_id, |
| 58 | time: forward.sent_at.map(|time| time.to_rfc3339()), |
| 59 | }) |
| 60 | .collect(), |
| 61 | cards: teams_core::card_texts(record), |
| 62 | links: teams_core::link_preview(record) |
| 63 | .into_iter() |
| 64 | .map(|preview| LinkInfo { |
| 65 | url: preview.url, |
| 66 | title: preview.title, |
| 67 | }) |
| 68 | .collect(), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn file_type(kind: teams_core::FileKind) -> &'static str { |
| 73 | match kind { |
| 74 | teams_core::FileKind::Word => "word", |
| 75 | teams_core::FileKind::Excel => "excel", |
| 76 | teams_core::FileKind::PowerPoint => "powerpoint", |
| 77 | teams_core::FileKind::Pdf => "pdf", |
| 78 | teams_core::FileKind::Image => "image", |
| 79 | teams_core::FileKind::Archive => "archive", |
| 80 | teams_core::FileKind::Loop => "loop", |
| 81 | teams_core::FileKind::Other => "other", |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub fn image_kind(kind: teams_core::ImageKind) -> ImageKind { |
| 86 | match kind { |
| 87 | teams_core::ImageKind::Inline => ImageKind::Inline, |
| 88 | teams_core::ImageKind::Gif => ImageKind::Gif, |
| 89 | teams_core::ImageKind::Sticker => ImageKind::Sticker, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use chrono::{TimeZone, Utc}; |
| 96 | |
| 97 | use super::*; |
| 98 | |
| 99 | fn record() -> MessageRecord { |
| 100 | MessageRecord { |
| 101 | conversation_id: "19:c@thread.v2".to_owned(), |
| 102 | message_id: "1728".to_owned(), |
| 103 | sender_id: Some("user-ada".to_owned()), |
| 104 | sender_name: Some("Ada Example".to_owned()), |
| 105 | created_at: Utc.with_ymd_and_hms(2026, 10, 6, 9, 0, 0).unwrap(), |
| 106 | body_html: concat!( |
| 107 | r#"<attachment id="Q1"></attachment><p>Hi <at id="0">Bob Sample</at>, see this</p>"#, |
| 108 | r#"<p><img src="https://graph.microsoft.com/v1.0/chats/19:c@thread.v2/messages/1728/hostedContents/aWQ9/$value" alt="a chart" width="300" height="120"></p>"#, |
| 109 | r#"<p><img src="https://media0.giphy.com/media/abc/giphy.gif"></p>"#, |
| 110 | r#"<attachment id="F1"></attachment>"#, |
| 111 | r#"<p><a href="https://news.example.com/story">https://news.example.com/story</a></p>"#, |
| 112 | ) |
| 113 | .to_owned(), |
| 114 | attachments_json: r#"[ |
| 115 | {"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","quote":{"message_id":"1700","preview":"earlier words","sender_name":"Bob Sample"}}, |
| 116 | {"content_type":"reference","name":"Plan.docx","url":"https://example.sharepoint.com/Plan.docx","text":null,"id":"F1"}, |
| 117 | {"content_type":"application/vnd.microsoft.card.adaptive","name":null,"url":null,"text":"fallback","content":"{\"type\":\"AdaptiveCard\",\"version\":\"1.4\",\"body\":[{\"type\":\"TextBlock\",\"text\":\"Build passed\"}]}"} |
| 118 | ]"# |
| 119 | .to_owned(), |
| 120 | reactions_json: r#"[ |
| 121 | {"reaction_type":"like","user_id":"user-me","user_name":"Me"}, |
| 122 | {"reaction_type":"like","user_id":"user-bob","user_name":"Bob Sample"}, |
| 123 | {"reaction_type":"heart","user_id":"user-bob","user_name":"Bob Sample"} |
| 124 | ]"# |
| 125 | .to_owned(), |
| 126 | mentions_json: r#"[{"user_id":"user-bob","name":"Bob","id":0,"target_id":"user-bob"},{"user_id":"user-bob","name":"Sample","id":1,"target_id":"user-bob"}]"#.to_owned(), |
| 127 | links_json: r#"[{"@type":"http://schema.skype.com/HyperLink","itemid":"0","url":"https://news.example.com/story","preview":{"title":"A story","description":"Words"},"previewenabled":true}]"#.to_owned(), |
| 128 | ..MessageRecord::default() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn extras_cover_every_part_of_a_real_shaped_message() { |
| 134 | let extras = message_extras(&record(), Some("user-me")); |
| 135 | assert!( |
| 136 | extras.text.starts_with("Hi @Bob Sample, see this"), |
| 137 | "{}", |
| 138 | extras.text |
| 139 | ); |
| 140 | assert!(!extras.text.contains("earlier words"), "{}", extras.text); |
| 141 | let images: Vec<_> = extras |
| 142 | .images |
| 143 | .iter() |
| 144 | .map(|image| (image.kind, image.alt.as_deref())) |
| 145 | .collect(); |
| 146 | assert_eq!( |
| 147 | images, |
| 148 | [(ImageKind::Inline, Some("a chart")), (ImageKind::Gif, None)] |
| 149 | ); |
| 150 | assert_ne!(extras.images[0].image_id, extras.images[1].image_id); |
| 151 | assert_eq!( |
| 152 | extras.reactions, |
| 153 | [ |
| 154 | ReactionInfo { |
| 155 | emoji: "\u{1F44D}".to_owned(), |
| 156 | count: 2, |
| 157 | mine: true |
| 158 | }, |
| 159 | ReactionInfo { |
| 160 | emoji: "\u{2764}".to_owned(), |
| 161 | count: 1, |
| 162 | mine: false |
| 163 | }, |
| 164 | ] |
| 165 | ); |
| 166 | assert_eq!( |
| 167 | extras.mentions, |
| 168 | [MentionInfo { |
| 169 | name: "Bob Sample".to_owned(), |
| 170 | user_id: Some("user-bob".to_owned()) |
| 171 | }] |
| 172 | ); |
| 173 | assert_eq!(extras.files.len(), 1); |
| 174 | assert_eq!(extras.files[0].name, "Plan.docx"); |
| 175 | assert_eq!(extras.files[0].file_type.as_deref(), Some("word")); |
| 176 | assert_eq!( |
| 177 | extras.quote, |
| 178 | Some(QuoteInfo { |
| 179 | message_id: "1700".to_owned(), |
| 180 | sender: Some("Bob Sample".to_owned()), |
| 181 | text: "earlier words".to_owned(), |
| 182 | }) |
| 183 | ); |
| 184 | assert_eq!(extras.cards, ["Build passed"]); |
| 185 | assert_eq!( |
| 186 | extras.links, |
| 187 | [LinkInfo { |
| 188 | url: "https://news.example.com/story".to_owned(), |
| 189 | title: Some("A story".to_owned()) |
| 190 | }] |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn an_inline_reply_blockquote_becomes_the_quote() { |
| 196 | let stored = MessageRecord { |
| 197 | body_html: r#"<blockquote itemscope="" itemtype="http://schema.skype.com/Reply" itemid="1700"><strong itemprop="mri">Bob Sample</strong><p itemprop="preview">earlier words</p></blockquote><p>answer</p>"#.to_owned(), |
| 198 | ..MessageRecord::default() |
| 199 | }; |
| 200 | let extras = message_extras(&stored, None); |
| 201 | assert_eq!(extras.text, "answer"); |
| 202 | assert_eq!( |
| 203 | extras.quote, |
| 204 | Some(QuoteInfo { |
| 205 | message_id: "1700".to_owned(), |
| 206 | sender: Some("Bob Sample".to_owned()), |
| 207 | text: "earlier words".to_owned(), |
| 208 | }) |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn a_plain_message_has_no_extras() { |
| 214 | let plain = MessageRecord { |
| 215 | body_html: "<p>hello</p>".to_owned(), |
| 216 | attachments_json: "[]".to_owned(), |
| 217 | reactions_json: "[]".to_owned(), |
| 218 | mentions_json: "[]".to_owned(), |
| 219 | links_json: "[]".to_owned(), |
| 220 | ..MessageRecord::default() |
| 221 | }; |
| 222 | let extras = message_extras(&plain, None); |
| 223 | assert_eq!(extras.text, "hello"); |
| 224 | assert_eq!( |
| 225 | MessageExtras { |
| 226 | text: String::new(), |
| 227 | ..extras |
| 228 | }, |
| 229 | MessageExtras::default() |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn a_forward_lists_the_original_message_apart_from_the_text() { |
| 235 | let forwarded = MessageRecord { |
| 236 | body_html: r#"<p>fyi</p><attachment id="9"></attachment>"#.to_owned(), |
| 237 | attachments_json: serde_json::json!([{ |
| 238 | "content_type": "forwardedMessageReference", |
| 239 | "name": null, "url": null, "text": null, "id": "9", |
| 240 | "content": serde_json::json!({ |
| 241 | "originalMessageId": "9", |
| 242 | "originalMessageContent": "<p>original <b>words</b></p>", |
| 243 | "originalConversationId": "19:origin@thread.v2", |
| 244 | "originalSentDateTime": "2026-10-06T08:00:00.000+00:00", |
| 245 | "originalMessageSender": {"user": {"id": "user-cleo", "displayName": null}} |
| 246 | }).to_string() |
| 247 | }]) |
| 248 | .to_string(), |
| 249 | ..record() |
| 250 | }; |
| 251 | let extras = message_extras(&forwarded, None); |
| 252 | assert_eq!(extras.text, "fyi"); |
| 253 | assert_eq!( |
| 254 | extras.forwarded, |
| 255 | [ForwardedInfo { |
| 256 | message_id: "9".to_owned(), |
| 257 | conversation_id: Some("19:origin@thread.v2".to_owned()), |
| 258 | sender: None, |
| 259 | sender_id: Some("user-cleo".to_owned()), |
| 260 | time: Some("2026-10-06T08:00:00+00:00".to_owned()), |
| 261 | text: "original words".to_owned(), |
| 262 | }] |
| 263 | ); |
| 264 | } |
| 265 | } |