Skip to content

Commit

Permalink
create GroupQueryArgs for ergonomic paramters to find_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Oct 29, 2024
1 parent 8a34799 commit e499fd3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 87 deletions.
37 changes: 10 additions & 27 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ use xmtp_proto::xmtp::mls::api::v1::{
use crate::{
api::ApiClientWrapper,
groups::{
group_metadata::ConversationType, group_permissions::PolicySet,
validated_commit::CommitValidationError, GroupError, GroupMetadataOptions, IntentError,
MlsGroup,
group_permissions::PolicySet, validated_commit::CommitValidationError, GroupError,
GroupMetadataOptions, IntentError, MlsGroup,
},
identity::{parse_credential, Identity, IdentityError},
identity_updates::{load_identity_updates, IdentityUpdateError},
intents::Intents,
mutex_registry::MutexRegistry,
retry::Retry,
retry_async, retryable,
storage::group::GroupQueryArgs,
storage::{
consent_record::{ConsentState, ConsentType, StoredConsentRecord},
db_connection::DbConnection,
Expand Down Expand Up @@ -217,16 +217,6 @@ impl From<&str> for ClientError {
}
}

#[derive(Debug, Default)]
pub struct FindGroupParams {
pub allowed_states: Option<Vec<GroupMembershipState>>,
pub created_after_ns: Option<i64>,
pub created_before_ns: Option<i64>,
pub limit: Option<i64>,
pub conversation_type: Option<ConversationType>,
pub consent_state: Option<ConsentState>,
}

/// Clients manage access to the network, identity, and data store
pub struct Client<ApiClient, V = RemoteSignatureVerifier<ApiClient>> {
pub(crate) api_client: ApiClientWrapper<ApiClient>,
Expand Down Expand Up @@ -682,18 +672,11 @@ where
/// - created_after_ns: only return groups created after the given timestamp (in nanoseconds)
/// - created_before_ns: only return groups created before the given timestamp (in nanoseconds)
/// - limit: only return the first `limit` groups
pub fn find_groups(&self, params: FindGroupParams) -> Result<Vec<MlsGroup<Self>>, ClientError> {
pub fn find_groups(&self, args: GroupQueryArgs) -> Result<Vec<MlsGroup<Self>>, ClientError> {
Ok(self
.store()
.conn()?
.find_groups(
params.allowed_states,
params.created_after_ns,
params.created_before_ns,
params.limit,
params.conversation_type,
params.consent_state,
)?
.find_groups(args)?
.into_iter()
.map(|stored_group| {
MlsGroup::new(self.clone(), stored_group.id, stored_group.created_at_ns)
Expand Down Expand Up @@ -981,12 +964,12 @@ pub(crate) mod tests {

use crate::{
builder::ClientBuilder,
client::FindGroupParams,
groups::GroupMetadataOptions,
hpke::{decrypt_welcome, encrypt_welcome},
identity::serialize_key_package_hash_ref,
storage::{
consent_record::{ConsentState, ConsentType, StoredConsentRecord},
group::GroupQueryArgs,
group_message::MsgQueryArgs,
schema::identity_updates,
},
Expand Down Expand Up @@ -1091,7 +1074,7 @@ pub(crate) mod tests {
.create_group(None, GroupMetadataOptions::default())
.unwrap();

let groups = client.find_groups(FindGroupParams::default()).unwrap();
let groups = client.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(groups.len(), 2);
assert_eq!(groups[0].group_id, group_1.group_id);
assert_eq!(groups[1].group_id, group_2.group_id);
Expand Down Expand Up @@ -1166,7 +1149,7 @@ pub(crate) mod tests {
let bob_received_groups = bo.sync_welcomes().await.unwrap();
assert_eq!(bob_received_groups.len(), 2);

let bo_groups = bo.find_groups(FindGroupParams::default()).unwrap();
let bo_groups = bo.find_groups(GroupQueryArgs::default()).unwrap();
let bo_group1 = bo.group(alix_bo_group1.clone().group_id).unwrap();
let bo_messages1 = bo_group1.find_messages(&MsgQueryArgs::default()).unwrap();
assert_eq!(bo_messages1.len(), 0);
Expand Down Expand Up @@ -1240,7 +1223,7 @@ pub(crate) mod tests {
tracing::info!("Syncing bolas welcomes");
// See if Bola can see that they were added to the group
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
tracing::info!("Syncing bolas messages");
Expand Down Expand Up @@ -1374,7 +1357,7 @@ pub(crate) mod tests {
bo.sync_welcomes().await.unwrap();

// Bo should have two groups now
let bo_groups = bo.find_groups(FindGroupParams::default()).unwrap();
let bo_groups = bo.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bo_groups.len(), 2);

// Bo's original key should be deleted
Expand Down
40 changes: 18 additions & 22 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ pub(crate) mod tests {
use crate::{
assert_err,
builder::ClientBuilder,
client::{FindGroupParams, MessageProcessingError},
client::MessageProcessingError,
codecs::{group_updated::GroupUpdatedCodec, ContentCodec},
groups::{
build_dm_protected_metadata_extension, build_mutable_metadata_extension_default,
Expand All @@ -1543,6 +1543,7 @@ pub(crate) mod tests {
},
storage::{
consent_record::ConsentState,
group::GroupQueryArgs,
group::Purpose,
group_intent::{IntentKind, IntentState},
group_message::{GroupMessageKind, MsgQueryArgs, StoredGroupMessage},
Expand All @@ -1556,7 +1557,7 @@ pub(crate) mod tests {

async fn receive_group_invite(client: &FullXmtpClient) -> MlsGroup<FullXmtpClient> {
client.sync_welcomes().await.unwrap();
let mut groups = client.find_groups(FindGroupParams::default()).unwrap();
let mut groups = client.find_groups(GroupQueryArgs::default()).unwrap();

groups.remove(0)
}
Expand Down Expand Up @@ -1865,7 +1866,7 @@ pub(crate) mod tests {

// Bo should not be able to actually read this group
bo.sync_welcomes().await.unwrap();
let groups = bo.find_groups(FindGroupParams::default()).unwrap();
let groups = bo.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(groups.len(), 0);
assert_logged!("failed to create group from welcome", 1);
});
Expand Down Expand Up @@ -1994,7 +1995,7 @@ pub(crate) mod tests {
group.send_message(b"hello").await.expect("send message");

bola_client.sync_welcomes().await.unwrap();
let bola_groups = bola_client.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola_client.find_groups(GroupQueryArgs::default()).unwrap();
let bola_group = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
let bola_messages = bola_group.find_messages(&MsgQueryArgs::default()).unwrap();
Expand Down Expand Up @@ -2355,7 +2356,7 @@ pub(crate) mod tests {
.await
.unwrap();
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand Down Expand Up @@ -2523,7 +2524,7 @@ pub(crate) mod tests {
.await
.unwrap();
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand Down Expand Up @@ -2603,7 +2604,7 @@ pub(crate) mod tests {
.await
.unwrap();
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand All @@ -2619,7 +2620,7 @@ pub(crate) mod tests {

// Verify that bola can not add caro because they are not an admin
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group: &MlsGroup<_> = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand Down Expand Up @@ -2683,7 +2684,7 @@ pub(crate) mod tests {

// Verify that bola can not add charlie because they are not an admin
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group: &MlsGroup<_> = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand Down Expand Up @@ -2712,7 +2713,7 @@ pub(crate) mod tests {
.await
.unwrap();
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand All @@ -2728,7 +2729,7 @@ pub(crate) mod tests {

// Verify that bola can not add caro as an admin because they are not a super admin
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group: &MlsGroup<_> = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
Expand Down Expand Up @@ -2979,7 +2980,7 @@ pub(crate) mod tests {

// Step 3: Verify that Bola can update the group name, and amal sees the update
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
let bola_group: &MlsGroup<_> = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
bola_group
Expand Down Expand Up @@ -3045,7 +3046,7 @@ pub(crate) mod tests {
// Step 3: Bola attemps to add Caro, but fails because group is admin only
let caro = ClientBuilder::new_test_client(&generate_local_wallet()).await;
bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
let bola_group: &MlsGroup<_> = bola_groups.first().unwrap();
bola_group.sync().await.unwrap();
let result = bola_group
Expand Down Expand Up @@ -3204,12 +3205,7 @@ pub(crate) mod tests {

// Bola can message amal
let _ = bola.sync_welcomes().await;
let bola_groups = bola
.find_groups(FindGroupParams {
conversation_type: None,
..FindGroupParams::default()
})
.unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
let bola_dm: &MlsGroup<_> = bola_groups.first().unwrap();
bola_dm.send_message(b"test one").await.unwrap();

Expand Down Expand Up @@ -3556,7 +3552,7 @@ pub(crate) mod tests {
.unwrap();

bola.sync_welcomes().await.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
let bola_groups = bola.find_groups(GroupQueryArgs::default()).unwrap();
let bola_group = bola_groups.first().unwrap();
// group consent state should default to unknown for users who did not create the group
assert_eq!(bola_group.consent_state().unwrap(), ConsentState::Unknown);
Expand All @@ -3575,7 +3571,7 @@ pub(crate) mod tests {
.unwrap();

caro.sync_welcomes().await.unwrap();
let caro_groups = caro.find_groups(FindGroupParams::default()).unwrap();
let caro_groups = caro.find_groups(GroupQueryArgs::default()).unwrap();
let caro_group = caro_groups.first().unwrap();

caro_group
Expand Down Expand Up @@ -3605,7 +3601,7 @@ pub(crate) mod tests {
.await
.unwrap();
bo.sync_welcomes().await.unwrap();
let bo_groups = bo.find_groups(FindGroupParams::default()).unwrap();
let bo_groups = bo.find_groups(GroupQueryArgs::default()).unwrap();
let bo_group = bo_groups.first().unwrap();

// Both members see the same amount of messages to start
Expand Down
Loading

0 comments on commit e499fd3

Please sign in to comment.