Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose group description bindings #883

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,15 @@ pub struct FfiCreateGroupOptions {
pub permissions: Option<FfiGroupPermissionsOptions>,
pub group_name: Option<String>,
pub group_image_url_square: Option<String>,
pub group_description: Option<String>,
}

impl FfiCreateGroupOptions {
pub fn into_group_metadata_options(self) -> GroupMetadataOptions {
GroupMetadataOptions {
name: self.group_name,
image_url_square: self.group_image_url_square,
description: self.group_description,
}
}
}
Expand Down Expand Up @@ -866,6 +868,35 @@ impl FfiGroup {
Ok(group_image_url_square)
}

pub async fn update_group_description(
&self,
group_description: String,
) -> Result<(), GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
self.group_id.clone(),
self.created_at_ns,
);

group
.update_group_description(&self.inner_client, group_description)
.await?;

Ok(())
}

pub fn group_description(&self) -> Result<String, GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
self.group_id.clone(),
self.created_at_ns,
);

let group_description = group.group_description()?;

Ok(group_description)
}

pub fn admin_list(&self) -> Result<Vec<String>, GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
Expand Down Expand Up @@ -1538,6 +1569,7 @@ mod tests {
permissions: Some(FfiGroupPermissionsOptions::AdminOnly),
group_name: Some("Group Name".to_string()),
group_image_url_square: Some("url".to_string()),
group_description: Some("group description".to_string()),
},
)
.await
Expand All @@ -1547,6 +1579,7 @@ mod tests {
assert_eq!(members.len(), 2);
assert_eq!(group.group_name().unwrap(), "Group Name");
assert_eq!(group.group_image_url_square().unwrap(), "url");
assert_eq!(group.group_description().unwrap(), "group description");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
Expand Down
3 changes: 3 additions & 0 deletions bindings_node/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ pub struct NapiCreateGroupOptions {
pub permissions: Option<GroupPermissions>,
pub group_name: Option<String>,
pub group_image_url_square: Option<String>,
pub group_description: Option<String>,
}

impl NapiCreateGroupOptions {
pub fn into_group_metadata_options(self) -> GroupMetadataOptions {
GroupMetadataOptions {
name: self.group_name,
image_url_square: self.group_image_url_square,
description: self.group_description,
}
}
}
Expand Down Expand Up @@ -61,6 +63,7 @@ impl NapiConversations {
permissions: None,
group_name: None,
group_image_url_square: None,
group_description: None,
},
};

Expand Down
3 changes: 2 additions & 1 deletion xmtp_mls/src/groups/group_mutable_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl GroupMutableMetadata {
);
attributes.insert(
MetadataField::Description.to_string(),
DEFAULT_GROUP_DESCRIPTION.to_string(),
opts.description
.unwrap_or_else(|| DEFAULT_GROUP_DESCRIPTION.to_string()),
);
attributes.insert(
MetadataField::GroupImageUrlSquare.to_string(),
Expand Down
7 changes: 7 additions & 0 deletions xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ impl UpdateMetadataIntentData {
field_value: group_image_url_square,
}
}

pub fn new_update_group_description(group_description: String) -> Self {
Self {
field_name: MetadataField::Description.to_string(),
field_value: group_description,
}
}
}

impl From<UpdateMetadataIntentData> for Vec<u8> {
Expand Down
41 changes: 41 additions & 0 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub struct MlsGroup {
pub struct GroupMetadataOptions {
pub name: Option<String>,
pub image_url_square: Option<String>,
pub description: Option<String>,
}

impl Clone for MlsGroup {
Expand Down Expand Up @@ -666,6 +667,40 @@ impl MlsGroup {
}
}

pub async fn update_group_description<ApiClient>(
&self,
client: &Client<ApiClient>,
group_description: String,
) -> Result<(), GroupError>
where
ApiClient: XmtpApi,
{
let conn = self.context.store.conn()?;
let intent_data: Vec<u8> =
UpdateMetadataIntentData::new_update_group_description(group_description).into();
let intent = conn.insert_group_intent(NewGroupIntent::new(
IntentKind::MetadataUpdate,
self.group_id.clone(),
intent_data,
))?;

self.sync_until_intent_resolved(conn, intent.id, client)
.await
}

pub fn group_description(&self) -> Result<String, GroupError> {
let mutable_metadata = self.mutable_metadata()?;
match mutable_metadata
.attributes
.get(&MetadataField::Description.to_string())
{
Some(group_description) => Ok(group_description.clone()),
None => Err(GroupError::GroupMutableMetadata(
GroupMutableMetadataError::MissingExtension,
)),
}
}

pub async fn update_group_image_url_square<ApiClient>(
&self,
client: &Client<ApiClient>,
Expand Down Expand Up @@ -1749,6 +1784,7 @@ mod tests {
GroupMetadataOptions {
name: Some("Group Name".to_string()),
image_url_square: Some("url".to_string()),
description: Some("group description".to_string()),
},
)
.unwrap();
Expand All @@ -1762,9 +1798,14 @@ mod tests {
.attributes
.get(&MetadataField::GroupImageUrlSquare.to_string())
.unwrap();
let amal_group_description: &String = binding
.attributes
.get(&MetadataField::Description.to_string())
.unwrap();

assert_eq!(amal_group_name, "Group Name");
assert_eq!(amal_group_image_url, "url");
assert_eq!(amal_group_description, "group description");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
Expand Down