LocalBroker::personal_site_store

pull/19/head
Niko PLP 7 months ago
parent 0ebf17d594
commit 86e5ec52ef
  1. 27
      nextgraph/src/local_broker.rs
  2. 1
      ng-repo/src/errors.rs
  3. 8
      ng-verifier/src/site.rs
  4. 10
      ng-wallet/src/types.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<PubKey, NgError> {
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::*;

@ -48,6 +48,7 @@ pub enum NgError {
RepoNotFound,
BranchNotFound,
StoreNotFound,
UserNotFound,
}
impl Error for NgError {}

@ -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,

@ -476,11 +476,19 @@ impl SensitiveWallet {
Self::V0(v0) => &v0.client,
}
}
pub fn sites(&self) -> Keys<String, SiteV0> {
pub fn site_names(&self) -> Keys<String, SiteV0> {
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),

Loading…
Cancel
Save