BlueGit

ict rusty-teams

Files

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

rules.rs

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