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