BlueGit

ict rusty-teams

Files

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

server.rs

server.rs 8.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 }
63}
64
65pub fn kind_of(item: &ActivityItem) -> Kind {
66 match item.activity_type.as_str() {
67 "mention" | "mentionInChat" | "mentionInCommunity" | "mentionInSubCommunity"
68 | "communityMention" => Kind::Mention,
69 "reply" | "replyToReply" | "replyInChat" | "communityReply" | "communityReplyToReply" => {
70 Kind::Reply
71 }
72 "reaction" | "reactionInChat" | "like" | "likeInChat" | "reactionInCommunity"
73 | "reactionInSubCommunity" => Kind::Reaction,
74 "call" if item.activity_subtype == "missedCall" => Kind::MissedCall,
75 "call" | "voicemail" | "callOrMeetup" => Kind::Call,
76 "follow" => Kind::Posted,
77 "teamMembershipChange" => Kind::Team,
78 "msGraph" | "thirdParty" => Kind::App,
79 _ => Kind::Other,
80 }
81}
82
83fn jump_target(item: &ActivityItem, kind: Kind) -> String {
84 if matches!(kind, Kind::MissedCall | Kind::Call | Kind::Team | Kind::App) {
85 return String::new();
86 }
87 let in_channel = item
88 .thread_type
89 .as_deref()
90 .is_some_and(|thread_type| CHANNEL_THREAD_TYPES.contains(&thread_type));
91 let root = item.source_reply_chain_id.as_ref().filter(|_| in_channel);
92 root.or(item.source_message_id.as_ref())
93 .cloned()
94 .unwrap_or_default()
95}
96
97fn preview_of(item: &ActivityItem) -> String {
98 let fixed = match item.preview_template.as_str() {
99 "sentAFile" => "Sent a file",
100 "sentAnImage" => "Sent an image",
101 "sentACard" => "Sent a card",
102 "previewUnavailable" => "Preview Unavailable",
103 "deleted" => "This message has been deleted.",
104 "forwardedMessage" => "Forwarded message",
105 "isMeeting" => "Teams Meeting",
106 "replyWithQuote" if item.preview.is_empty() => "Replied with quote",
107 _ => return item.preview.clone(),
108 };
109 fixed.to_owned()
110}
111
112#[cfg(test)]
113mod tests {
114 use teams_core::ActivityHorizons;
115
116 use super::*;
117
118 fn item(activity_type: &str, subtype: &str) -> ActivityItem {
119 ActivityItem {
120 message_id: "1791368000001".into(),
121 activity_id: "1".into(),
122 activity_type: activity_type.into(),
123 activity_subtype: subtype.into(),
124 source_thread_id: "19:a@thread.tacv2".into(),
125 source_message_id: Some("1791367000002".into()),
126 source_reply_chain_id: Some("1791367000001".into()),
127 source_user_id: Some("8:orgid:user-1".into()),
128 source_user_name: "Ada Example".into(),
129 preview: "Ship it".into(),
130 preview_template: "hasText".into(),
131 thread_type: Some("topic".into()),
132 count: 1,
133 timestamp_ms: 1791368000000,
134 version_ms: 1791368000000,
135 ..ActivityItem::default()
136 }
137 }
138
139 #[test]
140 fn maps_the_teams_activity_types() {
141 let cases = [
142 ("mention", "person", Kind::Mention),
143 ("mentionInChat", "everyone", Kind::Mention),
144 ("replyToReply", "", Kind::Reply),
145 ("reactionInChat", "heart", Kind::Reaction),
146 ("call", "missedCall", Kind::MissedCall),
147 ("call", "missedMeetingNudge", Kind::Call),
148 ("follow", "channelNewMessage", Kind::Posted),
149 ("teamMembershipChange", "addedToTeam", Kind::Team),
150 ("msGraph", "recapIsReadyV2", Kind::App),
151 ("unknownType", "", Kind::Other),
152 ];
153 for (activity_type, subtype, kind) in cases {
154 assert_eq!(kind_of(&item(activity_type, subtype)), kind, "{activity_type}");
155 }
156 }
157
158 #[test]
159 fn a_channel_reply_jumps_to_its_root_and_a_chat_to_the_message() {
160 let channel = entry_of(&item("replyToReply", ""));
161 assert_eq!(channel.message_id, "1791367000001");
162 let chat = entry_of(&ActivityItem {
163 thread_type: Some("chat".into()),
164 ..item("mentionInChat", "person")
165 });
166 assert_eq!(chat.message_id, "1791367000002");
167 assert_eq!(entry_of(&item("call", "missedCall")).message_id, "");
168 assert_eq!(chat.actors[0].user_id.as_deref(), Some("user-1"));
169 }
170
171 #[test]
172 fn a_recap_opens_the_meeting_chat_it_names() {
173 let recap = entry_of(&ActivityItem {
174 source_thread_id: String::new(),
175 meeting_thread_id: Some("19:meeting_Zm9v@thread.v2".into()),
176 tap_url: Some("https://teams.microsoft.com/l/entity/x".into()),
177 ..item("msGraph", "recapIsReadyV2")
178 });
179 assert_eq!(recap.conversation_id, "19:meeting_Zm9v@thread.v2");
180 assert_eq!(recap.message_id, "");
181 assert_eq!(recap.link.as_deref(), Some("https://teams.microsoft.com/l/entity/x"));
182 }
183
184 #[test]
185 fn previews_follow_the_teams_template() {
186 let file = entry_of(&ActivityItem {
187 preview_template: "sentAFile".into(),
188 ..item("mention", "person")
189 });
190 assert_eq!(file.preview, "Sent a file");
191 assert_eq!(entry_of(&item("mention", "person")).preview, "Ship it");
192 assert_eq!(entry_of(&item("reactionInChat", "like")).glyphs.len(), 1);
193 }
194
195 #[test]
196 fn read_and_bell_come_from_the_snapshot_and_the_local_seen_mark() {
197 let snapshot = ActivitySnapshot {
198 items: vec![
199 ActivityItem {
200 message_id: "old".into(),
201 timestamp_ms: 100,
202 version_ms: 100,
203 ..item("mention", "person")
204 },
205 ActivityItem {
206 message_id: "new".into(),
207 timestamp_ms: 300,
208 version_ms: 300,
209 ..item("mention", "person")
210 },
211 ActivityItem {
212 message_id: "flagged".into(),
213 timestamp_ms: 400,
214 version_ms: 400,
215 read_flag: true,
216 ..item("mention", "person")
217 },
218 ],
219 horizons: ActivityHorizons {
220 seen_ms: 50,
221 bookmark_ms: 150,
222 },
223 };
224 let feed = feed_of(&snapshot, 0);
225 let read: Vec<(&str, bool, bool)> = feed
226 .entries()
227 .iter()
228 .map(|entry| (entry.id.as_str(), entry.read, entry.counts_for_bell))
229 .collect();
230 assert_eq!(
231 read,
232 [("old", true, false), ("new", false, true), ("flagged", true, false)]
233 );
234 assert_eq!(feed_of(&snapshot, 300).bell_count(), 0);
235 }
236}