Skip to content

Commit

Permalink
Allow creating conversations with inboxIds (#1511)
Browse files Browse the repository at this point in the history
* make inbox id methods

* cargo fmt

* cargo clippy
  • Loading branch information
nplasterer authored Jan 16, 2025
1 parent 61852d3 commit dad64d5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
68 changes: 68 additions & 0 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,62 @@ impl FfiConversations {
Ok(Arc::new(convo.into()))
}

pub async fn create_group_with_inbox_ids(
&self,
inbox_ids: Vec<String>,
opts: FfiCreateGroupOptions,
) -> Result<Arc<FfiConversation>, 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,
Expand All @@ -932,6 +988,18 @@ impl FfiConversations {
.map_err(Into::into)
}

pub async fn create_dm_with_inbox_id(
&self,
inbox_id: String,
) -> Result<Arc<FfiConversation>, 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<u8>,
Expand Down
25 changes: 20 additions & 5 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,20 @@ where
Ok(group)
}

pub async fn create_group_with_inbox_ids(
&self,
inbox_ids: &[InboxId],
permissions_policy_set: Option<PolicySet>,
opts: GroupMetadataOptions,
) -> Result<MlsGroup<Self>, 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<MlsGroup<Self>, ClientError> {
tracing::info!("creating dm with address: {}", account_address);
Expand All @@ -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<MlsGroup<Self>, ClientError> {
tracing::info!("creating dm with {}", dm_target_inbox_id);
let provider = self.mls_provider()?;

let group: MlsGroup<Client<ApiClient, V>> = 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
Expand Down
7 changes: 3 additions & 4 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
10 changes: 5 additions & 5 deletions xmtp_mls/src/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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()])
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit dad64d5

Please sign in to comment.