BlueGit

ict rusty-teams

Files

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

settings.rs

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