BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / followed.rs

followed.rs

followed.rs 1.2 KiB Download Raw
1use std::collections::HashSet;
2
3use store::Store;
4
5const META_KEY: &str = "notifications.followed_channels";
6
7pub fn load(store: &Store) -> HashSet<String> {
8 store
9 .meta(META_KEY)
10 .ok()
11 .flatten()
12 .map(|text| parse(&text))
13 .unwrap_or_default()
14}
15
16pub fn save(store: &Store, channel_ids: &HashSet<String>) {
17 let _ = store.set_meta(META_KEY, &serialize(channel_ids));
18}
19
20fn serialize(channel_ids: &HashSet<String>) -> String {
21 let mut ids: Vec<&str> = channel_ids.iter().map(String::as_str).collect();
22 ids.sort_unstable();
23 ids.join("\n")
24}
25
26fn parse(text: &str) -> HashSet<String> {
27 text.lines()
28 .filter(|id| !id.is_empty())
29 .map(str::to_owned)
30 .collect()
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn persists_in_store() {
39 let store = Store::open_in_memory().unwrap();
40 assert!(load(&store).is_empty());
41 let followed: HashSet<String> = ["19:a@thread.tacv2", "19:b@thread.tacv2"]
42 .into_iter()
43 .map(str::to_owned)
44 .collect();
45 save(&store, &followed);
46 assert_eq!(load(&store), followed);
47 save(&store, &HashSet::new());
48 assert!(load(&store).is_empty());
49 }
50}