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