| 1 | use store::Store; |
| 2 | |
| 3 | use crate::error::Result; |
| 4 | |
| 5 | const META_ACCOUNT_DEFAULT_REACTIONS: &str = "default_reactions_account"; |
| 6 | pub const TEAMS_DEFAULT_REACTIONS: [&str; 4] = ["like", "heart", "laugh", "surprised"]; |
| 7 | pub const DEFAULT_REACTION_SLOTS: usize = 5; |
| 8 | |
| 9 | pub fn store_account_default_reactions( |
| 10 | store: &Store, |
| 11 | reaction_ids: Option<&[String]>, |
| 12 | ) -> Result<()> { |
| 13 | let value = reaction_ids |
| 14 | .map(|reaction_ids| serde_json::Value::from(reaction_ids.to_vec()).to_string()) |
| 15 | .unwrap_or_default(); |
| 16 | Ok(store.set_meta(META_ACCOUNT_DEFAULT_REACTIONS, &value)?) |
| 17 | } |
| 18 | |
| 19 | /// The account setting `defaultMessageReactions`, Teams' own default set when it was never saved. |
| 20 | pub fn default_reactions(store: &Store) -> Vec<String> { |
| 21 | store |
| 22 | .meta(META_ACCOUNT_DEFAULT_REACTIONS) |
| 23 | .ok() |
| 24 | .flatten() |
| 25 | .filter(|stored| !stored.is_empty()) |
| 26 | .and_then(|stored| serde_json::from_str::<Vec<String>>(&stored).ok()) |
| 27 | .unwrap_or_else(|| { |
| 28 | TEAMS_DEFAULT_REACTIONS |
| 29 | .iter() |
| 30 | .map(|reaction_id| (*reaction_id).to_owned()) |
| 31 | .collect() |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | mod tests { |
| 37 | use super::*; |
| 38 | |
| 39 | #[test] |
| 40 | fn falls_back_to_the_teams_set_until_the_account_has_one() { |
| 41 | let store = Store::open_in_memory().unwrap(); |
| 42 | assert_eq!(default_reactions(&store), TEAMS_DEFAULT_REACTIONS); |
| 43 | let saved = vec!["fire".to_owned(), "like".to_owned()]; |
| 44 | store_account_default_reactions(&store, Some(&saved)).unwrap(); |
| 45 | assert_eq!(default_reactions(&store), saved); |
| 46 | store_account_default_reactions(&store, Some(&[])).unwrap(); |
| 47 | assert!(default_reactions(&store).is_empty()); |
| 48 | store_account_default_reactions(&store, None).unwrap(); |
| 49 | assert_eq!(default_reactions(&store), TEAMS_DEFAULT_REACTIONS); |
| 50 | } |
| 51 | } |