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