| 1 | use std::collections::HashMap; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use calling::Progress; |
| 5 | use chrono::{DateTime, Utc}; |
| 6 | use store::{ChannelNotificationLevel, ChannelNotifications}; |
| 7 | |
| 8 | use super::ring_tone::Ringtone; |
| 9 | use crate::call::{CallKind, CallModel}; |
| 10 | |
| 11 | pub const SOUND_THROTTLE: Duration = Duration::from_secs(10); |
| 12 | |
| 13 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 14 | pub enum Corner { |
| 15 | TopLeft, |
| 16 | TopRight, |
| 17 | BottomLeft, |
| 18 | #[default] |
| 19 | BottomRight, |
| 20 | } |
| 21 | |
| 22 | impl Corner { |
| 23 | pub const ALL: [Corner; 4] = [ |
| 24 | Corner::TopLeft, |
| 25 | Corner::TopRight, |
| 26 | Corner::BottomLeft, |
| 27 | Corner::BottomRight, |
| 28 | ]; |
| 29 | |
| 30 | pub fn label(self) -> &'static str { |
| 31 | match self { |
| 32 | Corner::TopLeft => "Top left", |
| 33 | Corner::TopRight => "Top right", |
| 34 | Corner::BottomLeft => "Bottom left", |
| 35 | Corner::BottomRight => "Bottom right", |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn key(self) -> &'static str { |
| 40 | match self { |
| 41 | Corner::TopLeft => "top_left", |
| 42 | Corner::TopRight => "top_right", |
| 43 | Corner::BottomLeft => "bottom_left", |
| 44 | Corner::BottomRight => "bottom_right", |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn from_key(key: &str) -> Option<Corner> { |
| 49 | Corner::ALL.into_iter().find(|corner| corner.key() == key) |
| 50 | } |
| 51 | |
| 52 | pub fn is_bottom(self) -> bool { |
| 53 | matches!(self, Corner::BottomLeft | Corner::BottomRight) |
| 54 | } |
| 55 | |
| 56 | pub fn is_right(self) -> bool { |
| 57 | matches!(self, Corner::TopRight | Corner::BottomRight) |
| 58 | } |
| 59 | |
| 60 | pub fn next(self) -> Corner { |
| 61 | let index = Corner::ALL |
| 62 | .iter() |
| 63 | .position(|corner| *corner == self) |
| 64 | .unwrap_or(0); |
| 65 | Corner::ALL[(index + 1) % Corner::ALL.len()] |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 70 | pub struct Settings { |
| 71 | pub sound: bool, |
| 72 | pub mentions_only: bool, |
| 73 | pub preview: bool, |
| 74 | pub corner: Corner, |
| 75 | pub flash: bool, |
| 76 | pub close_to_tray: bool, |
| 77 | pub do_not_disturb: bool, |
| 78 | pub mute_in_calls: bool, |
| 79 | pub ringtone: Ringtone, |
| 80 | } |
| 81 | |
| 82 | impl Default for Settings { |
| 83 | fn default() -> Self { |
| 84 | Settings { |
| 85 | sound: true, |
| 86 | mentions_only: false, |
| 87 | preview: true, |
| 88 | corner: Corner::BottomRight, |
| 89 | flash: true, |
| 90 | close_to_tray: true, |
| 91 | do_not_disturb: false, |
| 92 | mute_in_calls: false, |
| 93 | ringtone: Ringtone::Default, |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 99 | pub enum ChatKind { |
| 100 | Direct, |
| 101 | Group { member_count: usize }, |
| 102 | Channel { team: String, channel: String }, |
| 103 | } |
| 104 | |
| 105 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 106 | pub enum Preview { |
| 107 | Text(String), |
| 108 | Image, |
| 109 | } |
| 110 | |
| 111 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 112 | pub struct ChannelSignals { |
| 113 | pub mentions_channel: bool, |
| 114 | pub in_my_thread: bool, |
| 115 | pub hidden: bool, |
| 116 | pub is_reply: bool, |
| 117 | pub notifications: ChannelNotifications, |
| 118 | } |
| 119 | |
| 120 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 121 | pub struct Incoming { |
| 122 | pub conversation_id: String, |
| 123 | pub message_id: String, |
| 124 | pub kind: ChatKind, |
| 125 | pub chat_title: String, |
| 126 | pub sender_id: Option<String>, |
| 127 | pub sender_name: String, |
| 128 | pub preview: Preview, |
| 129 | pub mentions_me: bool, |
| 130 | pub muted: bool, |
| 131 | pub signals: ChannelSignals, |
| 132 | pub created_at: DateTime<Utc>, |
| 133 | } |
| 134 | |
| 135 | impl Incoming { |
| 136 | pub fn is_direct(&self) -> bool { |
| 137 | self.kind == ChatKind::Direct |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 142 | pub struct Environment { |
| 143 | pub chat_in_foreground: bool, |
| 144 | pub system_quiet: bool, |
| 145 | pub own_do_not_disturb: bool, |
| 146 | pub in_call: bool, |
| 147 | } |
| 148 | |
| 149 | pub fn in_call(server_in_call: bool, native_call: Option<&CallModel>) -> bool { |
| 150 | server_in_call || native_call.is_some_and(CallModel::is_active) |
| 151 | } |
| 152 | |
| 153 | pub fn should_ringback(native_call: Option<&CallModel>) -> bool { |
| 154 | native_call.is_some_and(|model| { |
| 155 | !model.incoming |
| 156 | && model.kind != CallKind::Meeting |
| 157 | && model.is_connecting() |
| 158 | && matches!(model.progress, Some(Progress::Calling | Progress::Ringing)) |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 163 | pub struct Decision { |
| 164 | pub toast: bool, |
| 165 | pub sound: bool, |
| 166 | } |
| 167 | |
| 168 | pub fn channel_alerts(incoming: &Incoming) -> bool { |
| 169 | if !matches!(incoming.kind, ChatKind::Channel { .. }) || incoming.mentions_me { |
| 170 | return true; |
| 171 | } |
| 172 | let signals = incoming.signals; |
| 173 | if signals.hidden { |
| 174 | return false; |
| 175 | } |
| 176 | if signals.mentions_channel || signals.in_my_thread { |
| 177 | return true; |
| 178 | } |
| 179 | let notifications = signals.notifications; |
| 180 | notifications.level != ChannelNotificationLevel::Off |
| 181 | && (!signals.is_reply || notifications.include_replies) |
| 182 | } |
| 183 | |
| 184 | fn channel_toasts(incoming: &Incoming) -> bool { |
| 185 | if !matches!(incoming.kind, ChatKind::Channel { .. }) || incoming.mentions_me { |
| 186 | return true; |
| 187 | } |
| 188 | let signals = incoming.signals; |
| 189 | signals.mentions_channel |
| 190 | || signals.in_my_thread |
| 191 | || signals.notifications.level == ChannelNotificationLevel::BannerAndFeed |
| 192 | } |
| 193 | |
| 194 | const SILENT: Decision = Decision { |
| 195 | toast: false, |
| 196 | sound: false, |
| 197 | }; |
| 198 | |
| 199 | pub fn decide(settings: &Settings, incoming: &Incoming, environment: Environment) -> Decision { |
| 200 | let suppressed = !channel_alerts(incoming) |
| 201 | || !channel_toasts(incoming) |
| 202 | || environment.chat_in_foreground |
| 203 | || environment.system_quiet |
| 204 | || environment.own_do_not_disturb |
| 205 | || settings.do_not_disturb |
| 206 | || (settings.mute_in_calls && environment.in_call) |
| 207 | || (incoming.muted && !incoming.mentions_me) |
| 208 | || (settings.mentions_only && !incoming.mentions_me && !incoming.is_direct()); |
| 209 | if suppressed { |
| 210 | return SILENT; |
| 211 | } |
| 212 | Decision { |
| 213 | toast: true, |
| 214 | sound: settings.sound && !environment.in_call, |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #[derive(Default)] |
| 219 | pub struct SoundThrottle { |
| 220 | last_played: HashMap<String, Instant>, |
| 221 | } |
| 222 | |
| 223 | impl SoundThrottle { |
| 224 | pub fn allow(&mut self, conversation_id: &str, now: Instant) -> bool { |
| 225 | let allowed = self |
| 226 | .last_played |
| 227 | .get(conversation_id) |
| 228 | .is_none_or(|last| now.duration_since(*last) >= SOUND_THROTTLE); |
| 229 | if allowed { |
| 230 | self.last_played.insert(conversation_id.to_owned(), now); |
| 231 | } |
| 232 | allowed |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | pub fn should_flash(settings: &Settings, toast_shown: bool, window_active: bool) -> bool { |
| 237 | settings.flash && toast_shown && !window_active |
| 238 | } |
| 239 | |
| 240 | #[cfg(test)] |
| 241 | mod tests { |
| 242 | use calling::{CallState, EndReason}; |
| 243 | |
| 244 | use super::*; |
| 245 | |
| 246 | fn incoming(kind: ChatKind) -> Incoming { |
| 247 | Incoming { |
| 248 | conversation_id: "chat".into(), |
| 249 | message_id: "1".into(), |
| 250 | kind, |
| 251 | chat_title: "Chat".into(), |
| 252 | sender_id: Some("u1".into()), |
| 253 | sender_name: "Mara".into(), |
| 254 | preview: Preview::Text("Hello".into()), |
| 255 | mentions_me: false, |
| 256 | muted: false, |
| 257 | signals: ChannelSignals::default(), |
| 258 | created_at: Utc::now(), |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | fn channel() -> Incoming { |
| 263 | incoming(ChatKind::Channel { |
| 264 | team: "Team".into(), |
| 265 | channel: "General".into(), |
| 266 | }) |
| 267 | } |
| 268 | |
| 269 | fn group() -> Incoming { |
| 270 | incoming(ChatKind::Group { member_count: 5 }) |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn plain_message_gets_toast_and_sound() { |
| 275 | let decision = decide(&Settings::default(), &group(), Environment::default()); |
| 276 | assert_eq!( |
| 277 | decision, |
| 278 | Decision { |
| 279 | toast: true, |
| 280 | sound: true |
| 281 | } |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn foreground_chat_is_silent() { |
| 287 | let environment = Environment { |
| 288 | chat_in_foreground: true, |
| 289 | ..Default::default() |
| 290 | }; |
| 291 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn focus_assist_and_dnd_are_silent() { |
| 296 | for environment in [ |
| 297 | Environment { |
| 298 | system_quiet: true, |
| 299 | ..Default::default() |
| 300 | }, |
| 301 | Environment { |
| 302 | own_do_not_disturb: true, |
| 303 | ..Default::default() |
| 304 | }, |
| 305 | ] { |
| 306 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 307 | } |
| 308 | let settings = Settings { |
| 309 | do_not_disturb: true, |
| 310 | ..Default::default() |
| 311 | }; |
| 312 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn in_call_follows_server_presence_or_a_live_native_call() { |
| 317 | let model = |state| { |
| 318 | let mut model = CallModel::incoming("Bea", "8:orgid:b", "Bea"); |
| 319 | model.state = state; |
| 320 | model |
| 321 | }; |
| 322 | let since = std::time::Instant::now(); |
| 323 | assert!(!in_call(false, None)); |
| 324 | assert!(in_call(true, None)); |
| 325 | assert!(in_call(false, Some(&model(CallState::Connecting)))); |
| 326 | assert!(in_call(false, Some(&model(CallState::Connected { since })))); |
| 327 | assert!(in_call( |
| 328 | false, |
| 329 | Some(&model(CallState::Reconnecting { since })) |
| 330 | )); |
| 331 | assert!(!in_call(false, Some(&model(CallState::Idle)))); |
| 332 | assert!(!in_call( |
| 333 | false, |
| 334 | Some(&model(CallState::Ended { |
| 335 | reason: EndReason::LocalHangup |
| 336 | })) |
| 337 | )); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn ringback_plays_only_while_an_outgoing_non_meeting_call_is_connecting() { |
| 342 | let callees = [("8:orgid:a".to_owned(), "Ana".to_owned())]; |
| 343 | let outgoing = |progress: Option<Progress>, state: CallState| { |
| 344 | let mut model = CallModel::people("Ana", &callees); |
| 345 | model.progress = progress; |
| 346 | model.state = state; |
| 347 | model |
| 348 | }; |
| 349 | let since = std::time::Instant::now(); |
| 350 | assert!(!should_ringback(None)); |
| 351 | assert!(!should_ringback(Some(&outgoing( |
| 352 | None, |
| 353 | CallState::Connecting |
| 354 | )))); |
| 355 | assert!(should_ringback(Some(&outgoing( |
| 356 | Some(Progress::Calling), |
| 357 | CallState::Connecting |
| 358 | )))); |
| 359 | assert!(should_ringback(Some(&outgoing( |
| 360 | Some(Progress::Ringing), |
| 361 | CallState::Connecting |
| 362 | )))); |
| 363 | assert!(!should_ringback(Some(&outgoing( |
| 364 | Some(Progress::Ringing), |
| 365 | CallState::Connected { since } |
| 366 | )))); |
| 367 | let ended = CallState::Ended { |
| 368 | reason: EndReason::LocalHangup, |
| 369 | }; |
| 370 | assert!(!should_ringback(Some(&outgoing( |
| 371 | Some(Progress::Ringing), |
| 372 | ended |
| 373 | )))); |
| 374 | let mut meeting = CallModel::meeting("Standup", false); |
| 375 | meeting.progress = Some(Progress::Calling); |
| 376 | assert!(!should_ringback(Some(&meeting))); |
| 377 | let mut incoming = CallModel::incoming("Bea", "8:orgid:b", "Bea"); |
| 378 | incoming.progress = Some(Progress::Ringing); |
| 379 | assert!(!should_ringback(Some(&incoming))); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn call_keeps_toast_but_drops_sound() { |
| 384 | let environment = Environment { |
| 385 | in_call: true, |
| 386 | ..Default::default() |
| 387 | }; |
| 388 | let decision = decide(&Settings::default(), &group(), environment); |
| 389 | assert_eq!( |
| 390 | decision, |
| 391 | Decision { |
| 392 | toast: true, |
| 393 | sound: false |
| 394 | } |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn mute_in_calls_drops_the_toast_only_while_in_a_call() { |
| 400 | let settings = Settings { |
| 401 | mute_in_calls: true, |
| 402 | ..Default::default() |
| 403 | }; |
| 404 | let in_call = Environment { |
| 405 | in_call: true, |
| 406 | ..Default::default() |
| 407 | }; |
| 408 | assert_eq!(decide(&settings, &group(), in_call), SILENT); |
| 409 | assert!(decide(&settings, &group(), Environment::default()).toast); |
| 410 | } |
| 411 | |
| 412 | #[test] |
| 413 | fn muted_chat_only_notifies_for_mentions() { |
| 414 | let mut muted = group(); |
| 415 | muted.muted = true; |
| 416 | assert_eq!( |
| 417 | decide(&Settings::default(), &muted, Environment::default()), |
| 418 | SILENT |
| 419 | ); |
| 420 | muted.mentions_me = true; |
| 421 | assert!(decide(&Settings::default(), &muted, Environment::default()).toast); |
| 422 | } |
| 423 | |
| 424 | #[test] |
| 425 | fn mentions_only_keeps_direct_and_mentions() { |
| 426 | let settings = Settings { |
| 427 | mentions_only: true, |
| 428 | ..Default::default() |
| 429 | }; |
| 430 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 431 | assert!( |
| 432 | decide( |
| 433 | &settings, |
| 434 | &incoming(ChatKind::Direct), |
| 435 | Environment::default() |
| 436 | ) |
| 437 | .toast |
| 438 | ); |
| 439 | let mut mention = group(); |
| 440 | mention.mentions_me = true; |
| 441 | assert!(decide(&settings, &mention, Environment::default()).toast); |
| 442 | } |
| 443 | |
| 444 | fn channel_with(level: ChannelNotificationLevel, include_replies: bool) -> Incoming { |
| 445 | let mut message = channel(); |
| 446 | message.signals.notifications = ChannelNotifications { |
| 447 | level, |
| 448 | include_replies, |
| 449 | }; |
| 450 | message |
| 451 | } |
| 452 | |
| 453 | fn toast_of(incoming: &Incoming) -> bool { |
| 454 | decide(&Settings::default(), incoming, Environment::default()).toast |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn banner_and_activity_posts_toast_and_feed() { |
| 459 | let post = channel_with(ChannelNotificationLevel::BannerAndFeed, false); |
| 460 | assert!(channel_alerts(&post)); |
| 461 | assert!(toast_of(&post)); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn activity_only_posts_feed_without_a_toast() { |
| 466 | let post = channel_with(ChannelNotificationLevel::Feed, false); |
| 467 | assert!(channel_alerts(&post)); |
| 468 | assert_eq!( |
| 469 | decide(&Settings::default(), &post, Environment::default()), |
| 470 | SILENT |
| 471 | ); |
| 472 | assert!(channel_alerts(&channel())); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn off_channel_stays_silent_except_for_mentions_and_my_threads() { |
| 477 | let mut post = channel_with(ChannelNotificationLevel::Off, true); |
| 478 | assert!(!channel_alerts(&post)); |
| 479 | assert_eq!( |
| 480 | decide(&Settings::default(), &post, Environment::default()), |
| 481 | SILENT |
| 482 | ); |
| 483 | post.signals.is_reply = true; |
| 484 | assert!(!channel_alerts(&post)); |
| 485 | let triggers: [fn(&mut Incoming); 3] = [ |
| 486 | |incoming| incoming.mentions_me = true, |
| 487 | |incoming| incoming.signals.mentions_channel = true, |
| 488 | |incoming| incoming.signals.in_my_thread = true, |
| 489 | ]; |
| 490 | for trigger in triggers { |
| 491 | let mut mention = channel_with(ChannelNotificationLevel::Off, false); |
| 492 | trigger(&mut mention); |
| 493 | assert!(channel_alerts(&mention)); |
| 494 | assert!(toast_of(&mention)); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | #[test] |
| 499 | fn replies_need_include_replies_or_my_thread() { |
| 500 | for level in [ |
| 501 | ChannelNotificationLevel::Feed, |
| 502 | ChannelNotificationLevel::BannerAndFeed, |
| 503 | ] { |
| 504 | let mut reply = channel_with(level, false); |
| 505 | reply.signals.is_reply = true; |
| 506 | assert!(!channel_alerts(&reply)); |
| 507 | reply.signals.in_my_thread = true; |
| 508 | assert!(channel_alerts(&reply)); |
| 509 | let mut included = channel_with(level, true); |
| 510 | included.signals.is_reply = true; |
| 511 | assert!(channel_alerts(&included)); |
| 512 | } |
| 513 | let mut banner_reply = channel_with(ChannelNotificationLevel::BannerAndFeed, true); |
| 514 | banner_reply.signals.is_reply = true; |
| 515 | assert!(toast_of(&banner_reply)); |
| 516 | let mut feed_reply = channel_with(ChannelNotificationLevel::Feed, true); |
| 517 | feed_reply.signals.is_reply = true; |
| 518 | assert!(!toast_of(&feed_reply)); |
| 519 | } |
| 520 | |
| 521 | #[test] |
| 522 | fn hidden_channel_only_alerts_for_mentions_of_me() { |
| 523 | let triggers: [fn(&mut Incoming); 3] = [ |
| 524 | |incoming| incoming.signals.mentions_channel = true, |
| 525 | |incoming| incoming.signals.in_my_thread = true, |
| 526 | |incoming| { |
| 527 | incoming.signals.notifications.level = ChannelNotificationLevel::BannerAndFeed |
| 528 | }, |
| 529 | ]; |
| 530 | for trigger in triggers { |
| 531 | let mut message = channel(); |
| 532 | trigger(&mut message); |
| 533 | message.signals.hidden = true; |
| 534 | assert!(!channel_alerts(&message)); |
| 535 | } |
| 536 | let mut mention = channel(); |
| 537 | mention.signals.hidden = true; |
| 538 | mention.mentions_me = true; |
| 539 | assert!(channel_alerts(&mention)); |
| 540 | } |
| 541 | |
| 542 | #[test] |
| 543 | fn chats_ignore_channel_signals() { |
| 544 | assert!(channel_alerts(&group())); |
| 545 | assert!(channel_alerts(&incoming(ChatKind::Direct))); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn sound_setting_off_keeps_toast() { |
| 550 | let settings = Settings { |
| 551 | sound: false, |
| 552 | ..Default::default() |
| 553 | }; |
| 554 | let decision = decide(&settings, &group(), Environment::default()); |
| 555 | assert_eq!( |
| 556 | decision, |
| 557 | Decision { |
| 558 | toast: true, |
| 559 | sound: false |
| 560 | } |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | #[test] |
| 565 | fn throttle_allows_once_per_window_per_chat() { |
| 566 | let mut throttle = SoundThrottle::default(); |
| 567 | let start = Instant::now(); |
| 568 | assert!(throttle.allow("a", start)); |
| 569 | assert!(!throttle.allow("a", start + Duration::from_secs(9))); |
| 570 | assert!(throttle.allow("b", start + Duration::from_secs(9))); |
| 571 | assert!(throttle.allow("a", start + Duration::from_secs(10))); |
| 572 | } |
| 573 | |
| 574 | #[test] |
| 575 | fn flash_needs_toast_inactive_window_and_setting() { |
| 576 | let settings = Settings::default(); |
| 577 | assert!(should_flash(&settings, true, false)); |
| 578 | assert!(!should_flash(&settings, true, true)); |
| 579 | assert!(!should_flash(&settings, false, false)); |
| 580 | let off = Settings { |
| 581 | flash: false, |
| 582 | ..Default::default() |
| 583 | }; |
| 584 | assert!(!should_flash(&off, true, false)); |
| 585 | } |
| 586 | |
| 587 | #[test] |
| 588 | fn corner_keys_round_trip() { |
| 589 | for corner in Corner::ALL { |
| 590 | assert_eq!(Corner::from_key(corner.key()), Some(corner)); |
| 591 | } |
| 592 | assert_eq!(Corner::BottomRight.next(), Corner::TopLeft); |
| 593 | } |
| 594 | } |