BlueGit

ict rusty-teams

Files

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

rules.rs

rules.rs 28.9 KiB Download Raw
1use std::collections::HashMap;
2use std::time::{Duration, Instant};
3
4use calling::Progress;
5use chrono::{DateTime, Utc};
6use store::{ChannelNotificationLevel, ChannelNotifications, MessageImportance};
7use teams_core::{
8 DesktopNotifications, MeetingChatNotifications, NotificationAccountSettings, NotificationPreset,
9};
10
11use super::sound_settings::SoundSettings;
12use super::sounds::Ringtone;
13use crate::call::{CallKind, CallModel};
14
15pub const SOUND_THROTTLE: Duration = Duration::from_secs(10);
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18pub enum Corner {
19 TopLeft,
20 TopRight,
21 BottomLeft,
22 #[default]
23 BottomRight,
24}
25
26impl Corner {
27 pub const ALL: [Corner; 4] = [
28 Corner::TopLeft,
29 Corner::TopRight,
30 Corner::BottomLeft,
31 Corner::BottomRight,
32 ];
33
34 pub fn label(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 key(self) -> &'static str {
44 match self {
45 Corner::TopLeft => "top_left",
46 Corner::TopRight => "top_right",
47 Corner::BottomLeft => "bottom_left",
48 Corner::BottomRight => "bottom_right",
49 }
50 }
51
52 pub fn from_key(key: &str) -> Option<Corner> {
53 Corner::ALL.into_iter().find(|corner| corner.key() == key)
54 }
55
56 pub fn is_bottom(self) -> bool {
57 matches!(self, Corner::BottomLeft | Corner::BottomRight)
58 }
59
60 pub fn is_right(self) -> bool {
61 matches!(self, Corner::TopRight | Corner::BottomRight)
62 }
63
64 pub fn next(self) -> Corner {
65 let index = Corner::ALL
66 .iter()
67 .position(|corner| *corner == self)
68 .unwrap_or(0);
69 Corner::ALL[(index + 1) % Corner::ALL.len()]
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct Settings {
75 pub account: NotificationAccountSettings,
76 pub corner: Corner,
77 pub flash: bool,
78 pub close_to_tray: bool,
79 pub do_not_disturb: bool,
80 pub mute_in_calls: bool,
81 pub sounds: SoundSettings,
82}
83
84impl Default for Settings {
85 fn default() -> Self {
86 Settings {
87 account: NotificationAccountSettings::default(),
88 corner: Corner::BottomRight,
89 flash: true,
90 close_to_tray: true,
91 do_not_disturb: false,
92 mute_in_calls: false,
93 sounds: SoundSettings::default(),
94 }
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub enum ChatKind {
100 Direct,
101 Group { member_count: usize },
102 Channel { team: String, channel: String },
103}
104
105#[derive(Debug, Clone, PartialEq, Eq)]
106pub enum Preview {
107 Text(String),
108 Image,
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
112pub 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, Copy, PartialEq, Eq, Default)]
121pub struct ChatSignals {
122 pub meeting: bool,
123 pub participated: bool,
124 pub desktop: DesktopNotifications,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq)]
128pub struct Incoming {
129 pub conversation_id: String,
130 pub message_id: String,
131 pub kind: ChatKind,
132 pub chat_title: String,
133 pub sender_id: Option<String>,
134 pub sender_name: String,
135 pub preview: Preview,
136 pub mentions_me: bool,
137 pub muted: bool,
138 pub signals: ChannelSignals,
139 pub chat: ChatSignals,
140 pub created_at: DateTime<Utc>,
141 pub importance: MessageImportance,
142}
143
144impl Incoming {
145 pub fn is_direct(&self) -> bool {
146 self.kind == ChatKind::Direct
147 }
148
149 pub fn is_urgent(&self) -> bool {
150 self.importance == MessageImportance::Urgent
151 }
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
155pub struct Environment {
156 pub chat_in_foreground: bool,
157 pub system_quiet: bool,
158 pub own_do_not_disturb: bool,
159 pub paused: bool,
160 pub own_busy: bool,
161 pub in_call: bool,
162}
163
164impl Environment {
165 pub fn busy(&self) -> bool {
166 self.in_call || self.own_busy
167 }
168}
169
170pub fn in_call(server_in_call: bool, native_call: Option<&CallModel>) -> bool {
171 server_in_call || native_call.is_some_and(CallModel::is_active)
172}
173
174pub fn should_ringback(native_call: Option<&CallModel>) -> bool {
175 native_call.is_some_and(|model| {
176 !model.incoming
177 && model.kind != CallKind::Meeting
178 && model.is_connecting()
179 && matches!(model.progress, Some(Progress::Calling | Progress::Ringing))
180 })
181}
182
183#[derive(Debug, Clone, Copy, PartialEq, Eq)]
184pub struct Decision {
185 pub toast: bool,
186 pub sound: bool,
187 pub urgent_sound: bool,
188}
189
190pub fn alerts(incoming: &Incoming) -> bool {
191 channel_alerts(incoming) && (!incoming.muted || incoming.mentions_me)
192}
193
194fn channel_alerts(incoming: &Incoming) -> bool {
195 if !matches!(incoming.kind, ChatKind::Channel { .. }) || incoming.mentions_me {
196 return true;
197 }
198 let signals = incoming.signals;
199 if signals.hidden {
200 return false;
201 }
202 if signals.mentions_channel || signals.in_my_thread {
203 return true;
204 }
205 let notifications = signals.notifications;
206 notifications.level != ChannelNotificationLevel::Off
207 && (!signals.is_reply || notifications.include_replies)
208}
209
210fn channel_toasts(incoming: &Incoming) -> bool {
211 if !matches!(incoming.kind, ChatKind::Channel { .. }) || incoming.mentions_me {
212 return true;
213 }
214 let signals = incoming.signals;
215 signals.mentions_channel
216 || signals.in_my_thread
217 || signals.notifications.level == ChannelNotificationLevel::BannerAndFeed
218}
219
220fn account_toasts(account: &NotificationAccountSettings, incoming: &Incoming) -> bool {
221 if matches!(incoming.kind, ChatKind::Channel { .. }) {
222 return if incoming.mentions_me {
223 account.personal_mentions.shows_banner()
224 } else {
225 account.preset != NotificationPreset::MentionsAndReplies
226 || incoming.signals.in_my_thread
227 };
228 }
229 let chat = incoming.chat;
230 if chat.desktop == DesktopNotifications::Off {
231 return false;
232 }
233 if incoming.mentions_me {
234 return account.personal_mentions.shows_banner();
235 }
236 let meeting_muted = chat.meeting
237 && match account.meeting_chats {
238 MeetingChatNotifications::Mute => true,
239 MeetingChatNotifications::MuteUntilJoin => !chat.participated,
240 MeetingChatNotifications::Unmute => false,
241 };
242 !meeting_muted
243 && (chat.desktop == DesktopNotifications::On || account.chat_messages.shows_banner())
244}
245
246pub fn shows_preview(settings: &Settings) -> bool {
247 settings.account.show_preview
248}
249
250const SILENT: Decision = Decision {
251 toast: false,
252 sound: false,
253 urgent_sound: false,
254};
255
256/// Teams lets urgent messages through Do not disturb ("You'll only get notifications for urgent messages and from your priority contacts").
257pub fn decide(settings: &Settings, incoming: &Incoming, environment: Environment) -> Decision {
258 let urgent = incoming.is_urgent();
259 let suppressed = !alerts(incoming)
260 || !channel_toasts(incoming)
261 || environment.chat_in_foreground
262 || environment.system_quiet
263 || (environment.own_do_not_disturb && !urgent)
264 || environment.paused
265 || settings.do_not_disturb
266 || (settings.mute_in_calls && environment.in_call)
267 || (!urgent && !account_toasts(&settings.account, incoming));
268 if suppressed {
269 return SILENT;
270 }
271 let busy_allows = settings.sounds.sound_when_busy || !environment.busy();
272 let urgent_sound = urgent && settings.sounds.urgent_sound && busy_allows;
273 Decision {
274 toast: true,
275 sound: urgent_sound || (!urgent && settings.sounds.notification_sound && busy_allows),
276 urgent_sound,
277 }
278}
279
280pub fn decide_proposal(
281 settings: &Settings,
282 desktop: DesktopNotifications,
283 environment: Environment,
284) -> Decision {
285 let suppressed = desktop == DesktopNotifications::Off
286 || environment.chat_in_foreground
287 || environment.system_quiet
288 || environment.own_do_not_disturb
289 || environment.paused
290 || settings.do_not_disturb
291 || (settings.mute_in_calls && environment.in_call);
292 if suppressed {
293 return SILENT;
294 }
295 Decision {
296 toast: true,
297 sound: settings.sounds.notification_sound
298 && (settings.sounds.sound_when_busy || !environment.busy()),
299 urgent_sound: false,
300 }
301}
302
303fn busy_allows_sound(settings: &Settings, busy: bool) -> bool {
304 !busy || settings.sounds.sound_when_busy
305}
306
307pub fn ringtone_for(settings: &Settings, in_call: bool, busy: bool) -> Option<Ringtone> {
308 let audible = settings.sounds.call_and_meeting_sound && busy_allows_sound(settings, busy);
309 audible.then_some(if in_call {
310 Ringtone::Beep
311 } else {
312 settings.sounds.ringtones.regular
313 })
314}
315
316pub fn meeting_start_sound(settings: &Settings, busy: bool) -> bool {
317 settings.sounds.call_and_meeting_sound && busy_allows_sound(settings, busy)
318}
319
320#[derive(Default)]
321pub struct SoundThrottle {
322 last_played: HashMap<String, Instant>,
323}
324
325impl SoundThrottle {
326 pub fn allow(&mut self, conversation_id: &str, now: Instant) -> bool {
327 let allowed = self
328 .last_played
329 .get(conversation_id)
330 .is_none_or(|last| now.duration_since(*last) >= SOUND_THROTTLE);
331 if allowed {
332 self.last_played.insert(conversation_id.to_owned(), now);
333 }
334 allowed
335 }
336}
337
338pub fn should_flash(settings: &Settings, toast_shown: bool, window_active: bool) -> bool {
339 settings.flash && toast_shown && !window_active
340}
341
342#[cfg(test)]
343mod tests {
344 use calling::{CallState, EndReason};
345 use teams_core::ActivityAlert;
346
347 use super::*;
348
349 fn incoming(kind: ChatKind) -> Incoming {
350 Incoming {
351 conversation_id: "chat".into(),
352 message_id: "1".into(),
353 kind,
354 chat_title: "Chat".into(),
355 sender_id: Some("u1".into()),
356 sender_name: "Mara".into(),
357 preview: Preview::Text("Hello".into()),
358 mentions_me: false,
359 muted: false,
360 signals: ChannelSignals::default(),
361 chat: ChatSignals::default(),
362 created_at: Utc::now(),
363 importance: Default::default(),
364 }
365 }
366
367 fn channel() -> Incoming {
368 incoming(ChatKind::Channel {
369 team: "Team".into(),
370 channel: "General".into(),
371 })
372 }
373
374 fn group() -> Incoming {
375 incoming(ChatKind::Group { member_count: 5 })
376 }
377
378 #[test]
379 fn paused_notifications_stay_silent_even_when_urgent() {
380 let mut incoming = incoming(ChatKind::Direct);
381 incoming.importance = MessageImportance::Urgent;
382 let paused = Environment {
383 paused: true,
384 ..Environment::default()
385 };
386 assert_eq!(decide(&Settings::default(), &incoming, paused), SILENT);
387 }
388
389 #[test]
390 fn plain_message_gets_toast_and_sound() {
391 let decision = decide(&Settings::default(), &group(), Environment::default());
392 assert_eq!(
393 decision,
394 Decision {
395 toast: true,
396 sound: true,
397 urgent_sound: false,
398 }
399 );
400 }
401
402 #[test]
403 fn proposals_toast_like_messages_but_ignore_chat_mute() {
404 let settings = Settings::default();
405 let shown = decide_proposal(
406 &settings,
407 DesktopNotifications::default(),
408 Environment::default(),
409 );
410 assert!(shown.toast && shown.sound);
411 for environment in [
412 Environment {
413 chat_in_foreground: true,
414 ..Default::default()
415 },
416 Environment {
417 own_do_not_disturb: true,
418 ..Default::default()
419 },
420 Environment {
421 system_quiet: true,
422 ..Default::default()
423 },
424 ] {
425 assert_eq!(
426 decide_proposal(&settings, DesktopNotifications::default(), environment),
427 SILENT
428 );
429 }
430 assert_eq!(
431 decide_proposal(&settings, DesktopNotifications::Off, Environment::default()),
432 SILENT
433 );
434 let quiet = Settings {
435 do_not_disturb: true,
436 ..Default::default()
437 };
438 assert_eq!(
439 decide_proposal(
440 &quiet,
441 DesktopNotifications::default(),
442 Environment::default()
443 ),
444 SILENT
445 );
446 }
447
448 #[test]
449 fn foreground_chat_is_silent() {
450 let environment = Environment {
451 chat_in_foreground: true,
452 ..Default::default()
453 };
454 assert_eq!(decide(&Settings::default(), &group(), environment), SILENT);
455 }
456
457 #[test]
458 fn focus_assist_and_dnd_are_silent() {
459 for environment in [
460 Environment {
461 system_quiet: true,
462 ..Default::default()
463 },
464 Environment {
465 own_do_not_disturb: true,
466 ..Default::default()
467 },
468 ] {
469 assert_eq!(decide(&Settings::default(), &group(), environment), SILENT);
470 }
471 let settings = Settings {
472 do_not_disturb: true,
473 ..Default::default()
474 };
475 assert_eq!(decide(&settings, &group(), Environment::default()), SILENT);
476 }
477
478 fn urgent() -> Incoming {
479 Incoming {
480 importance: MessageImportance::Urgent,
481 ..group()
482 }
483 }
484
485 #[test]
486 fn urgent_passes_do_not_disturb_and_the_account_rules_with_the_urgent_sound() {
487 let dnd = Environment {
488 own_do_not_disturb: true,
489 ..Default::default()
490 };
491 assert_eq!(
492 decide(&Settings::default(), &urgent(), dnd),
493 Decision {
494 toast: true,
495 sound: true,
496 urgent_sound: true,
497 }
498 );
499 let mut chat_off = urgent();
500 chat_off.chat.desktop = DesktopNotifications::Off;
501 assert!(decide(&Settings::default(), &chat_off, Environment::default()).toast);
502 let foreground = Environment {
503 chat_in_foreground: true,
504 ..Default::default()
505 };
506 assert_eq!(decide(&Settings::default(), &urgent(), foreground), SILENT);
507 }
508
509 #[test]
510 fn urgent_sound_follows_its_own_setting() {
511 let mut settings = Settings::default();
512 settings.sounds.urgent_sound = false;
513 let decision = decide(&settings, &urgent(), Environment::default());
514 assert!(decision.toast && !decision.sound && !decision.urgent_sound);
515 settings.sounds.urgent_sound = true;
516 settings.sounds.notification_sound = false;
517 let decision = decide(&settings, &urgent(), Environment::default());
518 assert!(decision.sound && decision.urgent_sound);
519 let important = Incoming {
520 importance: MessageImportance::High,
521 ..group()
522 };
523 assert!(!decide(&settings, &important, Environment::default()).sound);
524 }
525
526 #[test]
527 fn in_call_follows_server_presence_or_a_live_native_call() {
528 let model = |state| {
529 let mut model = CallModel::incoming("Bea", "8:orgid:b", "Bea");
530 model.state = state;
531 model
532 };
533 let since = std::time::Instant::now();
534 assert!(!in_call(false, None));
535 assert!(in_call(true, None));
536 assert!(in_call(false, Some(&model(CallState::Connecting))));
537 assert!(in_call(false, Some(&model(CallState::Connected { since }))));
538 assert!(in_call(
539 false,
540 Some(&model(CallState::Reconnecting { since }))
541 ));
542 assert!(!in_call(false, Some(&model(CallState::Idle))));
543 assert!(!in_call(
544 false,
545 Some(&model(CallState::Ended {
546 reason: EndReason::LocalHangup
547 }))
548 ));
549 }
550
551 #[test]
552 fn ringback_plays_only_while_an_outgoing_non_meeting_call_is_connecting() {
553 let callees = [("8:orgid:a".to_owned(), "Ana".to_owned())];
554 let outgoing = |progress: Option<Progress>, state: CallState| {
555 let mut model = CallModel::people("Ana", &callees);
556 model.progress = progress;
557 model.state = state;
558 model
559 };
560 let since = std::time::Instant::now();
561 assert!(!should_ringback(None));
562 assert!(!should_ringback(Some(&outgoing(
563 None,
564 CallState::Connecting
565 ))));
566 assert!(should_ringback(Some(&outgoing(
567 Some(Progress::Calling),
568 CallState::Connecting
569 ))));
570 assert!(should_ringback(Some(&outgoing(
571 Some(Progress::Ringing),
572 CallState::Connecting
573 ))));
574 assert!(!should_ringback(Some(&outgoing(
575 Some(Progress::Ringing),
576 CallState::Connected { since }
577 ))));
578 let ended = CallState::Ended {
579 reason: EndReason::LocalHangup,
580 };
581 assert!(!should_ringback(Some(&outgoing(
582 Some(Progress::Ringing),
583 ended
584 ))));
585 let mut meeting = CallModel::meeting("Standup", false);
586 meeting.progress = Some(Progress::Calling);
587 assert!(!should_ringback(Some(&meeting)));
588 let mut incoming = CallModel::incoming("Bea", "8:orgid:b", "Bea");
589 incoming.progress = Some(Progress::Ringing);
590 assert!(!should_ringback(Some(&incoming)));
591 let mut echo = CallModel::test();
592 echo.state = CallState::Connecting;
593 echo.progress = Some(Progress::Calling);
594 assert!(should_ringback(Some(&echo)));
595 }
596
597 #[test]
598 fn calls_and_busy_keep_the_sound_unless_the_busy_setting_is_off() {
599 let busy_environments = [
600 Environment {
601 in_call: true,
602 ..Default::default()
603 },
604 Environment {
605 own_busy: true,
606 ..Default::default()
607 },
608 ];
609 let quiet_when_busy = Settings {
610 sounds: SoundSettings {
611 sound_when_busy: false,
612 ..Default::default()
613 },
614 ..Default::default()
615 };
616 for environment in busy_environments {
617 assert!(decide(&Settings::default(), &group(), environment).sound);
618 assert_eq!(
619 decide(&quiet_when_busy, &group(), environment),
620 Decision {
621 toast: true,
622 sound: false,
623 urgent_sound: false,
624 }
625 );
626 }
627 assert!(decide(&quiet_when_busy, &group(), Environment::default()).sound);
628 }
629
630 #[test]
631 fn incoming_calls_ring_the_chosen_tone_and_beep_during_a_call() {
632 let mut settings = Settings::default();
633 settings.sounds.ringtones.regular = Ringtone::Bop;
634 assert_eq!(ringtone_for(&settings, false, false), Some(Ringtone::Bop));
635 assert_eq!(ringtone_for(&settings, true, true), Some(Ringtone::Beep));
636 settings.sounds.sound_when_busy = false;
637 assert_eq!(ringtone_for(&settings, false, true), None);
638 assert_eq!(ringtone_for(&settings, false, false), Some(Ringtone::Bop));
639 settings.sounds.call_and_meeting_sound = false;
640 assert_eq!(ringtone_for(&settings, false, false), None);
641 assert!(!meeting_start_sound(&settings, false));
642 }
643
644 #[test]
645 fn meeting_start_sound_follows_the_call_and_busy_settings() {
646 let mut settings = Settings::default();
647 assert!(meeting_start_sound(&settings, false));
648 assert!(meeting_start_sound(&settings, true));
649 settings.sounds.sound_when_busy = false;
650 assert!(!meeting_start_sound(&settings, true));
651 }
652
653 #[test]
654 fn mute_in_calls_drops_the_toast_only_while_in_a_call() {
655 let settings = Settings {
656 mute_in_calls: true,
657 ..Default::default()
658 };
659 let in_call = Environment {
660 in_call: true,
661 ..Default::default()
662 };
663 assert_eq!(decide(&settings, &group(), in_call), SILENT);
664 assert!(decide(&settings, &group(), Environment::default()).toast);
665 }
666
667 #[test]
668 fn muted_chat_only_alerts_and_notifies_for_mentions() {
669 let mut muted = group();
670 muted.muted = true;
671 assert!(!alerts(&muted));
672 assert_eq!(
673 decide(&Settings::default(), &muted, Environment::default()),
674 SILENT
675 );
676 muted.mentions_me = true;
677 assert!(alerts(&muted));
678 assert!(decide(&Settings::default(), &muted, Environment::default()).toast);
679 }
680
681 fn with_account(change: impl FnOnce(&mut NotificationAccountSettings)) -> Settings {
682 let mut settings = Settings::default();
683 change(&mut settings.account);
684 settings
685 }
686
687 fn toasts(settings: &Settings, incoming: &Incoming) -> bool {
688 decide(settings, incoming, Environment::default()).toast
689 }
690
691 fn mention(mut incoming: Incoming) -> Incoming {
692 incoming.mentions_me = true;
693 incoming
694 }
695
696 #[test]
697 fn chat_message_notifications_off_keeps_personal_mentions() {
698 let settings = with_account(|account| account.chat_messages = ActivityAlert::Off);
699 assert!(!toasts(&settings, &group()));
700 assert!(!toasts(&settings, &incoming(ChatKind::Direct)));
701 assert!(toasts(&settings, &mention(group())));
702 }
703
704 #[test]
705 fn personal_mentions_only_in_activity_give_no_banner() {
706 let settings = with_account(|account| account.personal_mentions = ActivityAlert::OnlyFeed);
707 assert!(!toasts(&settings, &mention(group())));
708 assert!(!toasts(&settings, &mention(channel())));
709 assert!(toasts(&settings, &group()));
710 }
711
712 #[test]
713 fn mentions_and_replies_preset_drops_plain_channel_posts() {
714 let settings =
715 with_account(|account| account.preset = NotificationPreset::MentionsAndReplies);
716 let mut post = channel_with(ChannelNotificationLevel::BannerAndFeed, true);
717 assert!(!toasts(&settings, &post));
718 post.signals.in_my_thread = true;
719 assert!(toasts(&settings, &post));
720 assert!(toasts(&settings, &mention(channel())));
721 assert!(toasts(&settings, &group()));
722 }
723
724 fn meeting_chat(participated: bool) -> Incoming {
725 let mut message = group();
726 message.chat.meeting = true;
727 message.chat.participated = participated;
728 message
729 }
730
731 #[test]
732 fn meeting_chats_stay_quiet_until_i_take_part() {
733 let settings = Settings::default();
734 assert!(!toasts(&settings, &meeting_chat(false)));
735 assert!(toasts(&settings, &meeting_chat(true)));
736 assert!(toasts(&settings, &mention(meeting_chat(false))));
737 }
738
739 #[test]
740 fn meeting_chat_mute_and_unmute() {
741 let muted = with_account(|account| account.meeting_chats = MeetingChatNotifications::Mute);
742 assert!(!toasts(&muted, &meeting_chat(true)));
743 assert!(toasts(&muted, &mention(meeting_chat(true))));
744 let unmuted =
745 with_account(|account| account.meeting_chats = MeetingChatNotifications::Unmute);
746 assert!(toasts(&unmuted, &meeting_chat(false)));
747 }
748
749 #[test]
750 fn per_chat_desktop_preference_overrides_the_account() {
751 let mut silenced = group();
752 silenced.chat.desktop = DesktopNotifications::Off;
753 assert!(!toasts(&Settings::default(), &silenced));
754 assert!(!toasts(&Settings::default(), &mention(silenced)));
755 let settings = with_account(|account| account.chat_messages = ActivityAlert::Off);
756 let mut selected = group();
757 selected.chat.desktop = DesktopNotifications::On;
758 assert!(toasts(&settings, &selected));
759 }
760
761 fn channel_with(level: ChannelNotificationLevel, include_replies: bool) -> Incoming {
762 let mut message = channel();
763 message.signals.notifications = ChannelNotifications {
764 level,
765 include_replies,
766 };
767 message
768 }
769
770 fn toast_of(incoming: &Incoming) -> bool {
771 decide(&Settings::default(), incoming, Environment::default()).toast
772 }
773
774 #[test]
775 fn banner_and_activity_posts_toast_and_feed() {
776 let post = channel_with(ChannelNotificationLevel::BannerAndFeed, false);
777 assert!(channel_alerts(&post));
778 assert!(toast_of(&post));
779 }
780
781 #[test]
782 fn activity_only_posts_feed_without_a_toast() {
783 let post = channel_with(ChannelNotificationLevel::Feed, false);
784 assert!(channel_alerts(&post));
785 assert_eq!(
786 decide(&Settings::default(), &post, Environment::default()),
787 SILENT
788 );
789 assert!(channel_alerts(&channel()));
790 }
791
792 #[test]
793 fn off_channel_stays_silent_except_for_mentions_and_my_threads() {
794 let mut post = channel_with(ChannelNotificationLevel::Off, true);
795 assert!(!channel_alerts(&post));
796 assert_eq!(
797 decide(&Settings::default(), &post, Environment::default()),
798 SILENT
799 );
800 post.signals.is_reply = true;
801 assert!(!channel_alerts(&post));
802 let triggers: [fn(&mut Incoming); 3] = [
803 |incoming| incoming.mentions_me = true,
804 |incoming| incoming.signals.mentions_channel = true,
805 |incoming| incoming.signals.in_my_thread = true,
806 ];
807 for trigger in triggers {
808 let mut mention = channel_with(ChannelNotificationLevel::Off, false);
809 trigger(&mut mention);
810 assert!(channel_alerts(&mention));
811 assert!(toast_of(&mention));
812 }
813 }
814
815 #[test]
816 fn replies_need_include_replies_or_my_thread() {
817 for level in [
818 ChannelNotificationLevel::Feed,
819 ChannelNotificationLevel::BannerAndFeed,
820 ] {
821 let mut reply = channel_with(level, false);
822 reply.signals.is_reply = true;
823 assert!(!channel_alerts(&reply));
824 reply.signals.in_my_thread = true;
825 assert!(channel_alerts(&reply));
826 let mut included = channel_with(level, true);
827 included.signals.is_reply = true;
828 assert!(channel_alerts(&included));
829 }
830 let mut banner_reply = channel_with(ChannelNotificationLevel::BannerAndFeed, true);
831 banner_reply.signals.is_reply = true;
832 assert!(toast_of(&banner_reply));
833 let mut feed_reply = channel_with(ChannelNotificationLevel::Feed, true);
834 feed_reply.signals.is_reply = true;
835 assert!(!toast_of(&feed_reply));
836 }
837
838 #[test]
839 fn hidden_channel_only_alerts_for_mentions_of_me() {
840 let triggers: [fn(&mut Incoming); 3] = [
841 |incoming| incoming.signals.mentions_channel = true,
842 |incoming| incoming.signals.in_my_thread = true,
843 |incoming| {
844 incoming.signals.notifications.level = ChannelNotificationLevel::BannerAndFeed
845 },
846 ];
847 for trigger in triggers {
848 let mut message = channel();
849 trigger(&mut message);
850 message.signals.hidden = true;
851 assert!(!channel_alerts(&message));
852 }
853 let mut mention = channel();
854 mention.signals.hidden = true;
855 mention.mentions_me = true;
856 assert!(channel_alerts(&mention));
857 }
858
859 #[test]
860 fn chats_ignore_channel_signals() {
861 assert!(channel_alerts(&group()));
862 assert!(channel_alerts(&incoming(ChatKind::Direct)));
863 }
864
865 #[test]
866 fn sound_setting_off_keeps_toast() {
867 let settings = Settings {
868 sounds: SoundSettings {
869 notification_sound: false,
870 ..Default::default()
871 },
872 ..Default::default()
873 };
874 let decision = decide(&settings, &group(), Environment::default());
875 assert_eq!(
876 decision,
877 Decision {
878 toast: true,
879 sound: false,
880 urgent_sound: false,
881 }
882 );
883 }
884
885 #[test]
886 fn throttle_allows_once_per_window_per_chat() {
887 let mut throttle = SoundThrottle::default();
888 let start = Instant::now();
889 assert!(throttle.allow("a", start));
890 assert!(!throttle.allow("a", start + Duration::from_secs(9)));
891 assert!(throttle.allow("b", start + Duration::from_secs(9)));
892 assert!(throttle.allow("a", start + Duration::from_secs(10)));
893 }
894
895 #[test]
896 fn flash_needs_toast_inactive_window_and_setting() {
897 let settings = Settings::default();
898 assert!(should_flash(&settings, true, false));
899 assert!(!should_flash(&settings, true, true));
900 assert!(!should_flash(&settings, false, false));
901 let off = Settings {
902 flash: false,
903 ..Default::default()
904 };
905 assert!(!should_flash(&off, true, false));
906 }
907
908 #[test]
909 fn corner_keys_round_trip() {
910 for corner in Corner::ALL {
911 assert_eq!(Corner::from_key(corner.key()), Some(corner));
912 }
913 assert_eq!(Corner::BottomRight.next(), Corner::TopLeft);
914 }
915}