BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / rules.rs

rules.rs

rules.rs 8.7 KiB Download Raw
1use std::collections::HashMap;
2use std::time::{Duration, Instant};
3
4use chrono::{DateTime, Utc};
5
6pub const SOUND_THROTTLE: Duration = Duration::from_secs(10);
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum Corner {
10 TopLeft,
11 TopRight,
12 BottomLeft,
13 #[default]
14 BottomRight,
15}
16
17impl Corner {
18 pub const ALL: [Corner; 4] = [
19 Corner::TopLeft,
20 Corner::TopRight,
21 Corner::BottomLeft,
22 Corner::BottomRight,
23 ];
24
25 pub fn label(self) -> &'static str {
26 match self {
27 Corner::TopLeft => "Top left",
28 Corner::TopRight => "Top right",
29 Corner::BottomLeft => "Bottom left",
30 Corner::BottomRight => "Bottom right",
31 }
32 }
33
34 pub fn key(self) -> &'static str {
35 match self {
36 Corner::TopLeft => "top_left",
37 Corner::TopRight => "top_right",
38 Corner::BottomLeft => "bottom_left",
39 Corner::BottomRight => "bottom_right",
40 }
41 }
42
43 pub fn from_key(key: &str) -> Option<Corner> {
44 Corner::ALL.into_iter().find(|corner| corner.key() == key)
45 }
46
47 pub fn is_bottom(self) -> bool {
48 matches!(self, Corner::BottomLeft | Corner::BottomRight)
49 }
50
51 pub fn is_right(self) -> bool {
52 matches!(self, Corner::TopRight | Corner::BottomRight)
53 }
54
55 pub fn next(self) -> Corner {
56 let index = Corner::ALL
57 .iter()
58 .position(|corner| *corner == self)
59 .unwrap_or(0);
60 Corner::ALL[(index + 1) % Corner::ALL.len()]
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct Settings {
66 pub sound: bool,
67 pub mentions_only: bool,
68 pub preview: bool,
69 pub corner: Corner,
70 pub flash: bool,
71 pub close_to_tray: bool,
72 pub do_not_disturb: bool,
73}
74
75impl Default for Settings {
76 fn default() -> Self {
77 Settings {
78 sound: true,
79 mentions_only: false,
80 preview: true,
81 corner: Corner::BottomRight,
82 flash: true,
83 close_to_tray: true,
84 do_not_disturb: false,
85 }
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
90pub enum ChatKind {
91 Direct,
92 Group { member_count: usize },
93 Channel { team: String, channel: String },
94}
95
96#[derive(Debug, Clone, PartialEq, Eq)]
97pub enum Preview {
98 Text(String),
99 Image,
100}
101
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub struct Incoming {
104 pub conversation_id: String,
105 pub message_id: String,
106 pub kind: ChatKind,
107 pub chat_title: String,
108 pub sender_id: Option<String>,
109 pub sender_name: String,
110 pub preview: Preview,
111 pub mentions_me: bool,
112 pub muted: bool,
113 pub created_at: DateTime<Utc>,
114}
115
116impl Incoming {
117 pub fn is_direct(&self) -> bool {
118 self.kind == ChatKind::Direct
119 }
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
123pub struct Environment {
124 pub chat_in_foreground: bool,
125 pub system_quiet: bool,
126 pub own_do_not_disturb: bool,
127 pub in_call: bool,
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub struct Decision {
132 pub toast: bool,
133 pub sound: bool,
134}
135
136const SILENT: Decision = Decision {
137 toast: false,
138 sound: false,
139};
140
141pub fn decide(settings: &Settings, incoming: &Incoming, environment: Environment) -> Decision {
142 let suppressed = environment.chat_in_foreground
143 || environment.system_quiet
144 || environment.own_do_not_disturb
145 || settings.do_not_disturb
146 || (incoming.muted && !incoming.mentions_me)
147 || (settings.mentions_only && !incoming.mentions_me && !incoming.is_direct());
148 if suppressed {
149 return SILENT;
150 }
151 Decision {
152 toast: true,
153 sound: settings.sound && !environment.in_call,
154 }
155}
156
157#[derive(Default)]
158pub struct SoundThrottle {
159 last_played: HashMap<String, Instant>,
160}
161
162impl SoundThrottle {
163 pub fn allow(&mut self, conversation_id: &str, now: Instant) -> bool {
164 let allowed = self
165 .last_played
166 .get(conversation_id)
167 .is_none_or(|last| now.duration_since(*last) >= SOUND_THROTTLE);
168 if allowed {
169 self.last_played.insert(conversation_id.to_owned(), now);
170 }
171 allowed
172 }
173}
174
175pub fn should_flash(settings: &Settings, toast_shown: bool, window_active: bool) -> bool {
176 settings.flash && toast_shown && !window_active
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182
183 fn incoming(kind: ChatKind) -> Incoming {
184 Incoming {
185 conversation_id: "chat".into(),
186 message_id: "1".into(),
187 kind,
188 chat_title: "Chat".into(),
189 sender_id: Some("u1".into()),
190 sender_name: "Mara".into(),
191 preview: Preview::Text("Hello".into()),
192 mentions_me: false,
193 muted: false,
194 created_at: Utc::now(),
195 }
196 }
197
198 fn group() -> Incoming {
199 incoming(ChatKind::Group { member_count: 5 })
200 }
201
202 #[test]
203 fn plain_message_gets_toast_and_sound() {
204 let decision = decide(&Settings::default(), &group(), Environment::default());
205 assert_eq!(
206 decision,
207 Decision {
208 toast: true,
209 sound: true
210 }
211 );
212 }
213
214 #[test]
215 fn foreground_chat_is_silent() {
216 let environment = Environment {
217 chat_in_foreground: true,
218 ..Default::default()
219 };
220 assert_eq!(decide(&Settings::default(), &group(), environment), SILENT);
221 }
222
223 #[test]
224 fn focus_assist_and_dnd_are_silent() {
225 for environment in [
226 Environment {
227 system_quiet: true,
228 ..Default::default()
229 },
230 Environment {
231 own_do_not_disturb: true,
232 ..Default::default()
233 },
234 ] {
235 assert_eq!(decide(&Settings::default(), &group(), environment), SILENT);
236 }
237 let settings = Settings {
238 do_not_disturb: true,
239 ..Default::default()
240 };
241 assert_eq!(decide(&settings, &group(), Environment::default()), SILENT);
242 }
243
244 #[test]
245 fn call_keeps_toast_but_drops_sound() {
246 let environment = Environment {
247 in_call: true,
248 ..Default::default()
249 };
250 let decision = decide(&Settings::default(), &group(), environment);
251 assert_eq!(
252 decision,
253 Decision {
254 toast: true,
255 sound: false
256 }
257 );
258 }
259
260 #[test]
261 fn muted_chat_only_notifies_for_mentions() {
262 let mut muted = group();
263 muted.muted = true;
264 assert_eq!(
265 decide(&Settings::default(), &muted, Environment::default()),
266 SILENT
267 );
268 muted.mentions_me = true;
269 assert!(decide(&Settings::default(), &muted, Environment::default()).toast);
270 }
271
272 #[test]
273 fn mentions_only_keeps_direct_and_mentions() {
274 let settings = Settings {
275 mentions_only: true,
276 ..Default::default()
277 };
278 assert_eq!(decide(&settings, &group(), Environment::default()), SILENT);
279 assert!(decide(&settings, &incoming(ChatKind::Direct), Environment::default()).toast);
280 let mut mention = group();
281 mention.mentions_me = true;
282 assert!(decide(&settings, &mention, Environment::default()).toast);
283 }
284
285 #[test]
286 fn sound_setting_off_keeps_toast() {
287 let settings = Settings {
288 sound: false,
289 ..Default::default()
290 };
291 let decision = decide(&settings, &group(), Environment::default());
292 assert_eq!(
293 decision,
294 Decision {
295 toast: true,
296 sound: false
297 }
298 );
299 }
300
301 #[test]
302 fn throttle_allows_once_per_window_per_chat() {
303 let mut throttle = SoundThrottle::default();
304 let start = Instant::now();
305 assert!(throttle.allow("a", start));
306 assert!(!throttle.allow("a", start + Duration::from_secs(9)));
307 assert!(throttle.allow("b", start + Duration::from_secs(9)));
308 assert!(throttle.allow("a", start + Duration::from_secs(10)));
309 }
310
311 #[test]
312 fn flash_needs_toast_inactive_window_and_setting() {
313 let settings = Settings::default();
314 assert!(should_flash(&settings, true, false));
315 assert!(!should_flash(&settings, true, true));
316 assert!(!should_flash(&settings, false, false));
317 let off = Settings {
318 flash: false,
319 ..Default::default()
320 };
321 assert!(!should_flash(&off, true, false));
322 }
323
324 #[test]
325 fn corner_keys_round_trip() {
326 for corner in Corner::ALL {
327 assert_eq!(Corner::from_key(corner.key()), Some(corner));
328 }
329 assert_eq!(Corner::BottomRight.next(), Corner::TopLeft);
330 }
331}