| 1 | use std::collections::HashMap; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use chrono::{DateTime, Utc}; |
| 5 | |
| 6 | pub const SOUND_THROTTLE: Duration = Duration::from_secs(10); |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 9 | pub enum Corner { |
| 10 | TopLeft, |
| 11 | TopRight, |
| 12 | BottomLeft, |
| 13 | #[default] |
| 14 | BottomRight, |
| 15 | } |
| 16 | |
| 17 | impl 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)] |
| 65 | pub 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 | |
| 75 | impl 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)] |
| 90 | pub enum ChatKind { |
| 91 | Direct, |
| 92 | Group { member_count: usize }, |
| 93 | Channel { team: String, channel: String }, |
| 94 | } |
| 95 | |
| 96 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 97 | pub enum Preview { |
| 98 | Text(String), |
| 99 | Image, |
| 100 | } |
| 101 | |
| 102 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 103 | pub struct ChannelSignals { |
| 104 | pub mentions_channel: bool, |
| 105 | pub in_my_thread: bool, |
| 106 | pub hidden: bool, |
| 107 | pub followed: bool, |
| 108 | } |
| 109 | |
| 110 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 111 | pub struct Incoming { |
| 112 | pub conversation_id: String, |
| 113 | pub message_id: String, |
| 114 | pub kind: ChatKind, |
| 115 | pub chat_title: String, |
| 116 | pub sender_id: Option<String>, |
| 117 | pub sender_name: String, |
| 118 | pub preview: Preview, |
| 119 | pub mentions_me: bool, |
| 120 | pub muted: bool, |
| 121 | pub signals: ChannelSignals, |
| 122 | pub created_at: DateTime<Utc>, |
| 123 | } |
| 124 | |
| 125 | impl Incoming { |
| 126 | pub fn is_direct(&self) -> bool { |
| 127 | self.kind == ChatKind::Direct |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 132 | pub struct Environment { |
| 133 | pub chat_in_foreground: bool, |
| 134 | pub system_quiet: bool, |
| 135 | pub own_do_not_disturb: bool, |
| 136 | pub in_call: bool, |
| 137 | } |
| 138 | |
| 139 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 140 | pub struct Decision { |
| 141 | pub toast: bool, |
| 142 | pub sound: bool, |
| 143 | } |
| 144 | |
| 145 | pub fn channel_alerts(incoming: &Incoming) -> bool { |
| 146 | if !matches!(incoming.kind, ChatKind::Channel { .. }) || incoming.mentions_me { |
| 147 | return true; |
| 148 | } |
| 149 | let signals = incoming.signals; |
| 150 | !signals.hidden && (signals.mentions_channel || signals.in_my_thread || signals.followed) |
| 151 | } |
| 152 | |
| 153 | const SILENT: Decision = Decision { |
| 154 | toast: false, |
| 155 | sound: false, |
| 156 | }; |
| 157 | |
| 158 | pub fn decide(settings: &Settings, incoming: &Incoming, environment: Environment) -> Decision { |
| 159 | let suppressed = !channel_alerts(incoming) |
| 160 | || environment.chat_in_foreground |
| 161 | || environment.system_quiet |
| 162 | || environment.own_do_not_disturb |
| 163 | || settings.do_not_disturb |
| 164 | || (incoming.muted && !incoming.mentions_me) |
| 165 | || (settings.mentions_only && !incoming.mentions_me && !incoming.is_direct()); |
| 166 | if suppressed { |
| 167 | return SILENT; |
| 168 | } |
| 169 | Decision { |
| 170 | toast: true, |
| 171 | sound: settings.sound && !environment.in_call, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #[derive(Default)] |
| 176 | pub struct SoundThrottle { |
| 177 | last_played: HashMap<String, Instant>, |
| 178 | } |
| 179 | |
| 180 | impl SoundThrottle { |
| 181 | pub fn allow(&mut self, conversation_id: &str, now: Instant) -> bool { |
| 182 | let allowed = self |
| 183 | .last_played |
| 184 | .get(conversation_id) |
| 185 | .is_none_or(|last| now.duration_since(*last) >= SOUND_THROTTLE); |
| 186 | if allowed { |
| 187 | self.last_played.insert(conversation_id.to_owned(), now); |
| 188 | } |
| 189 | allowed |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | pub fn should_flash(settings: &Settings, toast_shown: bool, window_active: bool) -> bool { |
| 194 | settings.flash && toast_shown && !window_active |
| 195 | } |
| 196 | |
| 197 | #[cfg(test)] |
| 198 | mod tests { |
| 199 | use super::*; |
| 200 | |
| 201 | fn incoming(kind: ChatKind) -> Incoming { |
| 202 | Incoming { |
| 203 | conversation_id: "chat".into(), |
| 204 | message_id: "1".into(), |
| 205 | kind, |
| 206 | chat_title: "Chat".into(), |
| 207 | sender_id: Some("u1".into()), |
| 208 | sender_name: "Mara".into(), |
| 209 | preview: Preview::Text("Hello".into()), |
| 210 | mentions_me: false, |
| 211 | muted: false, |
| 212 | signals: ChannelSignals::default(), |
| 213 | created_at: Utc::now(), |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn channel() -> Incoming { |
| 218 | incoming(ChatKind::Channel { |
| 219 | team: "Team".into(), |
| 220 | channel: "General".into(), |
| 221 | }) |
| 222 | } |
| 223 | |
| 224 | fn group() -> Incoming { |
| 225 | incoming(ChatKind::Group { member_count: 5 }) |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn plain_message_gets_toast_and_sound() { |
| 230 | let decision = decide(&Settings::default(), &group(), Environment::default()); |
| 231 | assert_eq!( |
| 232 | decision, |
| 233 | Decision { |
| 234 | toast: true, |
| 235 | sound: true |
| 236 | } |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn foreground_chat_is_silent() { |
| 242 | let environment = Environment { |
| 243 | chat_in_foreground: true, |
| 244 | ..Default::default() |
| 245 | }; |
| 246 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn focus_assist_and_dnd_are_silent() { |
| 251 | for environment in [ |
| 252 | Environment { |
| 253 | system_quiet: true, |
| 254 | ..Default::default() |
| 255 | }, |
| 256 | Environment { |
| 257 | own_do_not_disturb: true, |
| 258 | ..Default::default() |
| 259 | }, |
| 260 | ] { |
| 261 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 262 | } |
| 263 | let settings = Settings { |
| 264 | do_not_disturb: true, |
| 265 | ..Default::default() |
| 266 | }; |
| 267 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn call_keeps_toast_but_drops_sound() { |
| 272 | let environment = Environment { |
| 273 | in_call: true, |
| 274 | ..Default::default() |
| 275 | }; |
| 276 | let decision = decide(&Settings::default(), &group(), environment); |
| 277 | assert_eq!( |
| 278 | decision, |
| 279 | Decision { |
| 280 | toast: true, |
| 281 | sound: false |
| 282 | } |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn muted_chat_only_notifies_for_mentions() { |
| 288 | let mut muted = group(); |
| 289 | muted.muted = true; |
| 290 | assert_eq!( |
| 291 | decide(&Settings::default(), &muted, Environment::default()), |
| 292 | SILENT |
| 293 | ); |
| 294 | muted.mentions_me = true; |
| 295 | assert!(decide(&Settings::default(), &muted, Environment::default()).toast); |
| 296 | } |
| 297 | |
| 298 | #[test] |
| 299 | fn mentions_only_keeps_direct_and_mentions() { |
| 300 | let settings = Settings { |
| 301 | mentions_only: true, |
| 302 | ..Default::default() |
| 303 | }; |
| 304 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 305 | assert!(decide(&settings, &incoming(ChatKind::Direct), Environment::default()).toast); |
| 306 | let mut mention = group(); |
| 307 | mention.mentions_me = true; |
| 308 | assert!(decide(&settings, &mention, Environment::default()).toast); |
| 309 | } |
| 310 | |
| 311 | #[test] |
| 312 | fn plain_channel_post_is_silent() { |
| 313 | assert!(!channel_alerts(&channel())); |
| 314 | assert_eq!( |
| 315 | decide(&Settings::default(), &channel(), Environment::default()), |
| 316 | SILENT |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn channel_alerts_for_each_trigger() { |
| 322 | let triggers: [fn(&mut Incoming); 4] = [ |
| 323 | |incoming| incoming.mentions_me = true, |
| 324 | |incoming| incoming.signals.mentions_channel = true, |
| 325 | |incoming| incoming.signals.in_my_thread = true, |
| 326 | |incoming| incoming.signals.followed = true, |
| 327 | ]; |
| 328 | for trigger in triggers { |
| 329 | let mut message = channel(); |
| 330 | trigger(&mut message); |
| 331 | assert!(channel_alerts(&message)); |
| 332 | assert!(decide(&Settings::default(), &message, Environment::default()).toast); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn hidden_channel_only_alerts_for_mentions_of_me() { |
| 338 | let triggers: [fn(&mut Incoming); 3] = [ |
| 339 | |incoming| incoming.signals.mentions_channel = true, |
| 340 | |incoming| incoming.signals.in_my_thread = true, |
| 341 | |incoming| incoming.signals.followed = true, |
| 342 | ]; |
| 343 | for trigger in triggers { |
| 344 | let mut message = channel(); |
| 345 | trigger(&mut message); |
| 346 | message.signals.hidden = true; |
| 347 | assert!(!channel_alerts(&message)); |
| 348 | } |
| 349 | let mut mention = channel(); |
| 350 | mention.signals.hidden = true; |
| 351 | mention.mentions_me = true; |
| 352 | assert!(channel_alerts(&mention)); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn chats_ignore_channel_signals() { |
| 357 | assert!(channel_alerts(&group())); |
| 358 | assert!(channel_alerts(&incoming(ChatKind::Direct))); |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn sound_setting_off_keeps_toast() { |
| 363 | let settings = Settings { |
| 364 | sound: false, |
| 365 | ..Default::default() |
| 366 | }; |
| 367 | let decision = decide(&settings, &group(), Environment::default()); |
| 368 | assert_eq!( |
| 369 | decision, |
| 370 | Decision { |
| 371 | toast: true, |
| 372 | sound: false |
| 373 | } |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn throttle_allows_once_per_window_per_chat() { |
| 379 | let mut throttle = SoundThrottle::default(); |
| 380 | let start = Instant::now(); |
| 381 | assert!(throttle.allow("a", start)); |
| 382 | assert!(!throttle.allow("a", start + Duration::from_secs(9))); |
| 383 | assert!(throttle.allow("b", start + Duration::from_secs(9))); |
| 384 | assert!(throttle.allow("a", start + Duration::from_secs(10))); |
| 385 | } |
| 386 | |
| 387 | #[test] |
| 388 | fn flash_needs_toast_inactive_window_and_setting() { |
| 389 | let settings = Settings::default(); |
| 390 | assert!(should_flash(&settings, true, false)); |
| 391 | assert!(!should_flash(&settings, true, true)); |
| 392 | assert!(!should_flash(&settings, false, false)); |
| 393 | let off = Settings { |
| 394 | flash: false, |
| 395 | ..Default::default() |
| 396 | }; |
| 397 | assert!(!should_flash(&off, true, false)); |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn corner_keys_round_trip() { |
| 402 | for corner in Corner::ALL { |
| 403 | assert_eq!(Corner::from_key(corner.key()), Some(corner)); |
| 404 | } |
| 405 | assert_eq!(Corner::BottomRight.next(), Corner::TopLeft); |
| 406 | } |
| 407 | } |