BlueGit

ict rusty-teams

Files

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

settings.rs

settings.rs 5.5 KiB Download Raw
1use store::Store;
2
3use super::rules::{Corner, Settings};
4use super::sounds::{AlertSound, Ringtone, RingtoneAssignments, RingtoneCallType};
5
6const META_KEY: &str = "notifications.settings";
7const RINGTONE_PREFIX: &str = "ringtone.";
8
9pub fn load(store: &Store) -> Settings {
10 store
11 .meta(META_KEY)
12 .ok()
13 .flatten()
14 .map(|text| parse(&text))
15 .unwrap_or_default()
16}
17
18pub fn save(store: &Store, settings: &Settings) {
19 let _ = store.set_meta(META_KEY, &serialize(settings));
20}
21
22fn serialize(settings: &Settings) -> String {
23 let sounds = &settings.sounds;
24 let ringtones = RingtoneCallType::ALL.map(|call_type| {
25 (
26 format!("{RINGTONE_PREFIX}{}", call_type.name()),
27 sounds.ringtones.get(call_type).name().to_owned(),
28 )
29 });
30 [
31 ("sound".to_owned(), sounds.notification_sound.to_string()),
32 (
33 "sound_type".to_owned(),
34 sounds.notification_sound_type.name().to_owned(),
35 ),
36 ("urgent_sound".to_owned(), sounds.urgent_sound.to_string()),
37 (
38 "urgent_sound_type".to_owned(),
39 sounds.urgent_sound_type.name().to_owned(),
40 ),
41 (
42 "call_sound".to_owned(),
43 sounds.call_and_meeting_sound.to_string(),
44 ),
45 (
46 "sound_when_busy".to_owned(),
47 sounds.sound_when_busy.to_string(),
48 ),
49 ("corner".to_owned(), settings.corner.key().to_owned()),
50 ("taskbar_flash".to_owned(), settings.flash.to_string()),
51 (
52 "close_to_tray".to_owned(),
53 settings.close_to_tray.to_string(),
54 ),
55 (
56 "do_not_disturb".to_owned(),
57 settings.do_not_disturb.to_string(),
58 ),
59 (
60 "mute_in_calls".to_owned(),
61 settings.mute_in_calls.to_string(),
62 ),
63 ]
64 .into_iter()
65 .chain(ringtones)
66 .map(|(key, value)| format!("{key}={value}"))
67 .collect::<Vec<_>>()
68 .join("\n")
69}
70
71fn parse(text: &str) -> Settings {
72 let mut settings = Settings::default();
73 for line in text.lines() {
74 let Some((key, value)) = line.split_once('=') else {
75 continue;
76 };
77 let flag = value == "true";
78 let sounds = &mut settings.sounds;
79 match key {
80 "sound" => sounds.notification_sound = flag,
81 "sound_type" => set_alert_sound(&mut sounds.notification_sound_type, value),
82 "urgent_sound" => sounds.urgent_sound = flag,
83 "urgent_sound_type" => set_alert_sound(&mut sounds.urgent_sound_type, value),
84 "call_sound" => sounds.call_and_meeting_sound = flag,
85 "sound_when_busy" => sounds.sound_when_busy = flag,
86 "corner" => settings.corner = Corner::from_key(value).unwrap_or_default(),
87 "taskbar_flash" => settings.flash = flag,
88 "close_to_tray" => settings.close_to_tray = flag,
89 "do_not_disturb" => settings.do_not_disturb = flag,
90 "mute_in_calls" => settings.mute_in_calls = flag,
91 _ => set_ringtone(&mut sounds.ringtones, key, value),
92 }
93 }
94 settings
95}
96
97fn set_alert_sound(target: &mut AlertSound, value: &str) {
98 if let Some(sound) = AlertSound::from_name(value) {
99 *target = sound;
100 }
101}
102
103fn set_ringtone(ringtones: &mut RingtoneAssignments, key: &str, value: &str) {
104 let call_type = key
105 .strip_prefix(RINGTONE_PREFIX)
106 .and_then(RingtoneCallType::from_name);
107 if let (Some(call_type), Some(ringtone)) = (call_type, Ringtone::from_name(value)) {
108 ringtones.set(call_type, ringtone);
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::super::sound_settings::SoundSettings;
115 use super::*;
116
117 #[test]
118 fn round_trips_every_field() {
119 let settings = Settings {
120 account: Default::default(),
121 corner: Corner::TopLeft,
122 flash: false,
123 close_to_tray: false,
124 do_not_disturb: true,
125 mute_in_calls: true,
126 sounds: SoundSettings {
127 notification_sound: false,
128 notification_sound_type: AlertSound::Ping,
129 urgent_sound: false,
130 urgent_sound_type: AlertSound::Alarm,
131 call_and_meeting_sound: false,
132 sound_when_busy: false,
133 ringtones: RingtoneAssignments {
134 regular: Ringtone::Bop,
135 delegate: Ringtone::WishGranted,
136 ..Default::default()
137 },
138 },
139 };
140 assert_eq!(parse(&serialize(&settings)), settings);
141 }
142
143 #[test]
144 fn old_flash_key_is_ignored() {
145 assert!(parse("flash=false").flash);
146 assert!(!parse("taskbar_flash=false").flash);
147 }
148
149 #[test]
150 fn the_old_synthesized_ringtone_key_is_ignored() {
151 assert_eq!(parse("ringtone=chime"), Settings::default());
152 assert_eq!(
153 parse("mentions_only=true\npreview=false"),
154 Settings::default()
155 );
156 }
157
158 #[test]
159 fn empty_or_garbage_gives_defaults() {
160 assert_eq!(parse(""), Settings::default());
161 assert_eq!(parse("junk\ncorner=nowhere"), Settings::default());
162 }
163
164 #[test]
165 fn persists_in_store() {
166 let store = Store::open_in_memory().unwrap();
167 assert_eq!(load(&store), Settings::default());
168 let mut changed = Settings::default();
169 changed.sounds.notification_sound = false;
170 save(&store, &changed);
171 assert_eq!(load(&store), changed);
172 }
173}