diff options
Diffstat (limited to 'fedi/live_federation/database.rs')
-rw-r--r-- | fedi/live_federation/database.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/fedi/live_federation/database.rs b/fedi/live_federation/database.rs new file mode 100644 index 0000000..967c534 --- /dev/null +++ b/fedi/live_federation/database.rs | |||
@@ -0,0 +1,26 @@ | |||
1 | use crate::{objects::person::DbUser, Error}; | ||
2 | use anyhow::anyhow; | ||
3 | use std::sync::{Arc, Mutex}; | ||
4 | |||
5 | pub type DatabaseHandle = Arc<Database>; | ||
6 | |||
7 | /// Our "database" which contains all known users (local and federated) | ||
8 | pub struct Database { | ||
9 | pub users: Mutex<Vec<DbUser>>, | ||
10 | } | ||
11 | |||
12 | impl Database { | ||
13 | pub fn local_user(&self) -> DbUser { | ||
14 | let lock = self.users.lock().unwrap(); | ||
15 | lock.first().unwrap().clone() | ||
16 | } | ||
17 | |||
18 | pub fn read_user(&self, name: &str) -> Result<DbUser, Error> { | ||
19 | let db_user = self.local_user(); | ||
20 | if name == db_user.name { | ||
21 | Ok(db_user) | ||
22 | } else { | ||
23 | Err(anyhow!("Invalid user {name}").into()) | ||
24 | } | ||
25 | } | ||
26 | } | ||