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