Skip to content

Commit

Permalink
refactor: Merge friend module into identity
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Oct 7, 2023
1 parent 08d04a6 commit 0891dce
Show file tree
Hide file tree
Showing 7 changed files with 973 additions and 1,166 deletions.
78 changes: 33 additions & 45 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use store::document::ExtractedRootDocument;
use store::files::FileStore;
use store::friends::FriendsStore;
use store::identity::{IdentityStore, LookupBy};
use store::message::MessageStore;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -78,7 +77,6 @@ pub struct WarpIpfs {
identity_guard: Arc<tokio::sync::Mutex<()>>,
ipfs: Arc<RwLock<Option<Ipfs>>>,
tesseract: Tesseract,
friend_store: Arc<RwLock<Option<FriendsStore>>>,
identity_store: Arc<RwLock<Option<IdentityStore>>>,
message_store: Arc<RwLock<Option<MessageStore>>>,
file_store: Arc<RwLock<Option<FileStore>>>,
Expand Down Expand Up @@ -148,7 +146,6 @@ impl WarpIpfs {
config,
tesseract,
ipfs: Default::default(),
friend_store: Default::default(),
identity_store: Default::default(),
message_store: Default::default(),
file_store: Default::default(),
Expand Down Expand Up @@ -179,9 +176,7 @@ impl WarpIpfs {
async fn initialize_store(&self, init: bool) -> anyhow::Result<()> {
let tesseract = self.tesseract.clone();

if init && self.identity_store.read().is_some() && self.friend_store.read().is_some()
|| self.initialized.load(Ordering::SeqCst)
{
if init && self.identity_store.read().is_some() || self.initialized.load(Ordering::SeqCst) {
warn!("Identity is already loaded");
anyhow::bail!(Error::IdentityExist)
}
Expand Down Expand Up @@ -462,28 +457,29 @@ impl WarpIpfs {
tesseract.clone(),
config.store_setting.auto_push,
self.multipass_tx.clone(),
pb_tx,
&config,
discovery.clone(),
)
.await?;
info!("Identity store initialized");

let friend_store = FriendsStore::new(
ipfs.clone(),
identity_store.clone(),
discovery.clone(),
config.clone(),
tesseract.clone(),
self.multipass_tx.clone(),
pb_tx,
)
.await?;
info!("friends store initialized");
// let friend_store = FriendsStore::new(
// ipfs.clone(),
// identity_store.clone(),
// discovery.clone(),
// config.clone(),
// tesseract.clone(),
// self.multipass_tx.clone(),
//
// )
// .await?;
// info!("friends store initialized");

identity_store.set_friend_store(friend_store.clone()).await;
// identity_store.set_friend_store(friend_store.clone()).await;

*self.identity_store.write() = Some(identity_store.clone());
*self.friend_store.write() = Some(friend_store.clone());
// *self.friend_store.write() = Some(friend_store.clone());

*self.ipfs.write() = Some(ipfs.clone());

Expand All @@ -496,7 +492,7 @@ impl WarpIpfs {
ipfs.clone(),
config.path.map(|path| path.join("messages")),
identity_store,
friend_store,
// friend_store,
discovery,
Some(Box::new(self.clone()) as Box<dyn Constellation>),
false,
Expand All @@ -517,14 +513,6 @@ impl WarpIpfs {
Ok(())
}

pub(crate) async fn friend_store(&self) -> Result<FriendsStore, Error> {
self.identity_store(true).await?;
self.friend_store
.read()
.clone()
.ok_or(Error::MultiPassExtensionUnavailable)
}

pub(crate) async fn identity_store(&self, created: bool) -> Result<IdentityStore, Error> {
let store = self.identity_store_sync()?;
if created && !store.local_id_created().await {
Expand Down Expand Up @@ -578,8 +566,8 @@ impl WarpIpfs {
}

pub(crate) async fn is_blocked_by(&self, pubkey: &DID) -> Result<bool, Error> {
let friends = self.friend_store().await?;
friends.is_blocked_by(pubkey).await
let identity = self.identity_store(true).await?;
identity.is_blocked_by(pubkey).await
}
}

Expand Down Expand Up @@ -1059,77 +1047,77 @@ impl MultiPassImportExport for WarpIpfs {
#[async_trait::async_trait]
impl Friends for WarpIpfs {
async fn send_request(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.send_request(pubkey).await
}

async fn accept_request(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.accept_request(pubkey).await
}

async fn deny_request(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.reject_request(pubkey).await
}

async fn close_request(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.close_request(pubkey).await
}

async fn list_incoming_request(&self) -> Result<Vec<DID>, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.list_incoming_request().await
}

async fn list_outgoing_request(&self) -> Result<Vec<DID>, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.list_outgoing_request().await
}

async fn received_friend_request_from(&self, did: &DID) -> Result<bool, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.received_friend_request_from(did).await
}

async fn sent_friend_request_to(&self, did: &DID) -> Result<bool, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.sent_friend_request_to(did).await
}

async fn remove_friend(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.remove_friend(pubkey, true).await
}

async fn block(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.block(pubkey).await
}

async fn is_blocked(&self, did: &DID) -> Result<bool, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.is_blocked(did).await
}

async fn unblock(&mut self, pubkey: &DID) -> Result<(), Error> {
let mut store = self.friend_store().await?;
let mut store = self.identity_store(true).await?;
store.unblock(pubkey).await
}

async fn block_list(&self) -> Result<Vec<DID>, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.block_list().await.map(Vec::from_iter)
}

async fn list_friends(&self) -> Result<Vec<DID>, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.friends_list().await.map(Vec::from_iter)
}

async fn has_friend(&self, pubkey: &DID) -> Result<bool, Error> {
let store = self.friend_store().await?;
let store = self.identity_store(true).await?;
store.is_friend(pubkey).await
}
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/warp-ipfs/src/store/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::store::get_keypair_did;

use self::{identity::IdentityDocument, utils::GetLocalDag};

use super::friends::Request;
use super::identity::Request;

#[async_trait::async_trait]
pub(crate) trait ToCid: Sized {
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct ExtractedRootDocument {
pub friends: Vec<DID>,
pub block_list: Vec<DID>,
pub block_by_list: Vec<DID>,
pub request: Vec<super::friends::Request>,
pub request: Vec<super::identity::Request>,
pub signature: Option<Vec<u8>>,
}

Expand Down
Loading

0 comments on commit 0891dce

Please sign in to comment.