Skip to content

Commit

Permalink
chore: Export document on update and fix filtering from remote source
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Sep 30, 2023
1 parent 7ef581d commit 2de0573
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ impl IdentityStore {
return Ok(package);
}
Ok(Ok(Err(e))) => {
log::error!("Error registering identity to {peer_id}: {e}");
log::error!("Error importing from {peer_id}: {e}");
break;
}
Ok(Err(Canceled)) => {
Expand All @@ -1629,6 +1629,53 @@ impl IdentityStore {
Err(Error::IdentityDoesntExist)
}

pub async fn export_identity_document(&mut self) -> Result<(), Error> {
let document = self.own_identity_document().await?;

if let Some(sender) = self.identity_command.as_mut() {
if let DiscoveryConfig::Namespace {
discovery_type: DiscoveryType::RzPoint { addresses },
..
} = self.discovery.discovery_config()
{
for addr in addresses {
let Some(peer_id) = addr.peer_id() else {
continue;
};

let (tx, rx) = futures::channel::oneshot::channel();
let _ = sender
.send(shuttle::identity::IdentityCommand::Synchronized {
peer_id,
identity: document.clone().into(),
package: None,
response: tx,
})
.await;

match tokio::time::timeout(Duration::from_secs(20), rx).await {
Ok(Ok(Ok(_))) => {
break;
}
Ok(Ok(Err(e))) => {
log::error!("Error exporting to {peer_id}: {e}");
break;
}
Ok(Err(Canceled)) => {
log::error!("Channel been unexpectedly closed for {peer_id}");
continue;
}
Err(_) => {
log::error!("Request timeout for {peer_id}");
continue;
}
}
}
}
}
Ok(())
}

pub async fn export_identity_remote(&mut self, package: Vec<u8>) -> Result<(), Error> {
let document = self.own_identity_document().await?;

Expand Down Expand Up @@ -1658,7 +1705,7 @@ impl IdentityStore {
break;
}
Ok(Ok(Err(e))) => {
log::error!("Error registering identity to {peer_id}: {e}");
log::error!("Error exporting to {peer_id}: {e}");
break;
}
Ok(Err(Canceled)) => {
Expand Down Expand Up @@ -1866,6 +1913,7 @@ impl IdentityStore {

let mut list = idents_docs
.iter()
.filter(|document| document.did != own_did)
.filter_map(|doc| doc.resolve().ok())
.collect::<Vec<_>>();

Expand All @@ -1883,7 +1931,9 @@ impl IdentityStore {
let ident_cid = identity.to_cid(&self.ipfs).await?;
root_document.identity = ident_cid;

self.set_root_document(root_document).await
self.set_root_document(root_document).await?;
let _ = self.export_identity_document().await;
Ok(())
}

//TODO: Add a check to check directly through pubsub_peer (maybe even using connected peers) or through a separate server
Expand Down

0 comments on commit 2de0573

Please sign in to comment.