| 1 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use store::{ChannelNotifications, MessageRecord, Sidebar, Store}; |
| 5 | |
| 6 | use super::rules::{ChannelSignals, ChatKind, ChatSignals, Incoming, Preview}; |
| 7 | use crate::app_state::chat_title; |
| 8 | |
| 9 | const RECENT_WINDOW: usize = 20; |
| 10 | const FILE_FALLBACK: &str = "File"; |
| 11 | const LATE_ARRIVALS_KEPT: usize = 500; |
| 12 | |
| 13 | #[derive(Default)] |
| 14 | pub struct LateArrivals { |
| 15 | keys: HashSet<(String, String)>, |
| 16 | order: VecDeque<(String, String)>, |
| 17 | } |
| 18 | |
| 19 | impl LateArrivals { |
| 20 | pub fn insert(&mut self, conversation_id: &str, message_ids: Vec<String>) { |
| 21 | for message_id in message_ids { |
| 22 | let key = (conversation_id.to_owned(), message_id); |
| 23 | if self.keys.insert(key.clone()) { |
| 24 | self.order.push_back(key); |
| 25 | } |
| 26 | } |
| 27 | while self.order.len() > LATE_ARRIVALS_KEPT { |
| 28 | if let Some(oldest) = self.order.pop_front() { |
| 29 | self.keys.remove(&oldest); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn contains(&self, conversation_id: &str, message_id: &str) -> bool { |
| 35 | self.keys |
| 36 | .contains(&(conversation_id.to_owned(), message_id.to_owned())) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub struct IncomingTracker { |
| 41 | started: DateTime<Utc>, |
| 42 | watermarks: HashMap<String, DateTime<Utc>>, |
| 43 | } |
| 44 | |
| 45 | impl IncomingTracker { |
| 46 | pub fn new(started: DateTime<Utc>) -> Self { |
| 47 | IncomingTracker { |
| 48 | started, |
| 49 | watermarks: HashMap::new(), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn collect( |
| 54 | &mut self, |
| 55 | store: &Store, |
| 56 | sidebar: &Sidebar, |
| 57 | my_user_id: Option<&str>, |
| 58 | late_arrivals: &LateArrivals, |
| 59 | conversation_id: &str, |
| 60 | ) -> Vec<Incoming> { |
| 61 | let Ok(records) = store.messages(conversation_id, None, RECENT_WINDOW) else { |
| 62 | return Vec::new(); |
| 63 | }; |
| 64 | let records = records |
| 65 | .into_iter() |
| 66 | .filter(|record| !late_arrivals.contains(&record.conversation_id, &record.message_id)) |
| 67 | .collect(); |
| 68 | self.fresh(records, store, sidebar, my_user_id) |
| 69 | } |
| 70 | |
| 71 | fn fresh( |
| 72 | &mut self, |
| 73 | records: Vec<MessageRecord>, |
| 74 | store: &Store, |
| 75 | sidebar: &Sidebar, |
| 76 | my_user_id: Option<&str>, |
| 77 | ) -> Vec<Incoming> { |
| 78 | let mut incoming = Vec::new(); |
| 79 | let newest_own = newest_own_message(&records, my_user_id); |
| 80 | for record in records { |
| 81 | let watermark = self |
| 82 | .watermarks |
| 83 | .get(&record.conversation_id) |
| 84 | .copied() |
| 85 | .unwrap_or(self.started) |
| 86 | .max(self.started); |
| 87 | if record.created_at <= watermark { |
| 88 | continue; |
| 89 | } |
| 90 | self.watermarks |
| 91 | .insert(record.conversation_id.clone(), record.created_at); |
| 92 | if record.deleted |
| 93 | || record.call_event_json.is_some() |
| 94 | || is_own(&record, my_user_id) |
| 95 | || seen_elsewhere(&record, sidebar, my_user_id, &newest_own) |
| 96 | { |
| 97 | continue; |
| 98 | } |
| 99 | if let Some(entry) = build_incoming(&record, store, sidebar, my_user_id) { |
| 100 | incoming.push(entry); |
| 101 | } |
| 102 | } |
| 103 | incoming |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fn is_own(record: &MessageRecord, my_user_id: Option<&str>) -> bool { |
| 108 | matches!((record.sender_id.as_deref(), my_user_id), (Some(sender), Some(me)) if sender == me) |
| 109 | } |
| 110 | |
| 111 | fn newest_own_message( |
| 112 | records: &[MessageRecord], |
| 113 | my_user_id: Option<&str>, |
| 114 | ) -> HashMap<String, DateTime<Utc>> { |
| 115 | let mut newest = HashMap::new(); |
| 116 | for record in records.iter().filter(|record| is_own(record, my_user_id)) { |
| 117 | let entry = newest |
| 118 | .entry(record.conversation_id.clone()) |
| 119 | .or_insert(record.created_at); |
| 120 | *entry = (*entry).max(record.created_at); |
| 121 | } |
| 122 | newest |
| 123 | } |
| 124 | |
| 125 | fn seen_elsewhere( |
| 126 | record: &MessageRecord, |
| 127 | sidebar: &Sidebar, |
| 128 | my_user_id: Option<&str>, |
| 129 | newest_own: &HashMap<String, DateTime<Utc>>, |
| 130 | ) -> bool { |
| 131 | let read_on_another_device = sidebar |
| 132 | .chats |
| 133 | .iter() |
| 134 | .find(|chat| chat.id == record.conversation_id) |
| 135 | .and_then(|chat| chat.last_read_at) |
| 136 | .is_some_and(|read_at| read_at >= record.created_at); |
| 137 | let answered_later = newest_own |
| 138 | .get(&record.conversation_id) |
| 139 | .is_some_and(|own_at| *own_at >= record.created_at); |
| 140 | read_on_another_device || answered_later || reacted_by_me(record, my_user_id) |
| 141 | } |
| 142 | |
| 143 | fn reacted_by_me(record: &MessageRecord, my_user_id: Option<&str>) -> bool { |
| 144 | let Some(me) = my_user_id else { |
| 145 | return false; |
| 146 | }; |
| 147 | teams_core::reactions(record) |
| 148 | .iter() |
| 149 | .any(|reaction| reaction.user_id.as_deref() == Some(me)) |
| 150 | } |
| 151 | |
| 152 | pub fn build_incoming( |
| 153 | record: &MessageRecord, |
| 154 | store: &Store, |
| 155 | sidebar: &Sidebar, |
| 156 | my_user_id: Option<&str>, |
| 157 | ) -> Option<Incoming> { |
| 158 | let (kind, chat_name) = chat_context(sidebar, &record.conversation_id)?; |
| 159 | let signals = match kind { |
| 160 | ChatKind::Channel { .. } => channel_signals(record, store, sidebar, my_user_id), |
| 161 | _ => ChannelSignals::default(), |
| 162 | }; |
| 163 | let chat = chat_signals(store, sidebar, &record.conversation_id, my_user_id); |
| 164 | let mentions_me = mentions_me(record, my_user_id) |
| 165 | || (!matches!(kind, ChatKind::Channel { .. }) && teams_core::mentions_whole_chat(record)); |
| 166 | Some(Incoming { |
| 167 | conversation_id: record.conversation_id.clone(), |
| 168 | message_id: record.message_id.clone(), |
| 169 | kind, |
| 170 | chat_title: chat_name, |
| 171 | sender_id: record.sender_id.clone(), |
| 172 | sender_name: record.sender_name.clone().unwrap_or_default(), |
| 173 | preview: preview_of(record), |
| 174 | mentions_me, |
| 175 | muted: is_muted_chat(sidebar, &record.conversation_id), |
| 176 | signals, |
| 177 | chat, |
| 178 | created_at: record.created_at, |
| 179 | importance: record.importance, |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | fn chat_signals( |
| 184 | store: &Store, |
| 185 | sidebar: &Sidebar, |
| 186 | conversation_id: &str, |
| 187 | my_user_id: Option<&str>, |
| 188 | ) -> ChatSignals { |
| 189 | let meeting = sidebar |
| 190 | .chats |
| 191 | .iter() |
| 192 | .any(|chat| chat.id == conversation_id && chat.kind.eq_ignore_ascii_case("meeting")); |
| 193 | let participated = meeting |
| 194 | && store |
| 195 | .messages(conversation_id, None, RECENT_WINDOW) |
| 196 | .is_ok_and(|records| records.iter().any(|record| is_own(record, my_user_id))); |
| 197 | ChatSignals { |
| 198 | meeting, |
| 199 | participated, |
| 200 | ..ChatSignals::default() |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | fn is_muted_chat(sidebar: &Sidebar, conversation_id: &str) -> bool { |
| 205 | sidebar |
| 206 | .chats |
| 207 | .iter() |
| 208 | .any(|chat| chat.id == conversation_id && chat.muted) |
| 209 | } |
| 210 | |
| 211 | fn chat_context(sidebar: &Sidebar, conversation_id: &str) -> Option<(ChatKind, String)> { |
| 212 | if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) { |
| 213 | let kind = if chat.kind.eq_ignore_ascii_case("oneOnOne") { |
| 214 | ChatKind::Direct |
| 215 | } else { |
| 216 | ChatKind::Group { |
| 217 | member_count: chat.members.len().max(2), |
| 218 | } |
| 219 | }; |
| 220 | return Some((kind, chat_title(chat))); |
| 221 | } |
| 222 | sidebar.teams.iter().find_map(|team| { |
| 223 | team.channels |
| 224 | .iter() |
| 225 | .find(|channel| channel.id == conversation_id) |
| 226 | .map(|channel| { |
| 227 | ( |
| 228 | ChatKind::Channel { |
| 229 | team: team.team.name.clone(), |
| 230 | channel: channel.name.clone(), |
| 231 | }, |
| 232 | channel.name.clone(), |
| 233 | ) |
| 234 | }) |
| 235 | }) |
| 236 | } |
| 237 | |
| 238 | fn channel_signals( |
| 239 | record: &MessageRecord, |
| 240 | store: &Store, |
| 241 | sidebar: &Sidebar, |
| 242 | my_user_id: Option<&str>, |
| 243 | ) -> ChannelSignals { |
| 244 | let in_my_thread = match (record.reply_to_id.as_deref(), my_user_id) { |
| 245 | (Some(root_id), Some(me)) => store |
| 246 | .thread_has_sender(&record.conversation_id, root_id, me) |
| 247 | .unwrap_or(false), |
| 248 | _ => false, |
| 249 | }; |
| 250 | ChannelSignals { |
| 251 | mentions_channel: teams_core::mentions(record) |
| 252 | .iter() |
| 253 | .any(|mention| mention.group), |
| 254 | in_my_thread, |
| 255 | hidden: channel_hidden(sidebar, &record.conversation_id), |
| 256 | is_reply: record.reply_to_id.is_some(), |
| 257 | notifications: channel_notifications(sidebar, &record.conversation_id), |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | fn channel_notifications(sidebar: &Sidebar, conversation_id: &str) -> ChannelNotifications { |
| 262 | sidebar |
| 263 | .teams |
| 264 | .iter() |
| 265 | .find(|team| { |
| 266 | team.channels |
| 267 | .iter() |
| 268 | .any(|channel| channel.id == conversation_id) |
| 269 | }) |
| 270 | .map(|team| team.channel_notifications(conversation_id)) |
| 271 | .unwrap_or_default() |
| 272 | } |
| 273 | |
| 274 | fn channel_hidden(sidebar: &Sidebar, conversation_id: &str) -> bool { |
| 275 | sidebar.teams.iter().any(|team| { |
| 276 | team.channels |
| 277 | .iter() |
| 278 | .any(|channel| channel.id == conversation_id) |
| 279 | && (team.hidden |
| 280 | || team |
| 281 | .hidden_channel_ids |
| 282 | .iter() |
| 283 | .any(|channel_id| channel_id == conversation_id)) |
| 284 | }) |
| 285 | } |
| 286 | |
| 287 | fn mentions_me(record: &MessageRecord, my_user_id: Option<&str>) -> bool { |
| 288 | let Some(me) = my_user_id else { |
| 289 | return false; |
| 290 | }; |
| 291 | teams_core::mentions(record) |
| 292 | .iter() |
| 293 | .any(|mention| mention.user_id.as_deref() == Some(me)) |
| 294 | } |
| 295 | |
| 296 | pub fn preview_of(record: &MessageRecord) -> Preview { |
| 297 | if let Some(text) = teams_core::preview_text(&record.body_html) { |
| 298 | return Preview::Text(text); |
| 299 | } |
| 300 | if !teams_core::images(record).is_empty() { |
| 301 | return Preview::Image; |
| 302 | } |
| 303 | let file_name = teams_core::attachments(record) |
| 304 | .into_iter() |
| 305 | .find_map(|attachment| attachment.name); |
| 306 | Preview::Text(file_name.unwrap_or_else(|| FILE_FALLBACK.to_owned())) |
| 307 | } |
| 308 | |
| 309 | #[cfg(test)] |
| 310 | mod tests { |
| 311 | use chrono::Duration; |
| 312 | use store::{ |
| 313 | ChannelNotificationLevel, ChannelRecord, ChatRecord, MemberRecord, SidebarTeam, TeamRecord, |
| 314 | }; |
| 315 | |
| 316 | use super::*; |
| 317 | |
| 318 | fn sidebar() -> Sidebar { |
| 319 | Sidebar { |
| 320 | chats: vec![ |
| 321 | ChatRecord { |
| 322 | id: "direct".into(), |
| 323 | kind: "oneOnOne".into(), |
| 324 | title: "Mara".into(), |
| 325 | ..Default::default() |
| 326 | }, |
| 327 | ChatRecord { |
| 328 | id: "group".into(), |
| 329 | kind: "group".into(), |
| 330 | title: "Team".into(), |
| 331 | members: vec![MemberRecord::default(); 5], |
| 332 | ..Default::default() |
| 333 | }, |
| 334 | ], |
| 335 | teams: vec![ |
| 336 | team("team-open", false, &["channel-open"], &[]), |
| 337 | team("team-hidden", true, &["channel-in-hidden-team"], &[]), |
| 338 | team( |
| 339 | "team-partial", |
| 340 | false, |
| 341 | &["channel-hidden", "channel-shown"], |
| 342 | &["channel-hidden"], |
| 343 | ), |
| 344 | ], |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | fn team( |
| 349 | team_id: &str, |
| 350 | hidden: bool, |
| 351 | channel_ids: &[&str], |
| 352 | hidden_channel_ids: &[&str], |
| 353 | ) -> SidebarTeam { |
| 354 | SidebarTeam { |
| 355 | rights: Default::default(), |
| 356 | team: TeamRecord { |
| 357 | id: team_id.into(), |
| 358 | name: "Team".into(), |
| 359 | }, |
| 360 | channels: channel_ids |
| 361 | .iter() |
| 362 | .map(|id| ChannelRecord { |
| 363 | id: (*id).into(), |
| 364 | team_id: team_id.into(), |
| 365 | name: "General".into(), |
| 366 | membership_type: None, |
| 367 | last_message_at: None, |
| 368 | unread: false, |
| 369 | }) |
| 370 | .collect(), |
| 371 | hidden, |
| 372 | hidden_channel_ids: hidden_channel_ids |
| 373 | .iter() |
| 374 | .map(|id| (*id).to_owned()) |
| 375 | .collect(), |
| 376 | notifications: HashMap::new(), |
| 377 | mentions: Default::default(), |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | fn collect_one(message: MessageRecord, store: &Store) -> Incoming { |
| 382 | build_incoming(&message, store, &sidebar(), Some("me")).unwrap() |
| 383 | } |
| 384 | |
| 385 | fn record(conversation: &str, sender: &str, at: DateTime<Utc>, html: &str) -> MessageRecord { |
| 386 | MessageRecord { |
| 387 | subject: None, |
| 388 | conversation_id: conversation.into(), |
| 389 | message_id: format!("{conversation}-{}", at.timestamp_millis()), |
| 390 | reply_to_id: None, |
| 391 | sender_id: Some(sender.into()), |
| 392 | sender_name: Some("Sender".into()), |
| 393 | sender_application_id: None, |
| 394 | links_json: "[]".to_owned(), |
| 395 | created_at: at, |
| 396 | edited_at: None, |
| 397 | deleted: false, |
| 398 | body_html: html.into(), |
| 399 | attachments_json: "[]".into(), |
| 400 | reactions_json: "[]".into(), |
| 401 | mentions_json: "[]".into(), |
| 402 | call_event_json: None, |
| 403 | importance: Default::default(), |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn incoming_in_a_muted_chat_is_marked_muted() { |
| 409 | let mut quiet = sidebar(); |
| 410 | quiet.chats[1].muted = true; |
| 411 | let message = record("group", "u1", Utc::now(), "hi"); |
| 412 | let store = Store::open_in_memory().unwrap(); |
| 413 | let build = |
| 414 | |sidebar: &Sidebar| build_incoming(&message, &store, sidebar, Some("me")).unwrap(); |
| 415 | assert!(build(&quiet).muted); |
| 416 | assert!(!build(&sidebar()).muted); |
| 417 | } |
| 418 | |
| 419 | #[test] |
| 420 | fn meeting_chats_know_whether_i_took_part() { |
| 421 | let mut with_meeting = sidebar(); |
| 422 | with_meeting.chats[1].kind = "meeting".into(); |
| 423 | let store = Store::open_in_memory().unwrap(); |
| 424 | let message = record("group", "u1", Utc::now(), "hi"); |
| 425 | let first = build_incoming(&message, &store, &with_meeting, Some("me")).unwrap(); |
| 426 | assert!(first.chat.meeting); |
| 427 | assert!(!first.chat.participated); |
| 428 | let own = record("group", "me", Utc::now() - Duration::minutes(1), "hello"); |
| 429 | store.upsert_messages(&[own]).unwrap(); |
| 430 | let later = build_incoming(&message, &store, &with_meeting, Some("me")).unwrap(); |
| 431 | assert!(later.chat.participated); |
| 432 | assert!( |
| 433 | !build_incoming(&message, &store, &sidebar(), Some("me")) |
| 434 | .unwrap() |
| 435 | .chat |
| 436 | .meeting |
| 437 | ); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn history_before_start_is_ignored() { |
| 442 | let start = Utc::now(); |
| 443 | let mut tracker = IncomingTracker::new(start); |
| 444 | let old = record("direct", "u1", start - Duration::minutes(5), "alt"); |
| 445 | let new = record("direct", "u1", start + Duration::seconds(5), "neu"); |
| 446 | let found = tracker.fresh( |
| 447 | vec![old, new], |
| 448 | &Store::open_in_memory().unwrap(), |
| 449 | &sidebar(), |
| 450 | Some("me"), |
| 451 | ); |
| 452 | assert_eq!(found.len(), 1); |
| 453 | assert_eq!(found[0].preview, Preview::Text("neu".into())); |
| 454 | } |
| 455 | |
| 456 | #[test] |
| 457 | fn same_message_is_not_reported_twice() { |
| 458 | let start = Utc::now(); |
| 459 | let mut tracker = IncomingTracker::new(start); |
| 460 | let message = record("direct", "u1", start + Duration::seconds(5), "neu"); |
| 461 | let store = Store::open_in_memory().unwrap(); |
| 462 | let sidebar = sidebar(); |
| 463 | let first = tracker.fresh(vec![message.clone()], &store, &sidebar, None); |
| 464 | assert_eq!(first.len(), 1); |
| 465 | assert!( |
| 466 | tracker |
| 467 | .fresh(vec![message], &store, &sidebar, None) |
| 468 | .is_empty() |
| 469 | ); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn own_messages_from_other_devices_are_skipped() { |
| 474 | let start = Utc::now(); |
| 475 | let mut tracker = IncomingTracker::new(start); |
| 476 | let own = record("direct", "me", start + Duration::seconds(5), "ich"); |
| 477 | let store = Store::open_in_memory().unwrap(); |
| 478 | let found = tracker.fresh(vec![own], &store, &sidebar(), Some("me")); |
| 479 | assert!(found.is_empty()); |
| 480 | } |
| 481 | |
| 482 | #[test] |
| 483 | fn message_first_stored_by_my_reaction_from_the_phone_is_skipped() { |
| 484 | let start = Utc::now(); |
| 485 | let mut tracker = IncomingTracker::new(start); |
| 486 | let mut reacted = record("direct", "u1", start + Duration::seconds(5), "PR link"); |
| 487 | reacted.reactions_json = |
| 488 | r#"[{"reaction_type":"👍🏼","user_id":"me","user_name":null,"created_at":null}]"#.into(); |
| 489 | let mut liked_by_other = record("group", "u1", start + Duration::seconds(6), "hi"); |
| 490 | liked_by_other.reactions_json = |
| 491 | r#"[{"reaction_type":"👍","user_id":"u2","user_name":null,"created_at":null}]"#.into(); |
| 492 | let store = Store::open_in_memory().unwrap(); |
| 493 | let found = tracker.fresh( |
| 494 | vec![reacted, liked_by_other], |
| 495 | &store, |
| 496 | &sidebar(), |
| 497 | Some("me"), |
| 498 | ); |
| 499 | assert_eq!(found.len(), 1); |
| 500 | assert_eq!(found[0].conversation_id, "group"); |
| 501 | } |
| 502 | |
| 503 | #[test] |
| 504 | fn message_stored_late_by_an_update_is_not_an_arrival() { |
| 505 | let start = Utc::now(); |
| 506 | let store = Store::open_in_memory().unwrap(); |
| 507 | let late = record("direct", "u1", start + Duration::seconds(5), "PR link"); |
| 508 | let arrived = record("direct", "u1", start + Duration::seconds(6), "neu"); |
| 509 | store.upsert_messages(&[late.clone(), arrived]).unwrap(); |
| 510 | let mut late_arrivals = LateArrivals::default(); |
| 511 | late_arrivals.insert("direct", vec![late.message_id.clone()]); |
| 512 | let found = IncomingTracker::new(start).collect( |
| 513 | &store, |
| 514 | &sidebar(), |
| 515 | Some("me"), |
| 516 | &late_arrivals, |
| 517 | "direct", |
| 518 | ); |
| 519 | assert_eq!(found.len(), 1); |
| 520 | assert_eq!(found[0].preview, Preview::Text("neu".into())); |
| 521 | } |
| 522 | |
| 523 | #[test] |
| 524 | fn late_arrivals_forget_the_oldest_beyond_the_limit() { |
| 525 | let mut late_arrivals = LateArrivals::default(); |
| 526 | late_arrivals.insert( |
| 527 | "c", |
| 528 | (0..=LATE_ARRIVALS_KEPT).map(|n| n.to_string()).collect(), |
| 529 | ); |
| 530 | assert!(!late_arrivals.contains("c", "0")); |
| 531 | assert!(late_arrivals.contains("c", "1")); |
| 532 | assert!(late_arrivals.contains("c", &LATE_ARRIVALS_KEPT.to_string())); |
| 533 | assert!(!late_arrivals.contains("other", "1")); |
| 534 | } |
| 535 | |
| 536 | #[test] |
| 537 | fn message_read_on_another_device_is_skipped() { |
| 538 | let start = Utc::now(); |
| 539 | let at = start + Duration::seconds(5); |
| 540 | let store = Store::open_in_memory().unwrap(); |
| 541 | let mut read = sidebar(); |
| 542 | read.chats[0].last_read_at = Some(at); |
| 543 | let found = IncomingTracker::new(start).fresh( |
| 544 | vec![record("direct", "u1", at, "x")], |
| 545 | &store, |
| 546 | &read, |
| 547 | Some("me"), |
| 548 | ); |
| 549 | assert!(found.is_empty()); |
| 550 | read.chats[0].last_read_at = Some(at - Duration::milliseconds(1)); |
| 551 | let found = IncomingTracker::new(start).fresh( |
| 552 | vec![record("direct", "u1", at, "x")], |
| 553 | &store, |
| 554 | &read, |
| 555 | Some("me"), |
| 556 | ); |
| 557 | assert_eq!(found.len(), 1); |
| 558 | } |
| 559 | |
| 560 | #[test] |
| 561 | fn message_answered_from_another_device_is_skipped() { |
| 562 | let start = Utc::now(); |
| 563 | let theirs = record("direct", "u1", start + Duration::seconds(5), "frage"); |
| 564 | let mine = record("direct", "me", start + Duration::seconds(9), "antwort"); |
| 565 | let store = Store::open_in_memory().unwrap(); |
| 566 | let found = IncomingTracker::new(start).fresh( |
| 567 | vec![theirs.clone(), mine], |
| 568 | &store, |
| 569 | &sidebar(), |
| 570 | Some("me"), |
| 571 | ); |
| 572 | assert!(found.is_empty()); |
| 573 | let found = IncomingTracker::new(start).fresh(vec![theirs], &store, &sidebar(), Some("me")); |
| 574 | assert_eq!(found.len(), 1); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn detects_mention_of_me_and_group_size() { |
| 579 | let start = Utc::now(); |
| 580 | let mut message = record( |
| 581 | "group", |
| 582 | "u1", |
| 583 | start + Duration::seconds(5), |
| 584 | "<at>Me</at> hi", |
| 585 | ); |
| 586 | message.mentions_json = r#"[{"user_id":"me","name":"Me"}]"#.into(); |
| 587 | let found = collect_one(message.clone(), &Store::open_in_memory().unwrap()); |
| 588 | assert!(found.mentions_me); |
| 589 | assert_eq!(found.kind, ChatKind::Group { member_count: 5 }); |
| 590 | let other = build_incoming( |
| 591 | &message, |
| 592 | &Store::open_in_memory().unwrap(), |
| 593 | &sidebar(), |
| 594 | Some("other"), |
| 595 | ); |
| 596 | assert!(!other.unwrap().mentions_me); |
| 597 | } |
| 598 | |
| 599 | #[test] |
| 600 | fn everyone_in_a_group_chat_counts_as_a_mention_but_a_channel_mention_does_not() { |
| 601 | let store = Store::open_in_memory().unwrap(); |
| 602 | let mut message = record("group", "u1", Utc::now(), "<at>Everyone</at>"); |
| 603 | message.mentions_json = |
| 604 | r#"[{"user_id":null,"name":"Everyone","target_id":"group","group":true}]"#.into(); |
| 605 | assert!(collect_one(message, &store).mentions_me); |
| 606 | let mut post = record("channel-open", "u1", Utc::now(), "<at>channel</at>"); |
| 607 | post.mentions_json = |
| 608 | r#"[{"user_id":null,"name":"channel","target_id":"channel-open","group":true}]"#.into(); |
| 609 | assert!(!collect_one(post, &store).mentions_me); |
| 610 | } |
| 611 | |
| 612 | #[test] |
| 613 | fn unknown_conversation_yields_nothing() { |
| 614 | let message = record("nowhere", "u1", Utc::now(), "x"); |
| 615 | assert!( |
| 616 | build_incoming( |
| 617 | &message, |
| 618 | &Store::open_in_memory().unwrap(), |
| 619 | &sidebar(), |
| 620 | None |
| 621 | ) |
| 622 | .is_none() |
| 623 | ); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn empty_body_without_image_falls_back_to_file_label() { |
| 628 | let message = record("direct", "u1", Utc::now(), ""); |
| 629 | let found = build_incoming( |
| 630 | &message, |
| 631 | &Store::open_in_memory().unwrap(), |
| 632 | &sidebar(), |
| 633 | None, |
| 634 | ) |
| 635 | .unwrap(); |
| 636 | assert_eq!(found.preview, Preview::Text(FILE_FALLBACK.into())); |
| 637 | } |
| 638 | |
| 639 | #[test] |
| 640 | fn group_mention_sets_channel_signal() { |
| 641 | let store = Store::open_in_memory().unwrap(); |
| 642 | let mut message = record("channel-open", "u1", Utc::now(), "<at>channel</at>"); |
| 643 | message.mentions_json = |
| 644 | r#"[{"user_id":null,"name":"channel","target_id":"channel-open","group":true}]"#.into(); |
| 645 | assert!( |
| 646 | collect_one(message.clone(), &store) |
| 647 | .signals |
| 648 | .mentions_channel |
| 649 | ); |
| 650 | message.mentions_json = r#"[{"user_id":null,"name":"bot","target_id":"bot-1"}]"#.into(); |
| 651 | assert!(!collect_one(message, &store).signals.mentions_channel); |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | fn reply_in_my_thread_sets_signal() { |
| 656 | let store = Store::open_in_memory().unwrap(); |
| 657 | let start = Utc::now(); |
| 658 | let mut root = record("channel-open", "me", start, "root"); |
| 659 | root.message_id = "root".into(); |
| 660 | store.upsert_messages(&[root]).unwrap(); |
| 661 | let mut reply = record("channel-open", "u1", start + Duration::seconds(1), "reply"); |
| 662 | reply.reply_to_id = Some("root".into()); |
| 663 | assert!(collect_one(reply.clone(), &store).signals.in_my_thread); |
| 664 | reply.reply_to_id = Some("unknown-root".into()); |
| 665 | assert!(!collect_one(reply, &store).signals.in_my_thread); |
| 666 | let top_level = record("channel-open", "u1", start, "post"); |
| 667 | assert!(!collect_one(top_level, &store).signals.in_my_thread); |
| 668 | } |
| 669 | |
| 670 | #[test] |
| 671 | fn hidden_channels_are_flagged() { |
| 672 | let store = Store::open_in_memory().unwrap(); |
| 673 | let signals = |
| 674 | |channel: &str| collect_one(record(channel, "u1", Utc::now(), "x"), &store).signals; |
| 675 | assert!(!signals("channel-open").hidden); |
| 676 | assert!(signals("channel-in-hidden-team").hidden); |
| 677 | assert!(signals("channel-hidden").hidden); |
| 678 | assert!(!signals("channel-shown").hidden); |
| 679 | } |
| 680 | |
| 681 | #[test] |
| 682 | fn channel_notifications_and_reply_flag_come_from_the_sidebar() { |
| 683 | let store = Store::open_in_memory().unwrap(); |
| 684 | let banner = ChannelNotifications { |
| 685 | level: ChannelNotificationLevel::BannerAndFeed, |
| 686 | include_replies: true, |
| 687 | }; |
| 688 | let mut configured = sidebar(); |
| 689 | configured.teams[0] |
| 690 | .notifications |
| 691 | .insert("channel-open".into(), banner); |
| 692 | let mut reply = record("channel-open", "u1", Utc::now(), "x"); |
| 693 | let build = |message: &MessageRecord| { |
| 694 | build_incoming(message, &store, &configured, Some("me")) |
| 695 | .unwrap() |
| 696 | .signals |
| 697 | }; |
| 698 | assert_eq!(build(&reply).notifications, banner); |
| 699 | assert!(!build(&reply).is_reply); |
| 700 | reply.reply_to_id = Some("root".into()); |
| 701 | assert!(build(&reply).is_reply); |
| 702 | let other = record("channel-shown", "u1", Utc::now(), "x"); |
| 703 | assert_eq!(build(&other).notifications, ChannelNotifications::default()); |
| 704 | } |
| 705 | |
| 706 | #[test] |
| 707 | fn chats_carry_no_channel_signals() { |
| 708 | let store = Store::open_in_memory().unwrap(); |
| 709 | let found = collect_one(record("group", "u1", Utc::now(), "x"), &store); |
| 710 | assert_eq!(found.signals, ChannelSignals::default()); |
| 711 | } |
| 712 | } |