BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / notify / urgent_repeat.rs

urgent_repeat.rs

urgent_repeat.rs 5.5 KiB Download Raw
1use chrono::{DateTime, Duration, Utc};
2
3use super::rules::Incoming;
4
5pub const REPEAT_INTERVAL_MINUTES: i64 = 2;
6pub const REPEAT_WINDOW_MINUTES: i64 = 20;
7const REPEAT_COUNT: i64 = REPEAT_WINDOW_MINUTES / REPEAT_INTERVAL_MINUTES;
8
9struct Pending {
10 incoming: Incoming,
11 repeats_sent: i64,
12}
13
14/// Teams notifies again every 2 minutes for 20 minutes until an urgent message is read.
15#[derive(Default)]
16pub struct UrgentRepeats {
17 pending: Vec<Pending>,
18}
19
20impl UrgentRepeats {
21 pub fn track(&mut self, incoming: &Incoming, now: DateTime<Utc>) {
22 let already_tracked = self.pending.iter().any(|pending| {
23 pending.incoming.conversation_id == incoming.conversation_id
24 && pending.incoming.message_id == incoming.message_id
25 });
26 if !incoming.is_urgent() || already_tracked || expired(incoming.created_at, now) {
27 return;
28 }
29 self.pending.push(Pending {
30 incoming: incoming.clone(),
31 repeats_sent: 0,
32 });
33 }
34
35 pub fn stop_conversation(&mut self, conversation_id: &str) {
36 self.pending
37 .retain(|pending| pending.incoming.conversation_id != conversation_id);
38 }
39
40 pub fn stop_read(&mut self, read_until: impl Fn(&str) -> Option<DateTime<Utc>>) {
41 self.pending.retain(|pending| {
42 read_until(&pending.incoming.conversation_id)
43 .is_none_or(|read_at| read_at < pending.incoming.created_at)
44 });
45 }
46
47 pub fn due(&mut self, now: DateTime<Utc>) -> Vec<Incoming> {
48 let mut repeats = Vec::new();
49 self.pending.retain_mut(|pending| {
50 if expired(pending.incoming.created_at, now) {
51 return false;
52 }
53 let slot = elapsed_slots(pending.incoming.created_at, now).min(REPEAT_COUNT);
54 if slot > pending.repeats_sent {
55 pending.repeats_sent = slot;
56 repeats.push(pending.incoming.clone());
57 }
58 pending.repeats_sent < REPEAT_COUNT
59 });
60 repeats
61 }
62
63 pub fn is_empty(&self) -> bool {
64 self.pending.is_empty()
65 }
66}
67
68fn elapsed_slots(created_at: DateTime<Utc>, now: DateTime<Utc>) -> i64 {
69 let elapsed = now - created_at;
70 if elapsed < Duration::zero() {
71 return 0;
72 }
73 elapsed.num_seconds() / (REPEAT_INTERVAL_MINUTES * 60)
74}
75
76fn expired(created_at: DateTime<Utc>, now: DateTime<Utc>) -> bool {
77 now - created_at >= Duration::minutes(REPEAT_WINDOW_MINUTES + REPEAT_INTERVAL_MINUTES)
78}
79
80#[cfg(test)]
81mod tests {
82 use chrono::TimeZone;
83 use store::MessageImportance;
84
85 use super::*;
86 use crate::notify::rules::{ChatKind, Preview};
87
88 fn sent_at() -> DateTime<Utc> {
89 Utc.with_ymd_and_hms(2026, 10, 17, 9, 0, 0).unwrap()
90 }
91
92 fn minutes(count: i64) -> DateTime<Utc> {
93 sent_at() + Duration::minutes(count)
94 }
95
96 fn incoming(importance: MessageImportance) -> Incoming {
97 Incoming {
98 conversation_id: "chat".into(),
99 message_id: "m1".into(),
100 kind: ChatKind::Direct,
101 chat_title: "Lea".into(),
102 sender_id: Some("lea".into()),
103 sender_name: "Lea".into(),
104 preview: Preview::Text("Server down".into()),
105 mentions_me: false,
106 muted: false,
107 signals: Default::default(),
108 chat: Default::default(),
109 created_at: sent_at(),
110 importance,
111 }
112 }
113
114 fn tracked() -> UrgentRepeats {
115 let mut repeats = UrgentRepeats::default();
116 repeats.track(&incoming(MessageImportance::Urgent), sent_at());
117 repeats
118 }
119
120 #[test]
121 fn repeats_every_two_minutes_for_twenty_minutes() {
122 let mut repeats = tracked();
123 let mut pings = Vec::new();
124 for second in (0..=25 * 60).step_by(10) {
125 let now = sent_at() + Duration::seconds(second);
126 if !repeats.due(now).is_empty() {
127 pings.push(second / 60);
128 }
129 }
130 assert_eq!(pings, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
131 assert!(repeats.is_empty());
132 }
133
134 #[test]
135 fn only_urgent_messages_repeat() {
136 let mut repeats = UrgentRepeats::default();
137 repeats.track(&incoming(MessageImportance::High), sent_at());
138 repeats.track(&incoming(MessageImportance::Normal), sent_at());
139 assert!(repeats.is_empty());
140 }
141
142 #[test]
143 fn tracking_twice_keeps_one_schedule() {
144 let mut repeats = tracked();
145 repeats.track(&incoming(MessageImportance::Urgent), minutes(1));
146 assert_eq!(repeats.due(minutes(2)).len(), 1);
147 }
148
149 #[test]
150 fn a_missed_tick_pings_once_not_in_a_burst() {
151 let mut repeats = tracked();
152 assert_eq!(repeats.due(minutes(9)).len(), 1);
153 assert!(repeats.due(minutes(9)).is_empty());
154 assert_eq!(repeats.due(minutes(10)).len(), 1);
155 }
156
157 #[test]
158 fn opening_the_chat_stops_the_repeats() {
159 let mut repeats = tracked();
160 repeats.stop_conversation("chat");
161 assert!(repeats.due(minutes(2)).is_empty());
162 assert!(repeats.is_empty());
163 }
164
165 #[test]
166 fn reading_on_another_device_stops_the_repeats() {
167 let mut repeats = tracked();
168 repeats.stop_read(|_| Some(minutes(-1)));
169 assert!(!repeats.is_empty());
170 repeats.stop_read(|_| Some(sent_at()));
171 assert!(repeats.is_empty());
172 }
173
174 #[test]
175 fn a_message_older_than_the_window_is_not_tracked() {
176 let mut repeats = UrgentRepeats::default();
177 repeats.track(&incoming(MessageImportance::Urgent), minutes(30));
178 assert!(repeats.is_empty());
179 }
180}