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