BlueGit

ict rusty-teams

Files

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

extras.rs

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