Skip to content

Commit

Permalink
Merge branch 'main' into wasm-action
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Jun 25, 2024
2 parents ed3b2a3 + e1b16c6 commit f0fe59a
Show file tree
Hide file tree
Showing 18 changed files with 459 additions and 422 deletions.
177 changes: 86 additions & 91 deletions extensions/warp-ipfs/examples/identity-interface.rs

Large diffs are not rendered by default.

21 changes: 4 additions & 17 deletions extensions/warp-ipfs/examples/ipfs-friends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::Duration;
use futures::StreamExt;

use warp::crypto::rand::{self, prelude::*};
use warp::error::Error;
use warp::multipass::identity::{Identifier, Identity};
use warp::multipass::{MultiPass, MultiPassEventKind};
use warp_ipfs::config::Config;
Expand Down Expand Up @@ -77,20 +76,14 @@ async fn main() -> anyhow::Result<()> {
println!("{} Outgoing request:", username(&ident_a));

for outgoing in account_a.list_outgoing_request().await? {
let ident = account_a
.get_identity(Identifier::from(outgoing))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let ident = account_a.get_identity(Identifier::from(outgoing)).await?;
println!("To: {}", username(&ident));
println!();
}

println!("{} Incoming request:", username(&ident_b));
for incoming in account_b.list_incoming_request().await? {
let ident = account_b
.get_identity(Identifier::from(incoming))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let ident = account_b.get_identity(Identifier::from(incoming)).await?;

println!("From: {}", username(&ident));
println!();
Expand All @@ -115,21 +108,15 @@ async fn main() -> anyhow::Result<()> {

println!("{} Friends:", username(&ident_a));
for friend in account_a.list_friends().await? {
let friend = account_a
.get_identity(Identifier::did_key(friend))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let friend = account_a.get_identity(Identifier::did_key(friend)).await?;
println!("Username: {}", username(&friend));
println!("Public Key: {}", friend.did_key());
println!();
}

println!("{} Friends:", username(&ident_b));
for friend in account_b.list_friends().await? {
let friend = account_b
.get_identity(Identifier::did_key(friend))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let friend = account_b.get_identity(Identifier::did_key(friend)).await?;
println!("Username: {}", username(&friend));
println!("Public Key: {}", friend.did_key());
println!();
Expand Down
70 changes: 16 additions & 54 deletions extensions/warp-ipfs/examples/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,8 @@ async fn main() -> anyhow::Result<()> {
warp::multipass::MultiPassEventKind::FriendRequestReceived { from: did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());
if !opt.autoaccept_friend {
writeln!(stdout, "> Pending request from {username}. Do \"/accept-request {did}\" to accept.")?;
} else {
Expand All @@ -284,129 +282,103 @@ async fn main() -> anyhow::Result<()> {
warp::multipass::MultiPassEventKind::FriendRequestSent { to: did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> A request has been sent to {username}. Do \"/close-request {did}\" to if you wish to close the request")?;
}
warp::multipass::MultiPassEventKind::IncomingFriendRequestRejected { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> You've rejected {username} request")?;
},
warp::multipass::MultiPassEventKind::OutgoingFriendRequestRejected { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} rejected your request")?;
},
warp::multipass::MultiPassEventKind::IncomingFriendRequestClosed { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} has retracted their request")?;
},
warp::multipass::MultiPassEventKind::OutgoingFriendRequestClosed { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> Request for {username} has been retracted")?;
},
warp::multipass::MultiPassEventKind::FriendAdded { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> You are now friends with {username}")?;
},
warp::multipass::MultiPassEventKind::FriendRemoved { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} has been removed from friends list")?;
},
warp::multipass::MultiPassEventKind::IdentityOnline { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} has came online")?;
},
warp::multipass::MultiPassEventKind::IdentityOffline { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} went offline")?;
},
warp::multipass::MultiPassEventKind::Blocked { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} was blocked")?;
},
warp::multipass::MultiPassEventKind::Unblocked { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());
writeln!(stdout, "> {username} was unblocked")?;
},
warp::multipass::MultiPassEventKind::UnblockedBy { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} unblocked you")?;
},
warp::multipass::MultiPassEventKind::BlockedBy { did } => {
let username = new_account
.get_identity(Identifier::did_key(did.clone())).await
.ok()
.and_then(|list| list.first().cloned())
.map(|ident| ident.username())
.unwrap_or_else(|| did.to_string());
.unwrap_or_else(|_| did.to_string());

writeln!(stdout, "> {username} blocked you")?;
},
Expand Down Expand Up @@ -962,13 +934,7 @@ async fn main() -> anyhow::Result<()> {
}
},
Lookup(id) => {
let identities = match new_account.get_identity(id).await {
Ok(identity) => identity,
Err(e) => {
writeln!(stdout, "Error obtaining own identity: {e}")?;
continue;
}
};
let identities = new_account.get_identity(id).collect::<Vec<_>>().await;
let mut table = Table::new();
table.set_header(vec!["Username", "Public Key", "Created", "Last Updated", "Status Message", "Banner", "Picture", "Platform", "Status"]);
for identity in identities {
Expand Down Expand Up @@ -1010,11 +976,7 @@ async fn get_username(account: &dyn MultiPass, did: DID) -> String {
account
.get_identity(Identifier::did_key(did.clone()))
.await
.map(|list| {
list.first()
.map(|identity| format!("{}#{}", identity.username(), identity.short_id()))
.unwrap_or(did.to_string())
})
.map(|id| format!("{}#{}", id.username(), id.short_id()))
.unwrap_or(did.to_string())
}

Expand Down
20 changes: 4 additions & 16 deletions extensions/warp-ipfs/examples/wasm-ipfs-friends/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,14 @@ pub async fn run() -> Result<(), JsError> {
body.append_p(&format!("{} Outgoing request:", username(&ident_a)))?;

for outgoing in account_a.list_outgoing_request().await? {
let ident = account_a
.get_identity(Identifier::from(outgoing))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let ident = account_a.get_identity(Identifier::from(outgoing)).await?;
body.append_p(&format!("To: {}", username(&ident)))?;
body.append_p("")?;
}

body.append_p(&format!("{} Incoming request:", username(&ident_b)))?;
for incoming in account_b.list_incoming_request().await? {
let ident = account_b
.get_identity(Identifier::from(incoming))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let ident = account_b.get_identity(Identifier::from(incoming)).await?;

body.append_p(&format!("From: {}", username(&ident)))?;
body.append_p("")?;
Expand All @@ -131,21 +125,15 @@ pub async fn run() -> Result<(), JsError> {

body.append_p(&format!("{} Friends:", username(&ident_a)))?;
for friend in account_a.list_friends().await? {
let friend = account_a
.get_identity(Identifier::did_key(friend))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let friend = account_a.get_identity(Identifier::did_key(friend)).await?;
body.append_p(&format!("Username: {}", username(&friend)))?;
body.append_p(&format!("Public Key: {}", friend.did_key()))?;
body.append_p("")?;
}

body.append_p(&format!("{} Friends:", username(&ident_b)))?;
for friend in account_b.list_friends().await? {
let friend = account_b
.get_identity(Identifier::did_key(friend))
.await
.and_then(|list| list.first().cloned().ok_or(Error::IdentityDoesntExist))?;
let friend = account_b.get_identity(Identifier::did_key(friend)).await?;
body.append_p(&format!("Username: {}", username(&friend)))?;
body.append_p(&format!("Public Key: {}", friend.did_key()))?;
body.append_p("")?;
Expand Down
53 changes: 28 additions & 25 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use config::Config;
use store::document::ResolvedRootDocument;
use store::event_subscription::EventSubscription;
use store::files::FileStore;
use store::identity::{IdentityStore, LookupBy};
use store::identity::IdentityStore;
use store::message::MessageStore;
use utils::ExtensionType;
use warp::constellation::directory::Directory;
Expand All @@ -47,8 +47,9 @@ use warp::multipass::identity::{
Identifier, Identity, IdentityImage, IdentityProfile, IdentityUpdate, Relationship,
};
use warp::multipass::{
identity, Friends, IdentityImportOption, IdentityInformation, ImportLocation, LocalIdentity,
MultiPass, MultiPassEvent, MultiPassEventKind, MultiPassEventStream, MultiPassImportExport,
identity, Friends, GetIdentity, IdentityImportOption, IdentityInformation, ImportLocation,
LocalIdentity, MultiPass, MultiPassEvent, MultiPassEventKind, MultiPassEventStream,
MultiPassImportExport,
};
use warp::raygun::{
AttachmentEventStream, Conversation, ConversationSettings, EmbedState, GroupSettings, Location,
Expand Down Expand Up @@ -743,6 +744,18 @@ impl WarpIpfs {
.ok_or(Error::ConstellationExtensionUnavailable)
}

pub(crate) fn direct_identity_store(&self) -> std::result::Result<IdentityStore, Error> {
let store = self
.inner
.components
.read()
.as_ref()
.map(|com| com.identity_store.clone())
.ok_or(Error::MultiPassExtensionUnavailable)?;

Ok(store)
}

pub(crate) fn ipfs(&self) -> Result<Ipfs, Error> {
self.inner
.components
Expand All @@ -751,11 +764,6 @@ impl WarpIpfs {
.map(|com| com.ipfs.clone())
.ok_or(Error::MultiPassExtensionUnavailable)
}

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

impl Extension for WarpIpfs {
Expand Down Expand Up @@ -852,16 +860,13 @@ impl MultiPass for WarpIpfs {
Ok(profile)
}

async fn get_identity(&self, id: Identifier) -> Result<Vec<Identity>, Error> {
let store = self.identity_store(true).await?;

let kind = match id {
Identifier::DID(pk) => LookupBy::DidKey(pk),
Identifier::Username(username) => LookupBy::Username(username),
Identifier::DIDList(list) => LookupBy::DidKeys(list),
fn get_identity(&self, id: Identifier) -> GetIdentity {
let store = match self.direct_identity_store() {
Ok(store) => store,
_ => return GetIdentity::new(id, stream::empty().boxed()),
};

store.lookup(kind).await
store.lookup(id)
}
}

Expand Down Expand Up @@ -1436,15 +1441,13 @@ impl IdentityInformation for WarpIpfs {
}

async fn identity_relationship(&self, did: &DID) -> Result<identity::Relationship, Error> {
self.get_identity(Identifier::did_key(did.clone()))
.await?
.first()
.ok_or(Error::IdentityDoesntExist)?;
let friends = self.has_friend(did).await?;
let received_friend_request = self.received_friend_request_from(did).await?;
let sent_friend_request = self.sent_friend_request_to(did).await?;
let blocked = self.is_blocked(did).await?;
let blocked_by = self.is_blocked_by(did).await?;
let store = self.identity_store(true).await?;
store.lookup(did).await?;
let friends = store.is_friend(did).await?;
let received_friend_request = store.received_friend_request_from(did).await?;
let sent_friend_request = store.sent_friend_request_to(did).await?;
let blocked = store.is_blocked(did).await?;
let blocked_by = store.is_blocked_by(did).await?;

let mut relationship = Relationship::default();
relationship.set_friends(friends);
Expand Down
Loading

0 comments on commit f0fe59a

Please sign in to comment.