From 494097c276073dd64c6a9f1032f143388a0ef63e Mon Sep 17 00:00:00 2001 From: Mojtaba Chenani Date: Mon, 16 Dec 2024 23:15:46 +0100 Subject: [PATCH] make group.metadata async --- bindings_ffi/src/mls.rs | 12 ++++++------ bindings_node/src/conversation.rs | 6 ++++-- bindings_wasm/src/conversation.rs | 4 +++- examples/cli/serializable.rs | 3 ++- xmtp_mls/src/groups/mod.rs | 27 ++++++++++++++++----------- xmtp_mls/src/subscriptions.rs | 2 +- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/bindings_ffi/src/mls.rs b/bindings_ffi/src/mls.rs index 65d4baed8..ad9914533 100644 --- a/bindings_ffi/src/mls.rs +++ b/bindings_ffi/src/mls.rs @@ -1268,13 +1268,13 @@ impl FfiConversation { Ok(()) } - pub fn find_messages( + pub async fn find_messages( &self, opts: FfiListMessagesOptions, ) -> Result, GenericError> { let delivery_status = opts.delivery_status.map(|status| status.into()); let direction = opts.direction.map(|dir| dir.into()); - let kind = match self.conversation_type()? { + let kind = match self.conversation_type().await? { FfiConversationType::Group => None, FfiConversationType::Dm => Some(GroupMessageKind::Application), FfiConversationType::Sync => None, @@ -1544,9 +1544,9 @@ impl FfiConversation { self.inner.added_by_inbox_id().map_err(Into::into) } - pub fn group_metadata(&self) -> Result, GenericError> { + pub async fn group_metadata(&self) -> Result, GenericError> { let provider = self.inner.mls_provider()?; - let metadata = self.inner.metadata(&provider)?; + let metadata = self.inner.metadata(&provider).await?; Ok(Arc::new(FfiConversationMetadata { inner: Arc::new(metadata), })) @@ -1556,9 +1556,9 @@ impl FfiConversation { self.inner.dm_inbox_id().map_err(Into::into) } - pub fn conversation_type(&self) -> Result { + pub async fn conversation_type(&self) -> Result { let provider = self.inner.mls_provider()?; - let conversation_type = self.inner.conversation_type(&provider)?; + let conversation_type = self.inner.conversation_type(&provider).await?; Ok(conversation_type.into()) } } diff --git a/bindings_node/src/conversation.rs b/bindings_node/src/conversation.rs index e8a4066d0..b520af793 100644 --- a/bindings_node/src/conversation.rs +++ b/bindings_node/src/conversation.rs @@ -161,7 +161,7 @@ impl Conversation { } #[napi] - pub fn find_messages(&self, opts: Option) -> Result> { + pub async fn find_messages(&self, opts: Option) -> Result> { let opts = opts.unwrap_or_default(); let group = MlsGroup::new( self.inner_client.clone(), @@ -171,6 +171,7 @@ impl Conversation { let provider = group.mls_provider().map_err(ErrorWrapper::from)?; let conversation_type = group .conversation_type(&provider) + .await .map_err(ErrorWrapper::from)?; let kind = match conversation_type { ConversationType::Group => None, @@ -604,7 +605,7 @@ impl Conversation { } #[napi] - pub fn group_metadata(&self) -> Result { + pub async fn group_metadata(&self) -> Result { let group = MlsGroup::new( self.inner_client.clone(), self.group_id.clone(), @@ -613,6 +614,7 @@ impl Conversation { let metadata = group .metadata(&group.mls_provider().map_err(ErrorWrapper::from)?) + .await .map_err(ErrorWrapper::from)?; Ok(GroupMetadata { inner: metadata }) diff --git a/bindings_wasm/src/conversation.rs b/bindings_wasm/src/conversation.rs index d211d5cae..42ca2f06e 100644 --- a/bindings_wasm/src/conversation.rs +++ b/bindings_wasm/src/conversation.rs @@ -194,6 +194,7 @@ impl Conversation { .map_err(|e| JsError::new(&format!("{e}")))?; let conversation_type = group .conversation_type(&provider) + .await .map_err(|e| JsError::new(&format!("{e}")))?; let kind = match conversation_type { ConversationType::Group => None, @@ -528,7 +529,7 @@ impl Conversation { } #[wasm_bindgen(js_name = groupMetadata)] - pub fn group_metadata(&self) -> Result { + pub async fn group_metadata(&self) -> Result { let group = self.to_mls_group(); let metadata = group .metadata( @@ -536,6 +537,7 @@ impl Conversation { .mls_provider() .map_err(|e| JsError::new(&format!("{e}")))?, ) + .await .map_err(|e| JsError::new(&format!("{e}")))?; Ok(GroupMetadata { inner: metadata }) diff --git a/examples/cli/serializable.rs b/examples/cli/serializable.rs index 429876608..545081638 100644 --- a/examples/cli/serializable.rs +++ b/examples/cli/serializable.rs @@ -35,7 +35,8 @@ impl SerializableGroup { .mls_provider() .expect("MLS Provider could not be created"), ) - .expect("could not load metadata"); + .await + .unwrap(); let permissions = group.permissions().expect("could not load permissions"); Self { diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index b1fb6f92c..35662bc7d 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -896,7 +896,7 @@ impl MlsGroup { /// to perform these updates. pub async fn update_group_name(&self, group_name: String) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } let intent_data: Vec = @@ -914,7 +914,7 @@ impl MlsGroup { metadata_field: Option, ) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } if permission_update_type == PermissionUpdateType::UpdateMetadata @@ -955,7 +955,7 @@ impl MlsGroup { group_description: String, ) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } let intent_data: Vec = @@ -984,7 +984,7 @@ impl MlsGroup { group_image_url_square: String, ) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } let intent_data: Vec = @@ -1017,7 +1017,7 @@ impl MlsGroup { pinned_frame_url: String, ) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } let intent_data: Vec = @@ -1079,11 +1079,11 @@ impl MlsGroup { } /// Retrieves the conversation type of the group from the group's metadata extension. - pub fn conversation_type( + pub async fn conversation_type( &self, provider: &XmtpOpenMlsProvider, ) -> Result { - let metadata = self.metadata(provider)?; + let metadata = self.metadata(provider).await?; Ok(metadata.conversation_type) } @@ -1094,7 +1094,7 @@ impl MlsGroup { inbox_id: String, ) -> Result<(), GroupError> { let provider = self.client.mls_provider()?; - if self.metadata(&provider)?.conversation_type == ConversationType::Dm { + if self.metadata(&provider).await?.conversation_type == ConversationType::Dm { return Err(GroupError::DmGroupMetadataForbidden); } let intent_action_type = match action_type { @@ -1183,10 +1183,14 @@ impl MlsGroup { } /// Get the `GroupMetadata` of the group. - pub fn metadata(&self, provider: &XmtpOpenMlsProvider) -> Result { - self.load_mls_group_with_lock(provider, |mls_group| { - extract_group_metadata(&mls_group).map_err(Into::into) + pub async fn metadata( + &self, + provider: &XmtpOpenMlsProvider, + ) -> Result { + self.load_mls_group_with_lock_async(provider, |mls_group| { + futures::future::ready(extract_group_metadata(&mls_group).map_err(Into::into)) }) + .await } /// Get the `GroupMutableMetadata` of the group. @@ -3113,6 +3117,7 @@ pub(crate) mod tests { let protected_metadata: GroupMetadata = amal_group .metadata(&amal_group.mls_provider().unwrap()) + .await .unwrap(); assert_eq!( protected_metadata.conversation_type, diff --git a/xmtp_mls/src/subscriptions.rs b/xmtp_mls/src/subscriptions.rs index 23179946b..2efdd4a07 100644 --- a/xmtp_mls/src/subscriptions.rs +++ b/xmtp_mls/src/subscriptions.rs @@ -344,7 +344,7 @@ where } WelcomeOrGroup::Group(group) => group?, }; - let metadata = group.metadata(&provider)?; + let metadata = group.metadata(&provider).await?; Ok((metadata, group)) } }