From dad64d502009063904d973e5c68457081051a5ec Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 16 Jan 2025 12:34:57 -0800 Subject: [PATCH] Allow creating conversations with inboxIds (#1511) * make inbox id methods * cargo fmt * cargo clippy --- bindings_ffi/src/mls.rs | 68 +++++++++++++++++++++++++++++++++++ xmtp_mls/src/client.rs | 25 ++++++++++--- xmtp_mls/src/groups/mod.rs | 7 ++-- xmtp_mls/src/subscriptions.rs | 10 +++--- 4 files changed, 96 insertions(+), 14 deletions(-) diff --git a/bindings_ffi/src/mls.rs b/bindings_ffi/src/mls.rs index 3343cc510..983b81963 100644 --- a/bindings_ffi/src/mls.rs +++ b/bindings_ffi/src/mls.rs @@ -920,6 +920,62 @@ impl FfiConversations { Ok(Arc::new(convo.into())) } + pub async fn create_group_with_inbox_ids( + &self, + inbox_ids: Vec, + opts: FfiCreateGroupOptions, + ) -> Result, GenericError> { + log::info!( + "creating group with account inbox ids: {}", + inbox_ids.join(", ") + ); + + if let Some(FfiGroupPermissionsOptions::CustomPolicy) = opts.permissions { + if opts.custom_permission_policy_set.is_none() { + return Err(GenericError::Generic { + err: "CustomPolicy must include policy set".to_string(), + }); + } + } else if opts.custom_permission_policy_set.is_some() { + return Err(GenericError::Generic { + err: "Only CustomPolicy may specify a policy set".to_string(), + }); + } + + let metadata_options = opts.clone().into_group_metadata_options(); + + let group_permissions = match opts.permissions { + Some(FfiGroupPermissionsOptions::Default) => { + Some(xmtp_mls::groups::PreconfiguredPolicies::Default.to_policy_set()) + } + Some(FfiGroupPermissionsOptions::AdminOnly) => { + Some(xmtp_mls::groups::PreconfiguredPolicies::AdminsOnly.to_policy_set()) + } + Some(FfiGroupPermissionsOptions::CustomPolicy) => { + if let Some(policy_set) = opts.custom_permission_policy_set { + Some(policy_set.try_into()?) + } else { + None + } + } + _ => None, + }; + + let convo = if inbox_ids.is_empty() { + let group = self + .inner_client + .create_group(group_permissions, metadata_options)?; + group.sync().await?; + group + } else { + self.inner_client + .create_group_with_inbox_ids(&inbox_ids, group_permissions, metadata_options) + .await? + }; + + Ok(Arc::new(convo.into())) + } + pub async fn create_dm( &self, account_address: String, @@ -932,6 +988,18 @@ impl FfiConversations { .map_err(Into::into) } + pub async fn create_dm_with_inbox_id( + &self, + inbox_id: String, + ) -> Result, GenericError> { + log::info!("creating dm with target inbox_id: {}", inbox_id); + self.inner_client + .create_dm_by_inbox_id(inbox_id) + .await + .map(|g| Arc::new(g.into())) + .map_err(Into::into) + } + pub async fn process_streamed_welcome_message( &self, envelope_bytes: Vec, diff --git a/xmtp_mls/src/client.rs b/xmtp_mls/src/client.rs index 09b626348..09a22d239 100644 --- a/xmtp_mls/src/client.rs +++ b/xmtp_mls/src/client.rs @@ -546,6 +546,20 @@ where Ok(group) } + pub async fn create_group_with_inbox_ids( + &self, + inbox_ids: &[InboxId], + permissions_policy_set: Option, + opts: GroupMetadataOptions, + ) -> Result, ClientError> { + tracing::info!("creating group"); + let group = self.create_group(permissions_policy_set, opts)?; + + group.add_members_by_inbox_id(inbox_ids).await?; + + Ok(group) + } + /// Create a new Direct Message with the default settings pub async fn create_dm(&self, account_address: String) -> Result, ClientError> { tracing::info!("creating dm with address: {}", account_address); @@ -563,25 +577,26 @@ where } }; - self.create_dm_by_inbox_id(&provider, inbox_id).await + self.create_dm_by_inbox_id(inbox_id).await } /// Create a new Direct Message with the default settings - pub(crate) async fn create_dm_by_inbox_id( + pub async fn create_dm_by_inbox_id( &self, - provider: &XmtpOpenMlsProvider, dm_target_inbox_id: InboxId, ) -> Result, ClientError> { tracing::info!("creating dm with {}", dm_target_inbox_id); + let provider = self.mls_provider()?; + let group: MlsGroup> = MlsGroup::create_dm_and_insert( - provider, + &provider, Arc::new(self.clone()), GroupMembershipState::Allowed, dm_target_inbox_id.clone(), )?; group - .add_members_by_inbox_id_with_provider(provider, &[dm_target_inbox_id]) + .add_members_by_inbox_id_with_provider(&provider, &[dm_target_inbox_id]) .await?; // notify any streams of the new group diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 5a04373d2..ce62993dc 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -2147,14 +2147,13 @@ pub(crate) mod tests { let bo_wallet = generate_local_wallet(); let bo = ClientBuilder::new_test_client(&bo_wallet).await; - let bo_provider = bo.mls_provider().unwrap(); let bo_dm = bo - .create_dm_by_inbox_id(&bo_provider, alix.inbox_id().to_string()) + .create_dm_by_inbox_id(alix.inbox_id().to_string()) .await .unwrap(); let alix_dm = alix - .create_dm_by_inbox_id(&alix_provider, bo.inbox_id().to_string()) + .create_dm_by_inbox_id(bo.inbox_id().to_string()) .await .unwrap(); @@ -3603,7 +3602,7 @@ pub(crate) mod tests { // Amal creates a dm group targetting bola let amal_dm = amal - .create_dm_by_inbox_id(&amal.mls_provider().unwrap(), bola.inbox_id().to_string()) + .create_dm_by_inbox_id(bola.inbox_id().to_string()) .await .unwrap(); diff --git a/xmtp_mls/src/subscriptions.rs b/xmtp_mls/src/subscriptions.rs index 5b03302dd..684b613da 100644 --- a/xmtp_mls/src/subscriptions.rs +++ b/xmtp_mls/src/subscriptions.rs @@ -988,7 +988,7 @@ pub(crate) mod tests { }, ); - alix.create_dm_by_inbox_id(&alix.mls_provider().unwrap(), bo.inbox_id().to_string()) + alix.create_dm_by_inbox_id(bo.inbox_id().to_string()) .await .unwrap(); @@ -1038,7 +1038,7 @@ pub(crate) mod tests { let result = notify.wait_for_delivery().await; assert!(result.is_err(), "Stream unexpectedly received a Group"); - alix.create_dm_by_inbox_id(&alix.mls_provider().unwrap(), bo.inbox_id().to_string()) + alix.create_dm_by_inbox_id(bo.inbox_id().to_string()) .await .unwrap(); notify.wait_for_delivery().await.unwrap(); @@ -1061,7 +1061,7 @@ pub(crate) mod tests { notify_pointer.notify_one(); }); - alix.create_dm_by_inbox_id(&alix.mls_provider().unwrap(), bo.inbox_id().to_string()) + alix.create_dm_by_inbox_id(bo.inbox_id().to_string()) .await .unwrap(); notify.wait_for_delivery().await.unwrap(); @@ -1071,7 +1071,7 @@ pub(crate) mod tests { } let dm = bo - .create_dm_by_inbox_id(&bo.mls_provider().unwrap(), alix.inbox_id().to_string()) + .create_dm_by_inbox_id(alix.inbox_id().to_string()) .await .unwrap(); dm.add_members_by_inbox_id(&[alix.inbox_id()]) @@ -1114,7 +1114,7 @@ pub(crate) mod tests { .unwrap(); let alix_dm = alix - .create_dm_by_inbox_id(&alix.mls_provider().unwrap(), bo.inbox_id().to_string()) + .create_dm_by_inbox_id(bo.inbox_id().to_string()) .await .unwrap();