sos_server_storage/
traits.rs

1//! Server storage implementations.
2use crate::Result;
3use async_trait::async_trait;
4use indexmap::IndexSet;
5use sos_backend::FolderEventLog;
6use sos_core::{
7    device::{DevicePublicKey, TrustedDevice},
8    events::patch::FolderDiff,
9    AccountId, Paths, VaultFlags, VaultId,
10};
11use sos_sync::CreateSet;
12use sos_vault::{Summary, Vault};
13use std::collections::{HashMap, HashSet};
14use std::sync::Arc;
15use tokio::sync::RwLock;
16
17/// Trait for server storage implementations.
18#[async_trait]
19pub trait ServerAccountStorage {
20    /// Account identifier.
21    fn account_id(&self) -> &AccountId;
22
23    /// List the public keys of trusted devices.
24    fn list_device_keys(&self) -> HashSet<&DevicePublicKey>;
25
26    /// Computed storage directories for the provider.
27    fn paths(&self) -> Arc<Paths>;
28
29    /// Folder event logs.
30    fn folders(&self) -> &HashMap<VaultId, Arc<RwLock<FolderEventLog>>>;
31
32    /// Mutable folder event logs.
33    fn folders_mut(
34        &mut self,
35    ) -> &mut HashMap<VaultId, Arc<RwLock<FolderEventLog>>>;
36
37    /// Set the collection of trusted devices.
38    fn set_devices(&mut self, devices: IndexSet<TrustedDevice>);
39
40    /// Rename the account.
41    async fn rename_account(&self, name: &str) -> Result<()>;
42
43    /// Read a vault from storage.
44    async fn read_vault(&self, folder_id: &VaultId) -> Result<Vault>;
45
46    /// Write a vault to storage.
47    async fn write_vault(&self, vault: &Vault) -> Result<()>;
48
49    /*
50    /// Read the login vault from the storage.
51    async fn read_login_vault(&self) -> Result<Vault>;
52    */
53
54    /// Write a login vault to storage.
55    async fn write_login_vault(&self, vault: &Vault) -> Result<()>;
56
57    /// Replace all the events for a folder.
58    async fn replace_folder(
59        &self,
60        folder_id: &VaultId,
61        diff: &FolderDiff,
62    ) -> Result<(FolderEventLog, Vault)>;
63
64    /// Update folder flags.
65    async fn set_folder_flags(
66        &self,
67        folder_id: &VaultId,
68        flags: VaultFlags,
69    ) -> Result<()>;
70
71    /// Import an account from a change set of event logs.
72    ///
73    /// Does not prepare the identity vault event log
74    /// which should be done by calling `initialize_account()`
75    /// before creating new storage.
76    ///
77    /// Intended to be used on a server to create a new
78    /// account from a collection of patches.
79    async fn import_account(
80        &mut self,
81        account_data: &CreateSet,
82    ) -> Result<()>;
83
84    /// Load folders from the local disc.
85    ///
86    /// Creates the in-memory event logs for each folder on disc.
87    async fn load_folders(&mut self) -> Result<Vec<Summary>>;
88
89    /// Import a folder into an existing account.
90    ///
91    /// If a folder with the same identifier already exists
92    /// it is overwritten.
93    ///
94    /// Buffer is the encoded representation of the vault.
95    async fn import_folder(
96        &mut self,
97        id: &VaultId,
98        buffer: &[u8],
99    ) -> Result<()>;
100
101    /// Set the name of a folder.
102    async fn rename_folder(&mut self, id: &VaultId, name: &str)
103        -> Result<()>;
104
105    /// Delete a folder.
106    async fn delete_folder(&mut self, id: &VaultId) -> Result<()>;
107
108    /// Delete this account.
109    async fn delete_account(&mut self) -> Result<()>;
110}