BlueGit

ict rusty-teams

Files

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

settings.rs

settings.rs 5.4 KiB Download Raw
1use store::Store;
2
3use super::sounds::{AlertSound, Ringtone, RingtoneAssignments, RingtoneCallType};
4use super::rules::{Corner, Settings};
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 ("sound_type".to_owned(), sounds.notification_sound_type.name().to_owned()),
33 ("urgent_sound".to_owned(), sounds.urgent_sound.to_string()),
34 ("urgent_sound_type".to_owned(), sounds.urgent_sound_type.name().to_owned()),
35 ("call_sound".to_owned(), sounds.call_and_meeting_sound.to_string()),
36 ("sound_when_busy".to_owned(), sounds.sound_when_busy.to_string()),
37 ("mentions_only".to_owned(), settings.mentions_only.to_string()),
38 ("preview".to_owned(), settings.preview.to_string()),
39 ("corner".to_owned(), settings.corner.key().to_owned()),
40 ("taskbar_flash".to_owned(), settings.flash.to_string()),
41 ("close_to_tray".to_owned(), settings.close_to_tray.to_string()),
42 ("do_not_disturb".to_owned(), settings.do_not_disturb.to_string()),
43 ("mute_in_calls".to_owned(), settings.mute_in_calls.to_string()),
44 ]
45 .into_iter()
46 .chain(ringtones)
47 .map(|(key, value)| format!("{key}={value}"))
48 .collect::<Vec<_>>()
49 .join("\n")
50}
51
52fn parse(text: &str) -> Settings {
53 let mut settings = Settings::default();
54 for line in text.lines() {
55 let Some((key, value)) = line.split_once('=') else {
56 continue;
57 };
58 let flag = value == "true";
59 let sounds = &mut settings.sounds;
60 match key {
61 "sound" => sounds.notification_sound = flag,
62 "sound_type" => set_alert_sound(&mut sounds.notification_sound_type, value),
63 "urgent_sound" => sounds.urgent_sound = flag,
64 "urgent_sound_type" => set_alert_sound(&mut sounds.urgent_sound_type, value),
65 "call_sound" => sounds.call_and_meeting_sound = flag,
66 "sound_when_busy" => sounds.sound_when_busy = flag,
67 "mentions_only" => settings.mentions_only = flag,
68 "preview" => settings.preview = flag,
69 "corner" => settings.corner = Corner::from_key(value).unwrap_or_default(),
70 "taskbar_flash" => settings.flash = flag,
71 "close_to_tray" => settings.close_to_tray = flag,
72 "do_not_disturb" => settings.do_not_disturb = flag,
73 "mute_in_calls" => settings.mute_in_calls = flag,
74 _ => set_ringtone(&mut sounds.ringtones, key, value),
75 }
76 }
77 settings
78}
79
80fn set_alert_sound(target: &mut AlertSound, value: &str) {
81 if let Some(sound) = AlertSound::from_name(value) {
82 *target = sound;
83 }
84}
85
86fn set_ringtone(ringtones: &mut RingtoneAssignments, key: &str, value: &str) {
87 let call_type = key
88 .strip_prefix(RINGTONE_PREFIX)
89 .and_then(RingtoneCallType::from_name);
90 if let (Some(call_type), Some(ringtone)) = (call_type, Ringtone::from_name(value)) {
91 ringtones.set(call_type, ringtone);
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::super::sound_settings::SoundSettings;
98 use super::*;
99
100 #[test]
101 fn round_trips_every_field() {
102 let settings = Settings {
103 mentions_only: true,
104 preview: false,
105 corner: Corner::TopLeft,
106 flash: false,
107 close_to_tray: false,
108 do_not_disturb: true,
109 mute_in_calls: true,
110 sounds: SoundSettings {
111 notification_sound: false,
112 notification_sound_type: AlertSound::Ping,
113 urgent_sound: false,
114 urgent_sound_type: AlertSound::Alarm,
115 call_and_meeting_sound: false,
116 sound_when_busy: false,
117 ringtones: RingtoneAssignments {
118 regular: Ringtone::Bop,
119 delegate: Ringtone::WishGranted,
120 ..Default::default()
121 },
122 },
123 };
124 assert_eq!(parse(&serialize(&settings)), settings);
125 }
126
127 #[test]
128 fn old_flash_key_is_ignored() {
129 assert!(parse("flash=false").flash);
130 assert!(!parse("taskbar_flash=false").flash);
131 }
132
133 #[test]
134 fn the_old_synthesized_ringtone_key_is_ignored() {
135 assert_eq!(parse("ringtone=chime"), Settings::default());
136 }
137
138 #[test]
139 fn empty_or_garbage_gives_defaults() {
140 assert_eq!(parse(""), Settings::default());
141 assert_eq!(parse("junk\ncorner=nowhere"), Settings::default());
142 }
143
144 #[test]
145 fn persists_in_store() {
146 let store = Store::open_in_memory().unwrap();
147 assert_eq!(load(&store), Settings::default());
148 let mut changed = Settings::default();
149 changed.sounds.notification_sound = false;
150 save(&store, &changed);
151 assert_eq!(load(&store), changed);
152 }
153}