| 1 | use chrono::{DateTime, Utc}; |
| 2 | use teams_core::{ActivityItem, ActivitySnapshot}; |
| 3 | |
| 4 | use super::feed::{Actor, Entry, Feed, Kind}; |
| 5 | use crate::rows::reaction_glyph; |
| 6 | |
| 7 | const ORG_USER_PREFIX: &str = "8:orgid:"; |
| 8 | const CHANNEL_THREAD_TYPES: [&str; 2] = ["topic", "space"]; |
| 9 | |
| 10 | pub 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 | |
| 28 | fn 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 | |
| 66 | pub 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 | |
| 84 | fn 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 | |
| 91 | fn 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 | |
| 105 | fn 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" => "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)] |
| 121 | mod 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 | assert_eq!(entry_of(&item("mention", "person")).preview, "Ship it"); |
| 202 | assert_eq!(entry_of(&item("reactionInChat", "like")).glyphs.len(), 1); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn read_and_bell_come_from_the_snapshot_and_the_local_seen_mark() { |
| 207 | let snapshot = ActivitySnapshot { |
| 208 | items: vec![ |
| 209 | ActivityItem { |
| 210 | message_id: "old".into(), |
| 211 | timestamp_ms: 100, |
| 212 | version_ms: 100, |
| 213 | ..item("mention", "person") |
| 214 | }, |
| 215 | ActivityItem { |
| 216 | message_id: "new".into(), |
| 217 | timestamp_ms: 300, |
| 218 | version_ms: 300, |
| 219 | ..item("mention", "person") |
| 220 | }, |
| 221 | ActivityItem { |
| 222 | message_id: "flagged".into(), |
| 223 | timestamp_ms: 400, |
| 224 | version_ms: 400, |
| 225 | read_flag: true, |
| 226 | ..item("mention", "person") |
| 227 | }, |
| 228 | ], |
| 229 | horizons: ActivityHorizons { |
| 230 | seen_ms: 50, |
| 231 | bookmark_ms: 150, |
| 232 | }, |
| 233 | }; |
| 234 | let feed = feed_of(&snapshot, 0); |
| 235 | let read: Vec<(&str, bool, bool)> = feed |
| 236 | .entries() |
| 237 | .iter() |
| 238 | .map(|entry| (entry.id.as_str(), entry.read, entry.counts_for_bell)) |
| 239 | .collect(); |
| 240 | assert_eq!( |
| 241 | read, |
| 242 | [("old", true, false), ("new", false, true), ("flagged", true, false)] |
| 243 | ); |
| 244 | assert_eq!(feed_of(&snapshot, 300).bell_count(), 0); |
| 245 | } |
| 246 | } |