| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use store::{MessageRecord, Sidebar, Store}; |
| 5 | |
| 6 | use super::rules::{ChatKind, Incoming, Preview}; |
| 7 | use crate::app_state::chat_title; |
| 8 | |
| 9 | const RECENT_WINDOW: usize = 20; |
| 10 | const FILE_FALLBACK: &str = "File"; |
| 11 | |
| 12 | pub struct IncomingTracker { |
| 13 | started: DateTime<Utc>, |
| 14 | watermarks: HashMap<String, DateTime<Utc>>, |
| 15 | } |
| 16 | |
| 17 | impl IncomingTracker { |
| 18 | pub fn new(started: DateTime<Utc>) -> Self { |
| 19 | IncomingTracker { |
| 20 | started, |
| 21 | watermarks: HashMap::new(), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub fn collect( |
| 26 | &mut self, |
| 27 | store: &Store, |
| 28 | sidebar: &Sidebar, |
| 29 | my_user_id: Option<&str>, |
| 30 | conversation_id: &str, |
| 31 | ) -> Vec<Incoming> { |
| 32 | let Ok(records) = store.messages(conversation_id, None, RECENT_WINDOW) else { |
| 33 | return Vec::new(); |
| 34 | }; |
| 35 | self.fresh(records, sidebar, my_user_id) |
| 36 | } |
| 37 | |
| 38 | fn fresh( |
| 39 | &mut self, |
| 40 | records: Vec<MessageRecord>, |
| 41 | sidebar: &Sidebar, |
| 42 | my_user_id: Option<&str>, |
| 43 | ) -> Vec<Incoming> { |
| 44 | let mut incoming = Vec::new(); |
| 45 | for record in records { |
| 46 | let watermark = self |
| 47 | .watermarks |
| 48 | .get(&record.conversation_id) |
| 49 | .copied() |
| 50 | .unwrap_or(self.started) |
| 51 | .max(self.started); |
| 52 | if record.created_at <= watermark { |
| 53 | continue; |
| 54 | } |
| 55 | self.watermarks |
| 56 | .insert(record.conversation_id.clone(), record.created_at); |
| 57 | if record.deleted || is_own(&record, my_user_id) { |
| 58 | continue; |
| 59 | } |
| 60 | if let Some(entry) = build_incoming(&record, sidebar, my_user_id) { |
| 61 | incoming.push(entry); |
| 62 | } |
| 63 | } |
| 64 | incoming |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn is_own(record: &MessageRecord, my_user_id: Option<&str>) -> bool { |
| 69 | matches!((record.sender_id.as_deref(), my_user_id), (Some(sender), Some(me)) if sender == me) |
| 70 | } |
| 71 | |
| 72 | pub fn build_incoming( |
| 73 | record: &MessageRecord, |
| 74 | sidebar: &Sidebar, |
| 75 | my_user_id: Option<&str>, |
| 76 | ) -> Option<Incoming> { |
| 77 | let (kind, chat_name) = chat_context(sidebar, &record.conversation_id)?; |
| 78 | Some(Incoming { |
| 79 | conversation_id: record.conversation_id.clone(), |
| 80 | message_id: record.message_id.clone(), |
| 81 | kind, |
| 82 | chat_title: chat_name, |
| 83 | sender_id: record.sender_id.clone(), |
| 84 | sender_name: record.sender_name.clone().unwrap_or_default(), |
| 85 | preview: preview_of(record), |
| 86 | mentions_me: mentions_me(record, my_user_id), |
| 87 | muted: false, |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | fn chat_context(sidebar: &Sidebar, conversation_id: &str) -> Option<(ChatKind, String)> { |
| 92 | if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) { |
| 93 | let kind = if chat.kind.eq_ignore_ascii_case("oneOnOne") { |
| 94 | ChatKind::Direct |
| 95 | } else { |
| 96 | ChatKind::Group { |
| 97 | member_count: chat.members.len().max(2), |
| 98 | } |
| 99 | }; |
| 100 | return Some((kind, chat_title(chat))); |
| 101 | } |
| 102 | sidebar.teams.iter().find_map(|team| { |
| 103 | team.channels |
| 104 | .iter() |
| 105 | .find(|channel| channel.id == conversation_id) |
| 106 | .map(|channel| { |
| 107 | ( |
| 108 | ChatKind::Channel { |
| 109 | team: team.team.name.clone(), |
| 110 | channel: channel.name.clone(), |
| 111 | }, |
| 112 | channel.name.clone(), |
| 113 | ) |
| 114 | }) |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | fn mentions_me(record: &MessageRecord, my_user_id: Option<&str>) -> bool { |
| 119 | let Some(me) = my_user_id else { |
| 120 | return false; |
| 121 | }; |
| 122 | teams_core::mentions(record) |
| 123 | .iter() |
| 124 | .any(|mention| mention.user_id.as_deref() == Some(me)) |
| 125 | } |
| 126 | |
| 127 | fn preview_of(record: &MessageRecord) -> Preview { |
| 128 | if let Some(text) = teams_core::preview_text(&record.body_html) { |
| 129 | return Preview::Text(text); |
| 130 | } |
| 131 | if !teams_core::images(record).is_empty() { |
| 132 | return Preview::Image; |
| 133 | } |
| 134 | let file_name = teams_core::attachments(record) |
| 135 | .into_iter() |
| 136 | .find_map(|attachment| attachment.name); |
| 137 | Preview::Text(file_name.unwrap_or_else(|| FILE_FALLBACK.to_owned())) |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use chrono::Duration; |
| 143 | use store::{ChatRecord, MemberRecord}; |
| 144 | |
| 145 | use super::*; |
| 146 | |
| 147 | fn sidebar() -> Sidebar { |
| 148 | Sidebar { |
| 149 | chats: vec![ |
| 150 | ChatRecord { |
| 151 | id: "direct".into(), |
| 152 | kind: "oneOnOne".into(), |
| 153 | title: "Mara".into(), |
| 154 | ..Default::default() |
| 155 | }, |
| 156 | ChatRecord { |
| 157 | id: "group".into(), |
| 158 | kind: "group".into(), |
| 159 | title: "Team".into(), |
| 160 | members: vec![MemberRecord::default(); 5], |
| 161 | ..Default::default() |
| 162 | }, |
| 163 | ], |
| 164 | teams: Vec::new(), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn record(conversation: &str, sender: &str, at: DateTime<Utc>, html: &str) -> MessageRecord { |
| 169 | MessageRecord { |
| 170 | conversation_id: conversation.into(), |
| 171 | message_id: format!("{conversation}-{}", at.timestamp_millis()), |
| 172 | reply_to_id: None, |
| 173 | sender_id: Some(sender.into()), |
| 174 | sender_name: Some("Sender".into()), |
| 175 | created_at: at, |
| 176 | edited_at: None, |
| 177 | deleted: false, |
| 178 | body_html: html.into(), |
| 179 | attachments_json: "[]".into(), |
| 180 | reactions_json: "[]".into(), |
| 181 | mentions_json: "[]".into(), |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn history_before_start_is_ignored() { |
| 187 | let start = Utc::now(); |
| 188 | let mut tracker = IncomingTracker::new(start); |
| 189 | let old = record("direct", "u1", start - Duration::minutes(5), "alt"); |
| 190 | let new = record("direct", "u1", start + Duration::seconds(5), "neu"); |
| 191 | let found = tracker.fresh(vec![old, new], &sidebar(), Some("me")); |
| 192 | assert_eq!(found.len(), 1); |
| 193 | assert_eq!(found[0].preview, Preview::Text("neu".into())); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn same_message_is_not_reported_twice() { |
| 198 | let start = Utc::now(); |
| 199 | let mut tracker = IncomingTracker::new(start); |
| 200 | let message = record("direct", "u1", start + Duration::seconds(5), "neu"); |
| 201 | assert_eq!(tracker.fresh(vec![message.clone()], &sidebar(), None).len(), 1); |
| 202 | assert!(tracker.fresh(vec![message], &sidebar(), None).is_empty()); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn own_messages_from_other_devices_are_skipped() { |
| 207 | let start = Utc::now(); |
| 208 | let mut tracker = IncomingTracker::new(start); |
| 209 | let own = record("direct", "me", start + Duration::seconds(5), "ich"); |
| 210 | assert!(tracker.fresh(vec![own], &sidebar(), Some("me")).is_empty()); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn detects_mention_of_me_and_group_size() { |
| 215 | let start = Utc::now(); |
| 216 | let mut message = record("group", "u1", start + Duration::seconds(5), "<at>Me</at> hi"); |
| 217 | message.mentions_json = r#"[{"user_id":"me","name":"Me"}]"#.into(); |
| 218 | let found = build_incoming(&message, &sidebar(), Some("me")).unwrap(); |
| 219 | assert!(found.mentions_me); |
| 220 | assert_eq!(found.kind, ChatKind::Group { member_count: 5 }); |
| 221 | assert!(!build_incoming(&message, &sidebar(), Some("other")).unwrap().mentions_me); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn unknown_conversation_yields_nothing() { |
| 226 | let message = record("nowhere", "u1", Utc::now(), "x"); |
| 227 | assert!(build_incoming(&message, &sidebar(), None).is_none()); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn empty_body_without_image_falls_back_to_file_label() { |
| 232 | let message = record("direct", "u1", Utc::now(), ""); |
| 233 | let found = build_incoming(&message, &sidebar(), None).unwrap(); |
| 234 | assert_eq!(found.preview, Preview::Text(FILE_FALLBACK.into())); |
| 235 | } |
| 236 | } |