| 1 | use serde_json::{Map, Value}; |
| 2 | |
| 3 | pub(crate) const PROPERTY_NAME: &str = "conversationNotificationSettings"; |
| 4 | const ALL_NEW_POSTS: &str = "allNewPosts"; |
| 5 | const DESKTOP: &str = "dskNotif"; |
| 6 | const EVERYONE: &str = "everyone"; |
| 7 | const ON: &str = "On"; |
| 8 | const OFF: &str = "Off"; |
| 9 | |
| 10 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 11 | pub enum ChatNotificationMode { |
| 12 | #[default] |
| 13 | AllNewMessages, |
| 14 | Mute, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 18 | pub enum DesktopNotifications { |
| 19 | #[default] |
| 20 | MatchAllChats, |
| 21 | On, |
| 22 | Off, |
| 23 | } |
| 24 | |
| 25 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 26 | pub struct ChatNotificationWrite { |
| 27 | pub alerts: bool, |
| 28 | pub settings: String, |
| 29 | } |
| 30 | |
| 31 | fn stored(raw: Option<&str>) -> Map<String, Value> { |
| 32 | raw.and_then(|text| serde_json::from_str::<Value>(text).ok()) |
| 33 | .and_then(|value| value.as_object().cloned()) |
| 34 | .unwrap_or_default() |
| 35 | } |
| 36 | |
| 37 | pub fn parse_desktop(raw: Option<&str>) -> DesktopNotifications { |
| 38 | match stored(raw).get(DESKTOP).and_then(Value::as_str) { |
| 39 | Some(ON) => DesktopNotifications::On, |
| 40 | Some(OFF) => DesktopNotifications::Off, |
| 41 | _ => DesktopNotifications::MatchAllChats, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Teams' granular chat setting: `alerts` plus the merged `conversationNotificationSettings` text. |
| 46 | pub fn chat_notification_write( |
| 47 | raw: Option<&str>, |
| 48 | mode: ChatNotificationMode, |
| 49 | desktop: DesktopNotifications, |
| 50 | ) -> ChatNotificationWrite { |
| 51 | let mut settings = stored(raw); |
| 52 | let desktop_value = match desktop { |
| 53 | DesktopNotifications::MatchAllChats => Value::Null, |
| 54 | DesktopNotifications::On => Value::String(ON.to_owned()), |
| 55 | DesktopNotifications::Off => Value::String(OFF.to_owned()), |
| 56 | }; |
| 57 | settings.insert(DESKTOP.to_owned(), desktop_value); |
| 58 | let alerts = mode == ChatNotificationMode::AllNewMessages; |
| 59 | settings.insert( |
| 60 | ALL_NEW_POSTS.to_owned(), |
| 61 | Value::String(if alerts { ON } else { OFF }.to_owned()), |
| 62 | ); |
| 63 | if !alerts { |
| 64 | settings.insert(EVERYONE.to_owned(), Value::String(OFF.to_owned())); |
| 65 | } |
| 66 | ChatNotificationWrite { |
| 67 | alerts, |
| 68 | settings: Value::Object(settings).to_string(), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests { |
| 74 | use serde_json::json; |
| 75 | |
| 76 | use super::*; |
| 77 | |
| 78 | fn written(write: &ChatNotificationWrite) -> Value { |
| 79 | serde_json::from_str(&write.settings).unwrap() |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn desktop_preference_is_read_from_dsk_notif() { |
| 84 | assert_eq!(parse_desktop(None), DesktopNotifications::MatchAllChats); |
| 85 | assert_eq!( |
| 86 | parse_desktop(Some(r#"{"dskNotif":"Off"}"#)), |
| 87 | DesktopNotifications::Off |
| 88 | ); |
| 89 | assert_eq!( |
| 90 | parse_desktop(Some(r#"{"dskNotif":"On","allNewPosts":"On"}"#)), |
| 91 | DesktopNotifications::On |
| 92 | ); |
| 93 | assert_eq!( |
| 94 | parse_desktop(Some(r#"{"dskNotif":null}"#)), |
| 95 | DesktopNotifications::MatchAllChats |
| 96 | ); |
| 97 | assert_eq!( |
| 98 | parse_desktop(Some("garbage")), |
| 99 | DesktopNotifications::MatchAllChats |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn all_new_messages_turns_alerts_on_and_keeps_other_fields() { |
| 105 | let write = chat_notification_write( |
| 106 | Some(r#"{"everyone":"On","future":1}"#), |
| 107 | ChatNotificationMode::AllNewMessages, |
| 108 | DesktopNotifications::MatchAllChats, |
| 109 | ); |
| 110 | assert!(write.alerts); |
| 111 | assert_eq!( |
| 112 | written(&write), |
| 113 | json!({"everyone": "On", "future": 1, "dskNotif": null, "allNewPosts": "On"}) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn mute_turns_alerts_and_everyone_mentions_off() { |
| 119 | let write = chat_notification_write( |
| 120 | Some(r#"{"everyone":"On"}"#), |
| 121 | ChatNotificationMode::Mute, |
| 122 | DesktopNotifications::Off, |
| 123 | ); |
| 124 | assert!(!write.alerts); |
| 125 | assert_eq!( |
| 126 | written(&write), |
| 127 | json!({"everyone": "Off", "dskNotif": "Off", "allNewPosts": "Off"}) |
| 128 | ); |
| 129 | } |
| 130 | } |