| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use chrono::{DateTime, Duration, Utc}; |
| 4 | use gpui_kit::Context; |
| 5 | use store::{MessageImportance, MessageRecord}; |
| 6 | |
| 7 | use crate::app_state::AppState; |
| 8 | use crate::notify::urgent_repeat::{REPEAT_INTERVAL_MINUTES, REPEAT_WINDOW_MINUTES}; |
| 9 | use crate::runtime; |
| 10 | |
| 11 | const RECENT_MESSAGES: usize = 30; |
| 12 | |
| 13 | /// Teams acknowledges an urgent message from someone else once it is shown; after the repeat window there is nothing left to stop. |
| 14 | pub fn urgent_to_ack( |
| 15 | records: &[MessageRecord], |
| 16 | my_user_id: &str, |
| 17 | now: DateTime<Utc>, |
| 18 | acked: &HashSet<String>, |
| 19 | ) -> Vec<String> { |
| 20 | let oldest = now - Duration::minutes(REPEAT_WINDOW_MINUTES + REPEAT_INTERVAL_MINUTES); |
| 21 | records |
| 22 | .iter() |
| 23 | .filter(|record| { |
| 24 | record.importance == MessageImportance::Urgent |
| 25 | && !record.deleted |
| 26 | && record.created_at >= oldest |
| 27 | && record |
| 28 | .sender_id |
| 29 | .as_deref() |
| 30 | .is_some_and(|sender| sender != my_user_id) |
| 31 | && !acked.contains(&record.message_id) |
| 32 | }) |
| 33 | .map(|record| record.message_id.clone()) |
| 34 | .collect() |
| 35 | } |
| 36 | |
| 37 | impl AppState { |
| 38 | pub fn ack_urgent_messages(&mut self, chat_id: &str, _cx: &mut Context<Self>) { |
| 39 | if self.mode.demo || self.mode.read_only { |
| 40 | return; |
| 41 | } |
| 42 | let (Some(engine), Some(me)) = (self.engine.clone(), self.directory.me.as_ref()) else { |
| 43 | return; |
| 44 | }; |
| 45 | let Ok(records) = self.store.messages(chat_id, None, RECENT_MESSAGES) else { |
| 46 | return; |
| 47 | }; |
| 48 | let message_ids = urgent_to_ack(&records, &me.user_id, Utc::now(), &self.acked_urgent); |
| 49 | if message_ids.is_empty() { |
| 50 | return; |
| 51 | } |
| 52 | self.acked_urgent.extend(message_ids.iter().cloned()); |
| 53 | let chat_id = chat_id.to_owned(); |
| 54 | drop(runtime::spawn(async move { |
| 55 | for message_id in message_ids { |
| 56 | let _ = engine.ack_urgent_message(&chat_id, &message_id).await; |
| 57 | } |
| 58 | })); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[cfg(test)] |
| 63 | mod tests { |
| 64 | use chrono::TimeZone; |
| 65 | |
| 66 | use super::*; |
| 67 | |
| 68 | fn now() -> DateTime<Utc> { |
| 69 | Utc.with_ymd_and_hms(2026, 10, 17, 9, 30, 0).unwrap() |
| 70 | } |
| 71 | |
| 72 | fn record( |
| 73 | id: &str, |
| 74 | sender: &str, |
| 75 | minutes_ago: i64, |
| 76 | importance: MessageImportance, |
| 77 | ) -> MessageRecord { |
| 78 | MessageRecord { |
| 79 | conversation_id: "chat".into(), |
| 80 | message_id: id.into(), |
| 81 | sender_id: Some(sender.into()), |
| 82 | created_at: now() - Duration::minutes(minutes_ago), |
| 83 | importance, |
| 84 | ..Default::default() |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn acks_recent_urgent_messages_from_others_once() { |
| 90 | let records = [ |
| 91 | record("urgent", "lea", 3, MessageImportance::Urgent), |
| 92 | record("own", "me", 3, MessageImportance::Urgent), |
| 93 | record("important", "lea", 3, MessageImportance::High), |
| 94 | record("old", "lea", 40, MessageImportance::Urgent), |
| 95 | record("done", "lea", 1, MessageImportance::Urgent), |
| 96 | ]; |
| 97 | let acked = HashSet::from(["done".to_owned()]); |
| 98 | assert_eq!(urgent_to_ack(&records, "me", now(), &acked), ["urgent"]); |
| 99 | } |
| 100 | } |