BlueGit

ict rusty-teams

Files

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

rules.rs

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