| 1 | use std::collections::HashMap; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | pub const SOUND_THROTTLE: Duration = Duration::from_secs(10); |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 7 | pub enum Corner { |
| 8 | TopLeft, |
| 9 | TopRight, |
| 10 | BottomLeft, |
| 11 | #[default] |
| 12 | BottomRight, |
| 13 | } |
| 14 | |
| 15 | impl Corner { |
| 16 | pub const ALL: [Corner; 4] = [ |
| 17 | Corner::TopLeft, |
| 18 | Corner::TopRight, |
| 19 | Corner::BottomLeft, |
| 20 | Corner::BottomRight, |
| 21 | ]; |
| 22 | |
| 23 | pub fn label(self) -> &'static str { |
| 24 | match self { |
| 25 | Corner::TopLeft => "Top left", |
| 26 | Corner::TopRight => "Top right", |
| 27 | Corner::BottomLeft => "Bottom left", |
| 28 | Corner::BottomRight => "Bottom right", |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn key(self) -> &'static str { |
| 33 | match self { |
| 34 | Corner::TopLeft => "top_left", |
| 35 | Corner::TopRight => "top_right", |
| 36 | Corner::BottomLeft => "bottom_left", |
| 37 | Corner::BottomRight => "bottom_right", |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub fn from_key(key: &str) -> Option<Corner> { |
| 42 | Corner::ALL.into_iter().find(|corner| corner.key() == key) |
| 43 | } |
| 44 | |
| 45 | pub fn is_bottom(self) -> bool { |
| 46 | matches!(self, Corner::BottomLeft | Corner::BottomRight) |
| 47 | } |
| 48 | |
| 49 | pub fn is_right(self) -> bool { |
| 50 | matches!(self, Corner::TopRight | Corner::BottomRight) |
| 51 | } |
| 52 | |
| 53 | pub fn next(self) -> Corner { |
| 54 | let index = Corner::ALL |
| 55 | .iter() |
| 56 | .position(|corner| *corner == self) |
| 57 | .unwrap_or(0); |
| 58 | Corner::ALL[(index + 1) % Corner::ALL.len()] |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 63 | pub struct Settings { |
| 64 | pub sound: bool, |
| 65 | pub mentions_only: bool, |
| 66 | pub preview: bool, |
| 67 | pub corner: Corner, |
| 68 | pub flash: bool, |
| 69 | pub close_to_tray: bool, |
| 70 | pub do_not_disturb: bool, |
| 71 | } |
| 72 | |
| 73 | impl Default for Settings { |
| 74 | fn default() -> Self { |
| 75 | Settings { |
| 76 | sound: true, |
| 77 | mentions_only: false, |
| 78 | preview: true, |
| 79 | corner: Corner::BottomRight, |
| 80 | flash: true, |
| 81 | close_to_tray: true, |
| 82 | do_not_disturb: false, |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 88 | pub enum ChatKind { |
| 89 | Direct, |
| 90 | Group { member_count: usize }, |
| 91 | Channel { team: String, channel: String }, |
| 92 | } |
| 93 | |
| 94 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 95 | pub enum Preview { |
| 96 | Text(String), |
| 97 | Image, |
| 98 | } |
| 99 | |
| 100 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 101 | pub struct Incoming { |
| 102 | pub conversation_id: String, |
| 103 | pub message_id: String, |
| 104 | pub kind: ChatKind, |
| 105 | pub chat_title: String, |
| 106 | pub sender_id: Option<String>, |
| 107 | pub sender_name: String, |
| 108 | pub preview: Preview, |
| 109 | pub mentions_me: bool, |
| 110 | pub muted: bool, |
| 111 | } |
| 112 | |
| 113 | impl Incoming { |
| 114 | pub fn is_direct(&self) -> bool { |
| 115 | self.kind == ChatKind::Direct |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 120 | pub struct Environment { |
| 121 | pub chat_in_foreground: bool, |
| 122 | pub system_quiet: bool, |
| 123 | pub own_do_not_disturb: bool, |
| 124 | pub in_call: bool, |
| 125 | } |
| 126 | |
| 127 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 128 | pub struct Decision { |
| 129 | pub toast: bool, |
| 130 | pub sound: bool, |
| 131 | } |
| 132 | |
| 133 | const SILENT: Decision = Decision { |
| 134 | toast: false, |
| 135 | sound: false, |
| 136 | }; |
| 137 | |
| 138 | pub fn decide(settings: &Settings, incoming: &Incoming, environment: Environment) -> Decision { |
| 139 | let suppressed = environment.chat_in_foreground |
| 140 | || environment.system_quiet |
| 141 | || environment.own_do_not_disturb |
| 142 | || settings.do_not_disturb |
| 143 | || (incoming.muted && !incoming.mentions_me) |
| 144 | || (settings.mentions_only && !incoming.mentions_me && !incoming.is_direct()); |
| 145 | if suppressed { |
| 146 | return SILENT; |
| 147 | } |
| 148 | Decision { |
| 149 | toast: true, |
| 150 | sound: settings.sound && !environment.in_call, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[derive(Default)] |
| 155 | pub struct SoundThrottle { |
| 156 | last_played: HashMap<String, Instant>, |
| 157 | } |
| 158 | |
| 159 | impl SoundThrottle { |
| 160 | pub fn allow(&mut self, conversation_id: &str, now: Instant) -> bool { |
| 161 | let allowed = self |
| 162 | .last_played |
| 163 | .get(conversation_id) |
| 164 | .is_none_or(|last| now.duration_since(*last) >= SOUND_THROTTLE); |
| 165 | if allowed { |
| 166 | self.last_played.insert(conversation_id.to_owned(), now); |
| 167 | } |
| 168 | allowed |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | pub fn should_flash(settings: &Settings, mentions_me: bool, window_active: bool) -> bool { |
| 173 | settings.flash && mentions_me && !window_active |
| 174 | } |
| 175 | |
| 176 | #[cfg(test)] |
| 177 | mod tests { |
| 178 | use super::*; |
| 179 | |
| 180 | fn incoming(kind: ChatKind) -> Incoming { |
| 181 | Incoming { |
| 182 | conversation_id: "chat".into(), |
| 183 | message_id: "1".into(), |
| 184 | kind, |
| 185 | chat_title: "Chat".into(), |
| 186 | sender_id: Some("u1".into()), |
| 187 | sender_name: "Mara".into(), |
| 188 | preview: Preview::Text("Hello".into()), |
| 189 | mentions_me: false, |
| 190 | muted: false, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fn group() -> Incoming { |
| 195 | incoming(ChatKind::Group { member_count: 5 }) |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn plain_message_gets_toast_and_sound() { |
| 200 | let decision = decide(&Settings::default(), &group(), Environment::default()); |
| 201 | assert_eq!( |
| 202 | decision, |
| 203 | Decision { |
| 204 | toast: true, |
| 205 | sound: true |
| 206 | } |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn foreground_chat_is_silent() { |
| 212 | let environment = Environment { |
| 213 | chat_in_foreground: true, |
| 214 | ..Default::default() |
| 215 | }; |
| 216 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn focus_assist_and_dnd_are_silent() { |
| 221 | for environment in [ |
| 222 | Environment { |
| 223 | system_quiet: true, |
| 224 | ..Default::default() |
| 225 | }, |
| 226 | Environment { |
| 227 | own_do_not_disturb: true, |
| 228 | ..Default::default() |
| 229 | }, |
| 230 | ] { |
| 231 | assert_eq!(decide(&Settings::default(), &group(), environment), SILENT); |
| 232 | } |
| 233 | let settings = Settings { |
| 234 | do_not_disturb: true, |
| 235 | ..Default::default() |
| 236 | }; |
| 237 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn call_keeps_toast_but_drops_sound() { |
| 242 | let environment = Environment { |
| 243 | in_call: true, |
| 244 | ..Default::default() |
| 245 | }; |
| 246 | let decision = decide(&Settings::default(), &group(), environment); |
| 247 | assert_eq!( |
| 248 | decision, |
| 249 | Decision { |
| 250 | toast: true, |
| 251 | sound: false |
| 252 | } |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn muted_chat_only_notifies_for_mentions() { |
| 258 | let mut muted = group(); |
| 259 | muted.muted = true; |
| 260 | assert_eq!( |
| 261 | decide(&Settings::default(), &muted, Environment::default()), |
| 262 | SILENT |
| 263 | ); |
| 264 | muted.mentions_me = true; |
| 265 | assert!(decide(&Settings::default(), &muted, Environment::default()).toast); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn mentions_only_keeps_direct_and_mentions() { |
| 270 | let settings = Settings { |
| 271 | mentions_only: true, |
| 272 | ..Default::default() |
| 273 | }; |
| 274 | assert_eq!(decide(&settings, &group(), Environment::default()), SILENT); |
| 275 | assert!(decide(&settings, &incoming(ChatKind::Direct), Environment::default()).toast); |
| 276 | let mut mention = group(); |
| 277 | mention.mentions_me = true; |
| 278 | assert!(decide(&settings, &mention, Environment::default()).toast); |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn sound_setting_off_keeps_toast() { |
| 283 | let settings = Settings { |
| 284 | sound: false, |
| 285 | ..Default::default() |
| 286 | }; |
| 287 | let decision = decide(&settings, &group(), Environment::default()); |
| 288 | assert_eq!( |
| 289 | decision, |
| 290 | Decision { |
| 291 | toast: true, |
| 292 | sound: false |
| 293 | } |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn throttle_allows_once_per_window_per_chat() { |
| 299 | let mut throttle = SoundThrottle::default(); |
| 300 | let start = Instant::now(); |
| 301 | assert!(throttle.allow("a", start)); |
| 302 | assert!(!throttle.allow("a", start + Duration::from_secs(9))); |
| 303 | assert!(throttle.allow("b", start + Duration::from_secs(9))); |
| 304 | assert!(throttle.allow("a", start + Duration::from_secs(10))); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn flash_needs_mention_inactive_window_and_setting() { |
| 309 | let settings = Settings::default(); |
| 310 | assert!(should_flash(&settings, true, false)); |
| 311 | assert!(!should_flash(&settings, true, true)); |
| 312 | assert!(!should_flash(&settings, false, false)); |
| 313 | let off = Settings { |
| 314 | flash: false, |
| 315 | ..Default::default() |
| 316 | }; |
| 317 | assert!(!should_flash(&off, true, false)); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn corner_keys_round_trip() { |
| 322 | for corner in Corner::ALL { |
| 323 | assert_eq!(Corner::from_key(corner.key()), Some(corner)); |
| 324 | } |
| 325 | assert_eq!(Corner::BottomRight.next(), Corner::TopLeft); |
| 326 | } |
| 327 | } |