Skip to content

Commit

Permalink
Expose group metadata (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas authored Feb 20, 2024
1 parent 6e67f37 commit bfdda64
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bindings_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum GenericError {
GroupError(#[from] xmtp_mls::groups::GroupError),
#[error("Signature: {0}")]
Signature(#[from] xmtp_cryptography::signature::SignatureError),
#[error("Group metadata: {0}")]
GroupMetadata(#[from] xmtp_mls::groups::group_metadata::GroupMetadataError),
#[error("Generic {err}")]
Generic { err: String },
}
Expand Down
48 changes: 48 additions & 0 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use tokio::sync::{oneshot, oneshot::Sender};
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_mls::builder::IdentityStrategy;
use xmtp_mls::builder::LegacyIdentity;
use xmtp_mls::groups::group_metadata::ConversationType;
use xmtp_mls::groups::group_metadata::GroupMetadata;
use xmtp_mls::groups::PreconfiguredPolicies;
use xmtp_mls::{
builder::ClientBuilder,
client::Client as MlsClient,
Expand Down Expand Up @@ -173,6 +176,15 @@ pub enum GroupPermissions {
GroupCreatorIsAdmin,
}

impl From<PreconfiguredPolicies> for GroupPermissions {
fn from(policy: PreconfiguredPolicies) -> Self {
match policy {
PreconfiguredPolicies::EveryoneIsAdmin => GroupPermissions::EveryoneIsAdmin,
PreconfiguredPolicies::GroupCreatorIsAdmin => GroupPermissions::GroupCreatorIsAdmin,
}
}
}

#[uniffi::export(async_runtime = "tokio")]
impl FfiConversations {
pub async fn create_group(
Expand Down Expand Up @@ -443,6 +455,19 @@ impl FfiGroup {

Ok(group.is_active()?)
}

pub fn group_metadata(&self) -> Result<Arc<FfiGroupMetadata>, GenericError> {
let group = MlsGroup::new(
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
);

let metadata = group.metadata()?;
Ok(Arc::new(FfiGroupMetadata {
inner: Arc::new(metadata),
}))
}
}

#[uniffi::export]
Expand Down Expand Up @@ -507,6 +532,29 @@ pub trait FfiConversationCallback: Send + Sync {
fn on_conversation(&self, conversation: Arc<FfiGroup>);
}

#[derive(uniffi::Object)]
pub struct FfiGroupMetadata {
inner: Arc<GroupMetadata>,
}

#[uniffi::export]
impl FfiGroupMetadata {
pub fn creator_account_address(&self) -> String {
self.inner.creator_account_address.clone()
}

pub fn conversation_type(&self) -> String {
match self.inner.conversation_type {
ConversationType::Group => "group".to_string(),
ConversationType::Dm => "dm".to_string(),
}
}

pub fn policy_type(&self) -> Result<GroupPermissions, GenericError> {
Ok(self.inner.preconfigured_policy()?.into())
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
42 changes: 41 additions & 1 deletion xmtp_mls/src/groups/group_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use xmtp_proto::xmtp::mls::message_contents::{
ConversationType as ConversationTypeProto, GroupMetadataV1 as GroupMetadataProto,
};

use super::group_permissions::{PolicyError, PolicySet};
use super::{
group_permissions::{PolicyError, PolicySet},
PreconfiguredPolicies,
};

#[derive(Debug, Error)]
pub enum GroupMetadataError {
Expand Down Expand Up @@ -44,6 +47,10 @@ impl GroupMetadata {
}
}

pub fn preconfigured_policy(&self) -> Result<PreconfiguredPolicies, GroupMetadataError> {
Ok(PreconfiguredPolicies::from_policy_set(&self.policies)?)
}

pub(crate) fn from_proto(proto: GroupMetadataProto) -> Result<Self, GroupMetadataError> {
if proto.policies.is_none() {
return Err(GroupMetadataError::MissingPolicies);
Expand Down Expand Up @@ -132,3 +139,36 @@ pub fn extract_group_metadata(group: &OpenMlsGroup) -> Result<GroupMetadata, Gro

extension.metadata().try_into()
}

#[cfg(test)]
mod tests {
use crate::groups::group_permissions::{
policy_everyone_is_admin, policy_group_creator_is_admin,
};

use super::*;
#[test]
fn test_preconfigured_policy() {
let account_address = "account_address";
let group_metadata = GroupMetadata::new(
ConversationType::Group,
account_address.to_string(),
policy_everyone_is_admin(),
);
assert_eq!(
group_metadata.preconfigured_policy().unwrap(),
PreconfiguredPolicies::EveryoneIsAdmin
);

let group_metadata_creator_admin = GroupMetadata::new(
ConversationType::Group,
account_address.to_string(),
policy_group_creator_is_admin(),
);

assert_eq!(
group_metadata_creator_admin.preconfigured_policy().unwrap(),
PreconfiguredPolicies::GroupCreatorIsAdmin
);
}
}
11 changes: 11 additions & 0 deletions xmtp_mls/src/groups/group_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ pub(crate) fn policy_group_creator_is_admin() -> PolicySet {
)
}

#[derive(Debug, Clone, PartialEq)]
pub enum PreconfiguredPolicies {
EveryoneIsAdmin,
GroupCreatorIsAdmin,
Expand All @@ -357,6 +358,16 @@ impl PreconfiguredPolicies {
PreconfiguredPolicies::GroupCreatorIsAdmin => policy_group_creator_is_admin(),
}
}

pub fn from_policy_set(policy_set: &PolicySet) -> Result<Self, PolicyError> {
if policy_set.eq(&policy_everyone_is_admin()) {
Ok(PreconfiguredPolicies::EveryoneIsAdmin)
} else if policy_set.eq(&policy_group_creator_is_admin()) {
Ok(PreconfiguredPolicies::GroupCreatorIsAdmin)
} else {
Err(PolicyError::InvalidPolicy)
}
}
}

impl Default for PreconfiguredPolicies {
Expand Down
11 changes: 10 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod group_metadata;
pub mod group_metadata;
mod group_permissions;
mod intents;
mod members;
mod subscriptions;
mod sync;
pub mod validated_commit;

use self::group_metadata::extract_group_metadata;
pub use self::group_permissions::PreconfiguredPolicies;
pub use self::intents::{AddressesOrInstallationIds, IntentError};
use self::{
Expand Down Expand Up @@ -338,6 +339,14 @@ where

Ok(mls_group.is_active())
}

pub fn metadata(&self) -> Result<GroupMetadata, GroupError> {
let conn = &self.client.store.conn()?;
let provider = XmtpOpenMlsProvider::new(conn);
let mls_group = self.load_mls_group(&provider)?;

Ok(extract_group_metadata(&mls_group)?)
}
}

fn extract_message_v1(message: GroupMessage) -> Result<GroupMessageV1, MessageProcessingError> {
Expand Down

0 comments on commit bfdda64

Please sign in to comment.