| 1 | //! The organizations of the signed-in account and which one the window shows. |
| 2 | //! |
| 3 | //! Lives in its own file beside the caches, because it decides which cache to open. |
| 4 | //! The home tenant keeps `cache.sqlite3`; a guest tenant gets `cache-<tenant id>.sqlite3`. |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | |
| 9 | pub const ORGS_FILE: &str = "orgs.json"; |
| 10 | /// Start flag that opens one chat once its org has loaded, used when a toast of another org is clicked. |
| 11 | pub const OPEN_CONVERSATION_FLAG: &str = "--open-conversation"; |
| 12 | const HOME_DATABASE: &str = "cache.sqlite3"; |
| 13 | |
| 14 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 15 | pub struct Org { |
| 16 | pub id: String, |
| 17 | pub name: String, |
| 18 | pub guest: bool, |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 22 | pub struct Orgs { |
| 23 | /// Tenant id of the org the window shows; `None` is the home tenant. |
| 24 | pub active: Option<String>, |
| 25 | pub list: Vec<Org>, |
| 26 | } |
| 27 | |
| 28 | impl Orgs { |
| 29 | pub fn path() -> Option<PathBuf> { |
| 30 | crate::support::local_data_dir().map(|root| root.join(ORGS_FILE)) |
| 31 | } |
| 32 | |
| 33 | /// A missing or unreadable file is the home tenant with no known orgs. |
| 34 | pub fn load(path: &Path) -> Orgs { |
| 35 | std::fs::read(path) |
| 36 | .ok() |
| 37 | .and_then(|bytes| serde_json::from_slice(&bytes).ok()) |
| 38 | .unwrap_or_default() |
| 39 | } |
| 40 | |
| 41 | pub fn save(&self, path: &Path) -> std::io::Result<()> { |
| 42 | let bytes = serde_json::to_vec_pretty(self).map_err(std::io::Error::other)?; |
| 43 | std::fs::write(path, bytes) |
| 44 | } |
| 45 | |
| 46 | /// The active org when it is a guest tenant, the only case that needs its own tokens and cache. |
| 47 | pub fn active_guest(&self) -> Option<&Org> { |
| 48 | let active = self.active.as_deref()?; |
| 49 | self.list |
| 50 | .iter() |
| 51 | .find(|org| org.guest && org.id == active && is_tenant_id(&org.id)) |
| 52 | } |
| 53 | |
| 54 | pub fn is_active(&self, org: &Org) -> bool { |
| 55 | match self.active_guest() { |
| 56 | Some(active) => active.id == org.id, |
| 57 | None => !org.guest, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn database_file(&self) -> String { |
| 62 | match self.active_guest() { |
| 63 | Some(org) => format!("cache-{}.sqlite3", org.id), |
| 64 | None => HOME_DATABASE.to_owned(), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Takes the account's current org list; an active org that is gone falls back to home. |
| 69 | pub fn replace_list(&mut self, list: Vec<Org>) { |
| 70 | self.list = list; |
| 71 | if self.active_guest().is_none() { |
| 72 | self.active = None; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn activate(&mut self, org: &Org) { |
| 77 | self.active = org.guest.then(|| org.id.clone()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Tenant ids are GUIDs; anything else must not end up in a file name. |
| 82 | fn is_tenant_id(id: &str) -> bool { |
| 83 | !id.is_empty() |
| 84 | && id |
| 85 | .chars() |
| 86 | .all(|character| character.is_ascii_hexdigit() || character == '-') |
| 87 | } |
| 88 | |
| 89 | #[cfg(test)] |
| 90 | mod tests { |
| 91 | use super::*; |
| 92 | |
| 93 | fn org(id: &str, guest: bool) -> Org { |
| 94 | Org { |
| 95 | id: id.into(), |
| 96 | name: id.into(), |
| 97 | guest, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn a_guest_org_gets_its_own_cache_and_home_keeps_the_old_one() { |
| 103 | let mut orgs = Orgs { |
| 104 | active: None, |
| 105 | list: vec![org("aaaa-01", false), org("bbbb-02", true)], |
| 106 | }; |
| 107 | assert_eq!(orgs.database_file(), "cache.sqlite3"); |
| 108 | assert!(orgs.is_active(&orgs.list[0].clone())); |
| 109 | orgs.activate(&orgs.list[1].clone()); |
| 110 | assert_eq!(orgs.database_file(), "cache-bbbb-02.sqlite3"); |
| 111 | assert!(orgs.is_active(&orgs.list[1].clone())); |
| 112 | orgs.activate(&orgs.list[0].clone()); |
| 113 | assert_eq!(orgs.active, None); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn an_unknown_or_unsafe_active_org_is_the_home_tenant() { |
| 118 | let mut orgs = Orgs { |
| 119 | active: Some("../evil".into()), |
| 120 | list: vec![org("../evil", true)], |
| 121 | }; |
| 122 | assert_eq!(orgs.database_file(), "cache.sqlite3"); |
| 123 | orgs.active = Some("bbbb-02".into()); |
| 124 | orgs.replace_list(vec![org("aaaa-01", false)]); |
| 125 | assert_eq!(orgs.active, None); |
| 126 | } |
| 127 | } |