BlueGit

ict rusty-teams

Files

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

extras.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
extras.rs 8.2 KiB Download Raw
1use mcp::{
2 FileInfo, 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 cards: teams_core::card_texts(record),
51 links: teams_core::link_preview(record)
52 .into_iter()
53 .map(|preview| LinkInfo {
54 url: preview.url,
55 title: preview.title,
56 })
57 .collect(),
58 }
59}
60
61fn file_type(kind: teams_core::FileKind) -> &'static str {
62 match kind {
63 teams_core::FileKind::Word => "word",
64 teams_core::FileKind::Excel => "excel",
65 teams_core::FileKind::PowerPoint => "powerpoint",
66 teams_core::FileKind::Pdf => "pdf",
67 teams_core::FileKind::Image => "image",
68 teams_core::FileKind::Archive => "archive",
69 teams_core::FileKind::Other => "other",
70 }
71}
72
73pub fn image_kind(kind: teams_core::ImageKind) -> ImageKind {
74 match kind {
75 teams_core::ImageKind::Inline => ImageKind::Inline,
76 teams_core::ImageKind::Gif => ImageKind::Gif,
77 teams_core::ImageKind::Sticker => ImageKind::Sticker,
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use chrono::{TimeZone, Utc};
84
85 use super::*;
86
87 fn record() -> MessageRecord {
88 MessageRecord {
89 conversation_id: "19:c@thread.v2".to_owned(),
90 message_id: "1728".to_owned(),
91 sender_id: Some("user-ada".to_owned()),
92 sender_name: Some("Ada Example".to_owned()),
93 created_at: Utc.with_ymd_and_hms(2026, 10, 6, 9, 0, 0).unwrap(),
94 body_html: concat!(
95 r#"<attachment id="Q1"></attachment><p>Hi <at id="0">Bob Sample</at>, see this</p>"#,
96 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>"#,
97 r#"<p><img src="https://media0.giphy.com/media/abc/giphy.gif"></p>"#,
98 r#"<attachment id="F1"></attachment>"#,
99 )
100 .to_owned(),
101 attachments_json: r#"[
102 {"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","quote":{"message_id":"1700","preview":"earlier words","sender_name":"Bob Sample"}},
103 {"content_type":"reference","name":"Plan.docx","url":"https://example.sharepoint.com/Plan.docx","text":null,"id":"F1"},
104 {"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\"}]}"}
105 ]"#
106 .to_owned(),
107 reactions_json: r#"[
108 {"reaction_type":"like","user_id":"user-me","user_name":"Me"},
109 {"reaction_type":"like","user_id":"user-bob","user_name":"Bob Sample"},
110 {"reaction_type":"heart","user_id":"user-bob","user_name":"Bob Sample"}
111 ]"#
112 .to_owned(),
113 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(),
114 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(),
115 ..MessageRecord::default()
116 }
117 }
118
119 #[test]
120 fn extras_cover_every_part_of_a_real_shaped_message() {
121 let extras = message_extras(&record(), Some("user-me"));
122 assert!(
123 extras.text.starts_with("Hi @Bob Sample, see this"),
124 "{}",
125 extras.text
126 );
127 assert!(!extras.text.contains("earlier words"), "{}", extras.text);
128 let images: Vec<_> = extras
129 .images
130 .iter()
131 .map(|image| (image.kind, image.alt.as_deref()))
132 .collect();
133 assert_eq!(
134 images,
135 [(ImageKind::Inline, Some("a chart")), (ImageKind::Gif, None)]
136 );
137 assert_ne!(extras.images[0].image_id, extras.images[1].image_id);
138 assert_eq!(
139 extras.reactions,
140 [
141 ReactionInfo {
142 emoji: "\u{1F44D}".to_owned(),
143 count: 2,
144 mine: true
145 },
146 ReactionInfo {
147 emoji: "\u{2764}".to_owned(),
148 count: 1,
149 mine: false
150 },
151 ]
152 );
153 assert_eq!(
154 extras.mentions,
155 [MentionInfo {
156 name: "Bob Sample".to_owned(),
157 user_id: Some("user-bob".to_owned())
158 }]
159 );
160 assert_eq!(extras.files.len(), 1);
161 assert_eq!(extras.files[0].name, "Plan.docx");
162 assert_eq!(extras.files[0].file_type.as_deref(), Some("word"));
163 assert_eq!(
164 extras.quote,
165 Some(QuoteInfo {
166 message_id: "1700".to_owned(),
167 sender: Some("Bob Sample".to_owned()),
168 text: "earlier words".to_owned(),
169 })
170 );
171 assert_eq!(extras.cards, ["Build passed"]);
172 assert_eq!(
173 extras.links,
174 [LinkInfo {
175 url: "https://news.example.com/story".to_owned(),
176 title: Some("A story".to_owned())
177 }]
178 );
179 }
180
181 #[test]
182 fn an_inline_reply_blockquote_becomes_the_quote() {
183 let stored = MessageRecord {
184 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(),
185 ..MessageRecord::default()
186 };
187 let extras = message_extras(&stored, None);
188 assert_eq!(extras.text, "answer");
189 assert_eq!(
190 extras.quote,
191 Some(QuoteInfo {
192 message_id: "1700".to_owned(),
193 sender: Some("Bob Sample".to_owned()),
194 text: "earlier words".to_owned(),
195 })
196 );
197 }
198
199 #[test]
200 fn a_plain_message_has_no_extras() {
201 let plain = MessageRecord {
202 body_html: "<p>hello</p>".to_owned(),
203 attachments_json: "[]".to_owned(),
204 reactions_json: "[]".to_owned(),
205 mentions_json: "[]".to_owned(),
206 links_json: "[]".to_owned(),
207 ..MessageRecord::default()
208 };
209 let extras = message_extras(&plain, None);
210 assert_eq!(extras.text, "hello");
211 assert_eq!(
212 MessageExtras {
213 text: String::new(),
214 ..extras
215 },
216 MessageExtras::default()
217 );
218 }
219}