Skip to content

Commit

Permalink
make group.metadata async
Browse files Browse the repository at this point in the history
  • Loading branch information
mchenani committed Dec 16, 2024
1 parent eb4baf4 commit 494097c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
12 changes: 6 additions & 6 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,13 +1268,13 @@ impl FfiConversation {
Ok(())
}

pub fn find_messages(
pub async fn find_messages(
&self,
opts: FfiListMessagesOptions,
) -> Result<Vec<FfiMessage>, 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,
Expand Down Expand Up @@ -1544,9 +1544,9 @@ impl FfiConversation {
self.inner.added_by_inbox_id().map_err(Into::into)
}

pub fn group_metadata(&self) -> Result<Arc<FfiConversationMetadata>, GenericError> {
pub async fn group_metadata(&self) -> Result<Arc<FfiConversationMetadata>, 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),
}))
Expand All @@ -1556,9 +1556,9 @@ impl FfiConversation {
self.inner.dm_inbox_id().map_err(Into::into)
}

pub fn conversation_type(&self) -> Result<FfiConversationType, GenericError> {
pub async fn conversation_type(&self) -> Result<FfiConversationType, GenericError> {
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())
}
}
Expand Down
6 changes: 4 additions & 2 deletions bindings_node/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Conversation {
}

#[napi]
pub fn find_messages(&self, opts: Option<ListMessagesOptions>) -> Result<Vec<Message>> {
pub async fn find_messages(&self, opts: Option<ListMessagesOptions>) -> Result<Vec<Message>> {
let opts = opts.unwrap_or_default();
let group = MlsGroup::new(
self.inner_client.clone(),
Expand All @@ -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,
Expand Down Expand Up @@ -604,7 +605,7 @@ impl Conversation {
}

#[napi]
pub fn group_metadata(&self) -> Result<GroupMetadata> {
pub async fn group_metadata(&self) -> Result<GroupMetadata> {
let group = MlsGroup::new(
self.inner_client.clone(),
self.group_id.clone(),
Expand All @@ -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 })
Expand Down
4 changes: 3 additions & 1 deletion bindings_wasm/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -528,14 +529,15 @@ impl Conversation {
}

#[wasm_bindgen(js_name = groupMetadata)]
pub fn group_metadata(&self) -> Result<GroupMetadata, JsError> {
pub async fn group_metadata(&self) -> Result<GroupMetadata, JsError> {
let group = self.to_mls_group();
let metadata = group
.metadata(
&group
.mls_provider()
.map_err(|e| JsError::new(&format!("{e}")))?,
)
.await
.map_err(|e| JsError::new(&format!("{e}")))?;

Ok(GroupMetadata { inner: metadata })
Expand Down
3 changes: 2 additions & 1 deletion examples/cli/serializable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 16 additions & 11 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
/// 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<u8> =
Expand All @@ -914,7 +914,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
metadata_field: Option<MetadataField>,
) -> 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
Expand Down Expand Up @@ -955,7 +955,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
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<u8> =
Expand Down Expand Up @@ -984,7 +984,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
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<u8> =
Expand Down Expand Up @@ -1017,7 +1017,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
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<u8> =
Expand Down Expand Up @@ -1079,11 +1079,11 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
}

/// 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<ConversationType, GroupError> {
let metadata = self.metadata(provider)?;
let metadata = self.metadata(provider).await?;
Ok(metadata.conversation_type)
}

Expand All @@ -1094,7 +1094,7 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
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 {
Expand Down Expand Up @@ -1183,10 +1183,14 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {
}

/// Get the `GroupMetadata` of the group.
pub fn metadata(&self, provider: &XmtpOpenMlsProvider) -> Result<GroupMetadata, GroupError> {
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<GroupMetadata, GroupError> {
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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ where
}
WelcomeOrGroup::Group(group) => group?,
};
let metadata = group.metadata(&provider)?;
let metadata = group.metadata(&provider).await?;
Ok((metadata, group))
}
}
Expand Down

0 comments on commit 494097c

Please sign in to comment.