| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use common::model::Id; |
| 4 | |
| 5 | use crate::{read_config, write_config}; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | pub enum Mark { |
| 9 | Mute, |
| 10 | Pin, |
| 11 | } |
| 12 | |
| 13 | impl Mark { |
| 14 | fn file(self) -> &'static str { |
| 15 | match self { |
| 16 | Mark::Mute => "mutes", |
| 17 | Mark::Pin => "pins", |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn prefix(account_user_id: Id) -> String { |
| 23 | format!("{account_user_id}\t") |
| 24 | } |
| 25 | |
| 26 | pub fn load(mark: Mark, account_user_id: Id) -> HashSet<Id> { |
| 27 | let Some(content) = read_config(mark.file()) else { |
| 28 | return HashSet::new(); |
| 29 | }; |
| 30 | let prefix = prefix(account_user_id); |
| 31 | |
| 32 | content |
| 33 | .lines() |
| 34 | .filter_map(|line| line.strip_prefix(&prefix)) |
| 35 | .filter_map(|community_id| community_id.parse().ok()) |
| 36 | .collect() |
| 37 | } |
| 38 | |
| 39 | pub fn save(mark: Mark, account_user_id: Id, ids: &HashSet<Id>) { |
| 40 | let prefix = prefix(account_user_id); |
| 41 | let mut content: String = read_config(mark.file()) |
| 42 | .unwrap_or_default() |
| 43 | .lines() |
| 44 | .filter(|line| !line.starts_with(&prefix)) |
| 45 | .map(|line| format!("{line}\n")) |
| 46 | .collect(); |
| 47 | content.extend( |
| 48 | ids.iter() |
| 49 | .map(|community_id| format!("{prefix}{community_id}\n")), |
| 50 | ); |
| 51 | |
| 52 | write_config(mark.file(), &content); |
| 53 | } |