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 created_at: sent_at(),
109 importance,
110 }
111 }
112
113 fn tracked() -> UrgentRepeats {
114 let mut repeats = UrgentRepeats::default();
115 repeats.track(&incoming(MessageImportance::Urgent), sent_at());
116 repeats
117 }
118
119 #[test]
120 fn repeats_every_two_minutes_for_twenty_minutes() {
121 let mut repeats = tracked();
122 let mut pings = Vec::new();
123 for second in (0..=25 * 60).step_by(10) {
124 let now = sent_at() + Duration::seconds(second);
125 if !repeats.due(now).is_empty() {
126 pings.push(second / 60);
127 }
128 }
129 assert_eq!(pings, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
130 assert!(repeats.is_empty());
131 }
132
133 #[test]
134 fn only_urgent_messages_repeat() {
135 let mut repeats = UrgentRepeats::default();
136 repeats.track(&incoming(MessageImportance::High), sent_at());
137 repeats.track(&incoming(MessageImportance::Normal), sent_at());
138 assert!(repeats.is_empty());
139 }
140
141 #[test]
142 fn tracking_twice_keeps_one_schedule() {
143 let mut repeats = tracked();
144 repeats.track(&incoming(MessageImportance::Urgent), minutes(1));
145 assert_eq!(repeats.due(minutes(2)).len(), 1);
146 }
147
148 #[test]
149 fn a_missed_tick_pings_once_not_in_a_burst() {
150 let mut repeats = tracked();
151 assert_eq!(repeats.due(minutes(9)).len(), 1);
152 assert!(repeats.due(minutes(9)).is_empty());
153 assert_eq!(repeats.due(minutes(10)).len(), 1);
154 }
155
156 #[test]
157 fn opening_the_chat_stops_the_repeats() {
158 let mut repeats = tracked();
159 repeats.stop_conversation("chat");
160 assert!(repeats.due(minutes(2)).is_empty());
161 assert!(repeats.is_empty());
162 }
163
164 #[test]
165 fn reading_on_another_device_stops_the_repeats() {
166 let mut repeats = tracked();
167 repeats.stop_read(|_| Some(minutes(-1)));
168 assert!(!repeats.is_empty());
169 repeats.stop_read(|_| Some(sent_at()));
170 assert!(repeats.is_empty());
171 }
172
173 #[test]
174 fn a_message_older_than_the_window_is_not_tracked() {
175 let mut repeats = UrgentRepeats::default();
176 repeats.track(&incoming(MessageImportance::Urgent), minutes(30));
177 assert!(repeats.is_empty());
178 }
179}