BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / activity / server.rs

server.rs

server.rs 9.5 KiB Download Raw
1use chrono::{DateTime, Utc};
2use teams_core::{ActivityItem, ActivitySnapshot};
3
4use super::feed::{Actor, Entry, Feed, Kind};
5use crate::rows::reaction_glyph;
6
7const ORG_USER_PREFIX: &str = "8:orgid:";
8const CHANNEL_THREAD_TYPES: [&str; 2] = ["topic", "space"];
9
10pub fn feed_of(snapshot: &ActivitySnapshot, seen_locally_ms: i64) -> Feed {
11 Feed::new(
12 snapshot
13 .items
14 .iter()
15 .map(|item| {
16 let read = snapshot.is_read(item);
17 Entry {
18 read,
19 counts_for_bell: snapshot.counts_for_bell(item)
20 && item.timestamp_ms > seen_locally_ms,
21 ..entry_of(item)
22 }
23 })
24 .collect(),
25 )
26}
27
28fn entry_of(item: &ActivityItem) -> Entry {
29 let kind = kind_of(item);
30 let conversation_id = match (&item.meeting_thread_id, item.source_thread_id.is_empty()) {
31 (Some(meeting_thread_id), true) => meeting_thread_id.clone(),
32 _ => item.source_thread_id.clone(),
33 };
34 Entry {
35 id: item.message_id.clone(),
36 conversation_id,
37 kind,
38 activity_type: item.activity_type.clone(),
39 subtype: item.activity_subtype.clone(),
40 message_id: jump_target(item, kind),
41 actors: vec![Actor {
42 user_id: item
43 .source_user_id
44 .as_deref()
45 .and_then(|user_id| user_id.strip_prefix(ORG_USER_PREFIX))
46 .map(str::to_owned),
47 name: item.source_user_name.clone(),
48 }],
49 preview: preview_of(item),
50 glyphs: match kind {
51 Kind::Reaction if !item.activity_subtype.is_empty() => {
52 vec![reaction_glyph(&item.activity_subtype)]
53 }
54 _ => Vec::new(),
55 },
56 count: item.count.max(1),
57 updated_at: DateTime::from_timestamp_millis(item.timestamp_ms).unwrap_or_else(Utc::now),
58 read: false,
59 counts_for_bell: false,
60 source_label: item.source_app_name.clone().or_else(|| item.thread_topic.clone()),
61 link: item.tap_url.clone(),
62 reply_id: channel_reply(item, kind),
63 }
64}
65
66pub fn kind_of(item: &ActivityItem) -> Kind {
67 match item.activity_type.as_str() {
68 "mention" | "mentionInChat" | "mentionInCommunity" | "mentionInSubCommunity"
69 | "communityMention" => Kind::Mention,
70 "reply" | "replyToReply" | "replyInChat" | "communityReply" | "communityReplyToReply" => {
71 Kind::Reply
72 }
73 "reaction" | "reactionInChat" | "like" | "likeInChat" | "reactionInCommunity"
74 | "reactionInSubCommunity" => Kind::Reaction,
75 "call" if item.activity_subtype == "missedCall" => Kind::MissedCall,
76 "call" | "voicemail" | "callOrMeetup" => Kind::Call,
77 "follow" => Kind::Posted,
78 "teamMembershipChange" => Kind::Team,
79 "msGraph" | "thirdParty" => Kind::App,
80 _ => Kind::Other,
81 }
82}
83
84fn channel_reply(item: &ActivityItem, kind: Kind) -> Option<String> {
85 let target = jump_target(item, kind);
86 item.source_message_id
87 .clone()
88 .filter(|reply_id| !target.is_empty() && *reply_id != target)
89}
90
91fn jump_target(item: &ActivityItem, kind: Kind) -> String {
92 if matches!(kind, Kind::MissedCall | Kind::Call | Kind::Team | Kind::App) {
93 return String::new();
94 }
95 let in_channel = item
96 .thread_type
97 .as_deref()
98 .is_some_and(|thread_type| CHANNEL_THREAD_TYPES.contains(&thread_type));
99 let root = item.source_reply_chain_id.as_ref().filter(|_| in_channel);
100 root.or(item.source_message_id.as_ref())
101 .cloned()
102 .unwrap_or_default()
103}
104
105fn preview_of(item: &ActivityItem) -> String {
106 let fixed = match item.preview_template.as_str() {
107 "sentAFile" => "Sent a file",
108 "sentAnImage" => "Sent an image",
109 "sentACard" => "Sent a card",
110 "previewUnavailable" if item.preview.is_empty() => "Preview Unavailable",
111 "deleted" => "This message has been deleted.",
112 "forwardedMessage" => "Forwarded message",
113 "isMeeting" => "Teams Meeting",
114 "replyWithQuote" if item.preview.is_empty() => "Replied with quote",
115 _ => return item.preview.clone(),
116 };
117 fixed.to_owned()
118}
119
120#[cfg(test)]
121mod tests {
122 use teams_core::ActivityHorizons;
123
124 use super::*;
125
126 fn item(activity_type: &str, subtype: &str) -> ActivityItem {
127 ActivityItem {
128 message_id: "1791368000001".into(),
129 activity_id: "1".into(),
130 activity_type: activity_type.into(),
131 activity_subtype: subtype.into(),
132 source_thread_id: "19:a@thread.tacv2".into(),
133 source_message_id: Some("1791367000002".into()),
134 source_reply_chain_id: Some("1791367000001".into()),
135 source_user_id: Some("8:orgid:user-1".into()),
136 source_user_name: "Ada Example".into(),
137 preview: "Ship it".into(),
138 preview_template: "hasText".into(),
139 thread_type: Some("topic".into()),
140 count: 1,
141 timestamp_ms: 1791368000000,
142 version_ms: 1791368000000,
143 ..ActivityItem::default()
144 }
145 }
146
147 #[test]
148 fn maps_the_teams_activity_types() {
149 let cases = [
150 ("mention", "person", Kind::Mention),
151 ("mentionInChat", "everyone", Kind::Mention),
152 ("replyToReply", "", Kind::Reply),
153 ("reactionInChat", "heart", Kind::Reaction),
154 ("call", "missedCall", Kind::MissedCall),
155 ("call", "missedMeetingNudge", Kind::Call),
156 ("follow", "channelNewMessage", Kind::Posted),
157 ("teamMembershipChange", "addedToTeam", Kind::Team),
158 ("msGraph", "recapIsReadyV2", Kind::App),
159 ("unknownType", "", Kind::Other),
160 ];
161 for (activity_type, subtype, kind) in cases {
162 assert_eq!(kind_of(&item(activity_type, subtype)), kind, "{activity_type}");
163 }
164 }
165
166 #[test]
167 fn a_channel_reply_jumps_to_its_root_and_a_chat_to_the_message() {
168 let channel = entry_of(&item("replyToReply", ""));
169 assert_eq!(channel.message_id, "1791367000001");
170 assert_eq!(channel.reply_id.as_deref(), Some("1791367000002"));
171 let chat = entry_of(&ActivityItem {
172 thread_type: Some("chat".into()),
173 ..item("mentionInChat", "person")
174 });
175 assert_eq!(chat.message_id, "1791367000002");
176 assert_eq!(chat.reply_id, None);
177 assert_eq!(entry_of(&item("call", "missedCall")).message_id, "");
178 assert_eq!(chat.actors[0].user_id.as_deref(), Some("user-1"));
179 }
180
181 #[test]
182 fn a_recap_opens_the_meeting_chat_it_names() {
183 let recap = entry_of(&ActivityItem {
184 source_thread_id: String::new(),
185 meeting_thread_id: Some("19:meeting_Zm9v@thread.v2".into()),
186 tap_url: Some("https://teams.microsoft.com/l/entity/x".into()),
187 ..item("msGraph", "recapIsReadyV2")
188 });
189 assert_eq!(recap.conversation_id, "19:meeting_Zm9v@thread.v2");
190 assert_eq!(recap.message_id, "");
191 assert_eq!(recap.link.as_deref(), Some("https://teams.microsoft.com/l/entity/x"));
192 }
193
194 #[test]
195 fn previews_follow_the_teams_template() {
196 let file = entry_of(&ActivityItem {
197 preview_template: "sentAFile".into(),
198 ..item("mention", "person")
199 });
200 assert_eq!(file.preview, "Sent a file");
201 let recap = entry_of(&ActivityItem {
202 preview: "You're following this meeting".into(),
203 preview_template: "previewUnavailable".into(),
204 ..item("msGraph", "recapIsReadyV2")
205 });
206 assert_eq!(recap.preview, "You're following this meeting");
207 let unavailable = entry_of(&ActivityItem {
208 preview: String::new(),
209 preview_template: "previewUnavailable".into(),
210 ..item("mention", "person")
211 });
212 assert_eq!(unavailable.preview, "Preview Unavailable");
213 assert_eq!(entry_of(&item("mention", "person")).preview, "Ship it");
214 assert_eq!(entry_of(&item("reactionInChat", "like")).glyphs.len(), 1);
215 }
216
217 #[test]
218 fn read_and_bell_come_from_the_snapshot_and_the_local_seen_mark() {
219 let snapshot = ActivitySnapshot {
220 items: vec![
221 ActivityItem {
222 message_id: "old".into(),
223 timestamp_ms: 100,
224 version_ms: 100,
225 ..item("mention", "person")
226 },
227 ActivityItem {
228 message_id: "new".into(),
229 timestamp_ms: 300,
230 version_ms: 300,
231 ..item("mention", "person")
232 },
233 ActivityItem {
234 message_id: "flagged".into(),
235 timestamp_ms: 400,
236 version_ms: 400,
237 read_flag: true,
238 ..item("mention", "person")
239 },
240 ],
241 horizons: ActivityHorizons {
242 seen_ms: 50,
243 bookmark_ms: 150,
244 },
245 };
246 let feed = feed_of(&snapshot, 0);
247 let read: Vec<(&str, bool, bool)> = feed
248 .entries()
249 .iter()
250 .map(|entry| (entry.id.as_str(), entry.read, entry.counts_for_bell))
251 .collect();
252 assert_eq!(
253 read,
254 [("old", true, false), ("new", false, true), ("flagged", true, false)]
255 );
256 assert_eq!(feed_of(&snapshot, 300).bell_count(), 0);
257 }
258}