From c60b462fc10101879978e6f3fb6ac4d41d268508 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Thu, 18 Apr 2024 18:51:55 +0300 Subject: [PATCH] LocalBroker::personal_site_store --- nextgraph/src/local_broker.rs | 27 +++++++++++++++++++++++++-- ng-repo/src/errors.rs | 1 + ng-verifier/src/site.rs | 8 ++++++++ ng-wallet/src/types.rs | 10 +++++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/nextgraph/src/local_broker.rs b/nextgraph/src/local_broker.rs index 39fcd6e..4521761 100644 --- a/nextgraph/src/local_broker.rs +++ b/nextgraph/src/local_broker.rs @@ -8,7 +8,7 @@ // according to those terms. use async_once_cell::OnceCell; -use async_std::sync::{Arc, Mutex, RwLock}; +use async_std::sync::{Arc, Mutex, RwLock, RwLockReadGuard}; use core::fmt; use ng_net::actor::EActor; use ng_net::connection::{ClientConfig, IConnect, NoiseFSM, StartConfig}; @@ -1320,7 +1320,7 @@ pub async fn wallet_close(wallet_name: &String) -> Result<(), NgError> { match broker.opened_wallets.remove(wallet_name) { Some(mut opened_wallet) => { - for user in opened_wallet.wallet.sites() { + for user in opened_wallet.wallet.site_names() { let key: PubKey = (user.as_str()).try_into().unwrap(); match broker.opened_sessions.remove(&key) { Some(id) => { @@ -1372,6 +1372,29 @@ pub async fn doc_fetch( session.verifier.doc_fetch(nuri, payload) } +/// retrieves the ID of the one of the 3 stores of a the personal Site (3P: public, protected, or private) +pub async fn personal_site_store(session_id: u8, store: SiteStoreType) -> Result { + let broker = match LOCAL_BROKER.get() { + None | Some(Err(_)) => return Err(NgError::LocalBrokerNotInitialized), + Some(Ok(broker)) => broker.read().await, + }; + if session_id as usize >= broker.opened_sessions_list.len() { + return Err(NgError::InvalidArgument); + } + let session = broker.opened_sessions_list[session_id as usize] + .as_ref() + .ok_or(NgError::SessionNotFound)?; + + match broker.opened_wallets.get(&session.config.wallet_name()) { + Some(opened_wallet) => { + let user_id = session.config.user_id(); + let site = opened_wallet.wallet.site(&user_id)?; + Ok(site.get_site_store_id(store)) + } + None => Err(NgError::WalletNotFound), + } +} + #[cfg(test)] mod test { use super::*; diff --git a/ng-repo/src/errors.rs b/ng-repo/src/errors.rs index 7b172c1..84fb9af 100644 --- a/ng-repo/src/errors.rs +++ b/ng-repo/src/errors.rs @@ -48,6 +48,7 @@ pub enum NgError { RepoNotFound, BranchNotFound, StoreNotFound, + UserNotFound, } impl Error for NgError {} diff --git a/ng-verifier/src/site.rs b/ng-verifier/src/site.rs index 3880726..9b27dc0 100644 --- a/ng-verifier/src/site.rs +++ b/ng-verifier/src/site.rs @@ -57,6 +57,14 @@ impl SiteV0 { }) } + pub fn get_site_store_id(&self, store_type: SiteStoreType) -> PubKey { + match store_type { + SiteStoreType::Public => self.public.id, + SiteStoreType::Protected => self.protected.id, + SiteStoreType::Private => self.private.id, + } + } + fn create_individual_( user_priv_key: PrivKey, verifier: &mut Verifier, diff --git a/ng-wallet/src/types.rs b/ng-wallet/src/types.rs index 5d1db05..9092d73 100644 --- a/ng-wallet/src/types.rs +++ b/ng-wallet/src/types.rs @@ -476,11 +476,19 @@ impl SensitiveWallet { Self::V0(v0) => &v0.client, } } - pub fn sites(&self) -> Keys { + pub fn site_names(&self) -> Keys { match self { Self::V0(v0) => v0.sites.keys(), } } + pub fn site(&self, user_id: &UserId) -> Result<&SiteV0, NgError> { + match self { + Self::V0(v0) => match v0.sites.get(&user_id.to_string()) { + Some(site) => Ok(site), + None => Err(NgError::UserNotFound), + }, + } + } pub fn set_client(&mut self, client: ClientV0) { match self { Self::V0(v0) => v0.client = Some(client),