BlueGit

ict rusty-teams

Files

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

rules.rs

rules.rs 28.7 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 }
592
593 #[test]
594 fn calls_and_busy_keep_the_sound_unless_the_busy_setting_is_off() {
595 let busy_environments = [
596 Environment {
597 in_call: true,
598 ..Default::default()
599 },
600 Environment {
601 own_busy: true,
602 ..Default::default()
603 },
604 ];
605 let quiet_when_busy = Settings {
606 sounds: SoundSettings {
607 sound_when_busy: false,
608 ..Default::default()
609 },
610 ..Default::default()
611 };
612 for environment in busy_environments {
613 assert!(decide(&Settings::default(), &group(), environment).sound);
614 assert_eq!(
615 decide(&quiet_when_busy, &group(), environment),
616 Decision {
617 toast: true,
618 sound: false,
619 urgent_sound: false,
620 }
621 );
622 }
623 assert!(decide(&quiet_when_busy, &group(), Environment::default()).sound);
624 }
625
626 #[test]
627 fn incoming_calls_ring_the_chosen_tone_and_beep_during_a_call() {
628 let mut settings = Settings::default();
629 settings.sounds.ringtones.regular = Ringtone::Bop;
630 assert_eq!(ringtone_for(&settings, false, false), Some(Ringtone::Bop));
631 assert_eq!(ringtone_for(&settings, true, true), Some(Ringtone::Beep));
632 settings.sounds.sound_when_busy = false;
633 assert_eq!(ringtone_for(&settings, false, true), None);
634 assert_eq!(ringtone_for(&settings, false, false), Some(Ringtone::Bop));
635 settings.sounds.call_and_meeting_sound = false;
636 assert_eq!(ringtone_for(&settings, false, false), None);
637 assert!(!meeting_start_sound(&settings, false));
638 }
639
640 #[test]
641 fn meeting_start_sound_follows_the_call_and_busy_settings() {
642 let mut settings = Settings::default();
643 assert!(meeting_start_sound(&settings, false));
644 assert!(meeting_start_sound(&settings, true));
645 settings.sounds.sound_when_busy = false;
646 assert!(!meeting_start_sound(&settings, true));
647 }
648
649 #[test]
650 fn mute_in_calls_drops_the_toast_only_while_in_a_call() {
651 let settings = Settings {
652 mute_in_calls: true,
653 ..Default::default()
654 };
655 let in_call = Environment {
656 in_call: true,
657 ..Default::default()
658 };
659 assert_eq!(decide(&settings, &group(), in_call), SILENT);
660 assert!(decide(&settings, &group(), Environment::default()).toast);
661 }
662
663 #[test]
664 fn muted_chat_only_alerts_and_notifies_for_mentions() {
665 let mut muted = group();
666 muted.muted = true;
667 assert!(!alerts(&muted));
668 assert_eq!(
669 decide(&Settings::default(), &muted, Environment::default()),
670 SILENT
671 );
672 muted.mentions_me = true;
673 assert!(alerts(&muted));
674 assert!(decide(&Settings::default(), &muted, Environment::default()).toast);
675 }
676
677 fn with_account(change: impl FnOnce(&mut NotificationAccountSettings)) -> Settings {
678 let mut settings = Settings::default();
679 change(&mut settings.account);
680 settings
681 }
682
683 fn toasts(settings: &Settings, incoming: &Incoming) -> bool {
684 decide(settings, incoming, Environment::default()).toast
685 }
686
687 fn mention(mut incoming: Incoming) -> Incoming {
688 incoming.mentions_me = true;
689 incoming
690 }
691
692 #[test]
693 fn chat_message_notifications_off_keeps_personal_mentions() {
694 let settings = with_account(|account| account.chat_messages = ActivityAlert::Off);
695 assert!(!toasts(&settings, &group()));
696 assert!(!toasts(&settings, &incoming(ChatKind::Direct)));
697 assert!(toasts(&settings, &mention(group())));
698 }
699
700 #[test]
701 fn personal_mentions_only_in_activity_give_no_banner() {
702 let settings = with_account(|account| account.personal_mentions = ActivityAlert::OnlyFeed);
703 assert!(!toasts(&settings, &mention(group())));
704 assert!(!toasts(&settings, &mention(channel())));
705 assert!(toasts(&settings, &group()));
706 }
707
708 #[test]
709 fn mentions_and_replies_preset_drops_plain_channel_posts() {
710 let settings =
711 with_account(|account| account.preset = NotificationPreset::MentionsAndReplies);
712 let mut post = channel_with(ChannelNotificationLevel::BannerAndFeed, true);
713 assert!(!toasts(&settings, &post));
714 post.signals.in_my_thread = true;
715 assert!(toasts(&settings, &post));
716 assert!(toasts(&settings, &mention(channel())));
717 assert!(toasts(&settings, &group()));
718 }
719
720 fn meeting_chat(participated: bool) -> Incoming {
721 let mut message = group();
722 message.chat.meeting = true;
723 message.chat.participated = participated;
724 message
725 }
726
727 #[test]
728 fn meeting_chats_stay_quiet_until_i_take_part() {
729 let settings = Settings::default();
730 assert!(!toasts(&settings, &meeting_chat(false)));
731 assert!(toasts(&settings, &meeting_chat(true)));
732 assert!(toasts(&settings, &mention(meeting_chat(false))));
733 }
734
735 #[test]
736 fn meeting_chat_mute_and_unmute() {
737 let muted = with_account(|account| account.meeting_chats = MeetingChatNotifications::Mute);
738 assert!(!toasts(&muted, &meeting_chat(true)));
739 assert!(toasts(&muted, &mention(meeting_chat(true))));
740 let unmuted =
741 with_account(|account| account.meeting_chats = MeetingChatNotifications::Unmute);
742 assert!(toasts(&unmuted, &meeting_chat(false)));
743 }
744
745 #[test]
746 fn per_chat_desktop_preference_overrides_the_account() {
747 let mut silenced = group();
748 silenced.chat.desktop = DesktopNotifications::Off;
749 assert!(!toasts(&Settings::default(), &silenced));
750 assert!(!toasts(&Settings::default(), &mention(silenced)));
751 let settings = with_account(|account| account.chat_messages = ActivityAlert::Off);
752 let mut selected = group();
753 selected.chat.desktop = DesktopNotifications::On;
754 assert!(toasts(&settings, &selected));
755 }
756
757 fn channel_with(level: ChannelNotificationLevel, include_replies: bool) -> Incoming {
758 let mut message = channel();
759 message.signals.notifications = ChannelNotifications {
760 level,
761 include_replies,
762 };
763 message
764 }
765
766 fn toast_of(incoming: &Incoming) -> bool {
767 decide(&Settings::default(), incoming, Environment::default()).toast
768 }
769
770 #[test]
771 fn banner_and_activity_posts_toast_and_feed() {
772 let post = channel_with(ChannelNotificationLevel::BannerAndFeed, false);
773 assert!(channel_alerts(&post));
774 assert!(toast_of(&post));
775 }
776
777 #[test]
778 fn activity_only_posts_feed_without_a_toast() {
779 let post = channel_with(ChannelNotificationLevel::Feed, false);
780 assert!(channel_alerts(&post));
781 assert_eq!(
782 decide(&Settings::default(), &post, Environment::default()),
783 SILENT
784 );
785 assert!(channel_alerts(&channel()));
786 }
787
788 #[test]
789 fn off_channel_stays_silent_except_for_mentions_and_my_threads() {
790 let mut post = channel_with(ChannelNotificationLevel::Off, true);
791 assert!(!channel_alerts(&post));
792 assert_eq!(
793 decide(&Settings::default(), &post, Environment::default()),
794 SILENT
795 );
796 post.signals.is_reply = true;
797 assert!(!channel_alerts(&post));
798 let triggers: [fn(&mut Incoming); 3] = [
799 |incoming| incoming.mentions_me = true,
800 |incoming| incoming.signals.mentions_channel = true,
801 |incoming| incoming.signals.in_my_thread = true,
802 ];
803 for trigger in triggers {
804 let mut mention = channel_with(ChannelNotificationLevel::Off, false);
805 trigger(&mut mention);
806 assert!(channel_alerts(&mention));
807 assert!(toast_of(&mention));
808 }
809 }
810
811 #[test]
812 fn replies_need_include_replies_or_my_thread() {
813 for level in [
814 ChannelNotificationLevel::Feed,
815 ChannelNotificationLevel::BannerAndFeed,
816 ] {
817 let mut reply = channel_with(level, false);
818 reply.signals.is_reply = true;
819 assert!(!channel_alerts(&reply));
820 reply.signals.in_my_thread = true;
821 assert!(channel_alerts(&reply));
822 let mut included = channel_with(level, true);
823 included.signals.is_reply = true;
824 assert!(channel_alerts(&included));
825 }
826 let mut banner_reply = channel_with(ChannelNotificationLevel::BannerAndFeed, true);
827 banner_reply.signals.is_reply = true;
828 assert!(toast_of(&banner_reply));
829 let mut feed_reply = channel_with(ChannelNotificationLevel::Feed, true);
830 feed_reply.signals.is_reply = true;
831 assert!(!toast_of(&feed_reply));
832 }
833
834 #[test]
835 fn hidden_channel_only_alerts_for_mentions_of_me() {
836 let triggers: [fn(&mut Incoming); 3] = [
837 |incoming| incoming.signals.mentions_channel = true,
838 |incoming| incoming.signals.in_my_thread = true,
839 |incoming| {
840 incoming.signals.notifications.level = ChannelNotificationLevel::BannerAndFeed
841 },
842 ];
843 for trigger in triggers {
844 let mut message = channel();
845 trigger(&mut message);
846 message.signals.hidden = true;
847 assert!(!channel_alerts(&message));
848 }
849 let mut mention = channel();
850 mention.signals.hidden = true;
851 mention.mentions_me = true;
852 assert!(channel_alerts(&mention));
853 }
854
855 #[test]
856 fn chats_ignore_channel_signals() {
857 assert!(channel_alerts(&group()));
858 assert!(channel_alerts(&incoming(ChatKind::Direct)));
859 }
860
861 #[test]
862 fn sound_setting_off_keeps_toast() {
863 let settings = Settings {
864 sounds: SoundSettings {
865 notification_sound: false,
866 ..Default::default()
867 },
868 ..Default::default()
869 };
870 let decision = decide(&settings, &group(), Environment::default());
871 assert_eq!(
872 decision,
873 Decision {
874 toast: true,
875 sound: false,
876 urgent_sound: false,
877 }
878 );
879 }
880
881 #[test]
882 fn throttle_allows_once_per_window_per_chat() {
883 let mut throttle = SoundThrottle::default();
884 let start = Instant::now();
885 assert!(throttle.allow("a", start));
886 assert!(!throttle.allow("a", start + Duration::from_secs(9)));
887 assert!(throttle.allow("b", start + Duration::from_secs(9)));
888 assert!(throttle.allow("a", start + Duration::from_secs(10)));
889 }
890
891 #[test]
892 fn flash_needs_toast_inactive_window_and_setting() {
893 let settings = Settings::default();
894 assert!(should_flash(&settings, true, false));
895 assert!(!should_flash(&settings, true, true));
896 assert!(!should_flash(&settings, false, false));
897 let off = Settings {
898 flash: false,
899 ..Default::default()
900 };
901 assert!(!should_flash(&off, true, false));
902 }
903
904 #[test]
905 fn corner_keys_round_trip() {
906 for corner in Corner::ALL {
907 assert_eq!(Corner::from_key(corner.key()), Some(corner));
908 }
909 assert_eq!(Corner::BottomRight.next(), Corner::TopLeft);
910 }
911}