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