BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / agent_api / extras.rs

extras.rs

extras.rs 10.2 KiB Download Raw
1use mcp::{
2 FileInfo, ForwardedInfo, ImageInfo, ImageKind, LinkInfo, MentionInfo, MessageExtras, QuoteInfo, ReactionInfo,
3};
4use store::MessageRecord;
5use teams_core::MentionTarget;
6
7pub 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
72fn 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::Other => "other",
81 }
82}
83
84pub fn image_kind(kind: teams_core::ImageKind) -> ImageKind {
85 match kind {
86 teams_core::ImageKind::Inline => ImageKind::Inline,
87 teams_core::ImageKind::Gif => ImageKind::Gif,
88 teams_core::ImageKind::Sticker => ImageKind::Sticker,
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use chrono::{TimeZone, Utc};
95
96 use super::*;
97
98 fn record() -> MessageRecord {
99 MessageRecord {
100 conversation_id: "19:c@thread.v2".to_owned(),
101 message_id: "1728".to_owned(),
102 sender_id: Some("user-ada".to_owned()),
103 sender_name: Some("Ada Example".to_owned()),
104 created_at: Utc.with_ymd_and_hms(2026, 10, 6, 9, 0, 0).unwrap(),
105 body_html: concat!(
106 r#"<attachment id="Q1"></attachment><p>Hi <at id="0">Bob Sample</at>, see this</p>"#,
107 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>"#,
108 r#"<p><img src="https://media0.giphy.com/media/abc/giphy.gif"></p>"#,
109 r#"<attachment id="F1"></attachment>"#,
110 r#"<p><a href="https://news.example.com/story">https://news.example.com/story</a></p>"#,
111 )
112 .to_owned(),
113 attachments_json: r#"[
114 {"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","quote":{"message_id":"1700","preview":"earlier words","sender_name":"Bob Sample"}},
115 {"content_type":"reference","name":"Plan.docx","url":"https://example.sharepoint.com/Plan.docx","text":null,"id":"F1"},
116 {"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\"}]}"}
117 ]"#
118 .to_owned(),
119 reactions_json: r#"[
120 {"reaction_type":"like","user_id":"user-me","user_name":"Me"},
121 {"reaction_type":"like","user_id":"user-bob","user_name":"Bob Sample"},
122 {"reaction_type":"heart","user_id":"user-bob","user_name":"Bob Sample"}
123 ]"#
124 .to_owned(),
125 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(),
126 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(),
127 ..MessageRecord::default()
128 }
129 }
130
131 #[test]
132 fn extras_cover_every_part_of_a_real_shaped_message() {
133 let extras = message_extras(&record(), Some("user-me"));
134 assert!(
135 extras.text.starts_with("Hi @Bob Sample, see this"),
136 "{}",
137 extras.text
138 );
139 assert!(!extras.text.contains("earlier words"), "{}", extras.text);
140 let images: Vec<_> = extras
141 .images
142 .iter()
143 .map(|image| (image.kind, image.alt.as_deref()))
144 .collect();
145 assert_eq!(
146 images,
147 [(ImageKind::Inline, Some("a chart")), (ImageKind::Gif, None)]
148 );
149 assert_ne!(extras.images[0].image_id, extras.images[1].image_id);
150 assert_eq!(
151 extras.reactions,
152 [
153 ReactionInfo {
154 emoji: "\u{1F44D}".to_owned(),
155 count: 2,
156 mine: true
157 },
158 ReactionInfo {
159 emoji: "\u{2764}".to_owned(),
160 count: 1,
161 mine: false
162 },
163 ]
164 );
165 assert_eq!(
166 extras.mentions,
167 [MentionInfo {
168 name: "Bob Sample".to_owned(),
169 user_id: Some("user-bob".to_owned())
170 }]
171 );
172 assert_eq!(extras.files.len(), 1);
173 assert_eq!(extras.files[0].name, "Plan.docx");
174 assert_eq!(extras.files[0].file_type.as_deref(), Some("word"));
175 assert_eq!(
176 extras.quote,
177 Some(QuoteInfo {
178 message_id: "1700".to_owned(),
179 sender: Some("Bob Sample".to_owned()),
180 text: "earlier words".to_owned(),
181 })
182 );
183 assert_eq!(extras.cards, ["Build passed"]);
184 assert_eq!(
185 extras.links,
186 [LinkInfo {
187 url: "https://news.example.com/story".to_owned(),
188 title: Some("A story".to_owned())
189 }]
190 );
191 }
192
193 #[test]
194 fn an_inline_reply_blockquote_becomes_the_quote() {
195 let stored = MessageRecord {
196 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(),
197 ..MessageRecord::default()
198 };
199 let extras = message_extras(&stored, None);
200 assert_eq!(extras.text, "answer");
201 assert_eq!(
202 extras.quote,
203 Some(QuoteInfo {
204 message_id: "1700".to_owned(),
205 sender: Some("Bob Sample".to_owned()),
206 text: "earlier words".to_owned(),
207 })
208 );
209 }
210
211 #[test]
212 fn a_plain_message_has_no_extras() {
213 let plain = MessageRecord {
214 body_html: "<p>hello</p>".to_owned(),
215 attachments_json: "[]".to_owned(),
216 reactions_json: "[]".to_owned(),
217 mentions_json: "[]".to_owned(),
218 links_json: "[]".to_owned(),
219 ..MessageRecord::default()
220 };
221 let extras = message_extras(&plain, None);
222 assert_eq!(extras.text, "hello");
223 assert_eq!(
224 MessageExtras {
225 text: String::new(),
226 ..extras
227 },
228 MessageExtras::default()
229 );
230 }
231
232 #[test]
233 fn a_forward_lists_the_original_message_apart_from_the_text() {
234 let forwarded = MessageRecord {
235 body_html: r#"<p>fyi</p><attachment id="9"></attachment>"#.to_owned(),
236 attachments_json: serde_json::json!([{
237 "content_type": "forwardedMessageReference",
238 "name": null, "url": null, "text": null, "id": "9",
239 "content": serde_json::json!({
240 "originalMessageId": "9",
241 "originalMessageContent": "<p>original <b>words</b></p>",
242 "originalConversationId": "19:origin@thread.v2",
243 "originalSentDateTime": "2026-10-06T08:00:00.000+00:00",
244 "originalMessageSender": {"user": {"id": "user-cleo", "displayName": null}}
245 }).to_string()
246 }])
247 .to_string(),
248 ..record()
249 };
250 let extras = message_extras(&forwarded, None);
251 assert_eq!(extras.text, "fyi");
252 assert_eq!(
253 extras.forwarded,
254 [ForwardedInfo {
255 message_id: "9".to_owned(),
256 conversation_id: Some("19:origin@thread.v2".to_owned()),
257 sender: None,
258 sender_id: Some("user-cleo".to_owned()),
259 time: Some("2026-10-06T08:00:00+00:00".to_owned()),
260 text: "original words".to_owned(),
261 }]
262 );
263 }
264}