Skip to content

Commit

Permalink
Merge branch 'main' into cv/mutable-metadata-group-name-part-2
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 15, 2024
2 parents 11d8841 + e1fad9a commit 28df410
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 19 deletions.
58 changes: 55 additions & 3 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ impl FfiConversations {
if !account_addresses.is_empty() {
convo.add_members(account_addresses).await?;
}

let out = Arc::new(FfiGroup {
inner_client: self.inner_client.clone(),
group_id: convo.group_id,
created_at_ns: convo.created_at_ns,
added_by_address: convo.added_by_address,
});

Ok(out)
Expand All @@ -232,13 +232,12 @@ impl FfiConversations {
) -> Result<Arc<FfiGroup>, GenericError> {
let inner = self.inner_client.as_ref();
let group = inner.process_streamed_welcome_message(envelope_bytes)?;

let out = Arc::new(FfiGroup {
inner_client: self.inner_client.clone(),
group_id: group.group_id,
created_at_ns: group.created_at_ns,
added_by_address: group.added_by_address,
});

Ok(out)
}

Expand Down Expand Up @@ -266,6 +265,7 @@ impl FfiConversations {
inner_client: self.inner_client.clone(),
group_id: group.group_id,
created_at_ns: group.created_at_ns,
added_by_address: group.added_by_address,
})
})
.collect();
Expand All @@ -285,6 +285,7 @@ impl FfiConversations {
inner_client: client.clone(),
group_id: convo.group_id,
created_at_ns: convo.created_at_ns,
added_by_address: convo.added_by_address,
}))
},
|| {}, // on_close_callback
Expand Down Expand Up @@ -318,6 +319,7 @@ pub struct FfiGroup {
inner_client: Arc<RustXmtpClient>,
group_id: Vec<u8>,
created_at_ns: i64,
added_by_address: Option<String>,
}

#[derive(uniffi::Record)]
Expand All @@ -340,6 +342,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

group.send_message(content_bytes.as_slice()).await?;
Expand All @@ -352,6 +355,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

group.sync().await?;
Expand All @@ -367,6 +371,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

let messages: Vec<FfiMessage> = group
Expand All @@ -392,6 +397,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);
let message = group.process_streamed_group_message(envelope_bytes).await?;
let ffi_message = message.into();
Expand All @@ -404,6 +410,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

let members: Vec<FfiGroupMember> = group
Expand All @@ -425,6 +432,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

group.add_members(account_addresses).await?;
Expand All @@ -437,6 +445,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

group.remove_members(account_addresses).await?;
Expand Down Expand Up @@ -496,6 +505,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

Ok(group.is_active()?)
Expand All @@ -506,6 +516,7 @@ impl FfiGroup {
self.inner_client.as_ref(),
self.group_id.clone(),
self.created_at_ns,
self.added_by_address.clone(),
);

let metadata = group.metadata()?;
Expand Down Expand Up @@ -1132,4 +1143,45 @@ mod tests {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
assert!(stream_closer.is_closed());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_group_who_added_me() {
// Create Clients
let amal = new_test_client().await;
let bola = new_test_client().await;

// Amal creates a group and adds Bola to the group
amal.conversations()
.create_group(vec![bola.account_address()], None)
.await
.unwrap();

// Bola syncs groups - this will decrypt the Welcome, identify who added Bola
// and then store that value on the group and insert into the database
let bola_conversations = bola.conversations();
let _ = bola_conversations.sync().await;

// Bola gets the group id. This will be needed to fetch the group from
// the database.
let bola_groups = bola_conversations
.list(crate::FfiListConversationsOptions {
created_after_ns: None,
created_before_ns: None,
limit: None,
})
.await
.unwrap();

let bola_group = bola_groups.first().unwrap();

// Check Bola's group for the added_by_address of the inviter
let added_by_address = bola_group.added_by_address.clone().unwrap();

// // Verify the welcome host_credential is equal to Amal's
assert_eq!(
amal.account_address(),
added_by_address,
"The Inviter and added_by_address do not match!"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- As SQLite does not support ALTER, we play this game of move, repopulate, drop. Here we recreate without the 'added_by_address' column.
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE backup_group(id BLOB PRIMARY KEY NOT NULL, created_at_ns BIGINT NOT NULL, membership_state INT NOT NULL, installations_last_checked BIGINT NOT NULL, purpose INT NOT NULL DEFAULT 1);
INSERT INTO backup_group SELECT id, created_at_ns, membership_state, installations_last_checked, pupose FROM groups;
DROP TABLE groups;
CREATE TABLE groups(id BLOB PRIMARY KEY NOT NULL, created_at_ns BIGINT NOT NULL, membership_state INT NOT NULL, installations_last_checked BIGINT NOT NULL, purpose INT NOT NULL DEFAULT 1);
INSERT INTO groups SELECT id, created_at_ns, membership_state, installations_last_checked, purpose FROM backup_group;
DROP TABLE backup_group;
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE groups
ADD COLUMN added_by_address TEXT
25 changes: 21 additions & 4 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ where
) -> Result<MlsGroup<ApiClient>, ClientError> {
log::info!("creating group");

let group = MlsGroup::create_and_insert(self, GroupMembershipState::Allowed, permissions)
.map_err(|e| ClientError::Generic(format!("group create error {}", e)))?;
let group = MlsGroup::create_and_insert(
self,
GroupMembershipState::Allowed,
permissions,
Some(self.account_address()),
)
.map_err(|e| ClientError::Generic(format!("group create error {}", e)))?;

Ok(group)
}
Expand All @@ -218,7 +223,12 @@ where
let conn = &mut self.store.conn()?;
let stored_group: Option<StoredGroup> = conn.fetch(&group_id)?;
match stored_group {
Some(group) => Ok(MlsGroup::new(self, group.id, group.created_at_ns)),
Some(group) => Ok(MlsGroup::new(
self,
group.id,
group.created_at_ns,
group.added_by_address,
)),
None => Err(ClientError::Generic("group not found".to_string())),
}
}
Expand All @@ -242,7 +252,14 @@ where
.conn()?
.find_groups(allowed_states, created_after_ns, created_before_ns, limit)?
.into_iter()
.map(|stored_group| MlsGroup::new(self, stored_group.id, stored_group.created_at_ns))
.map(|stored_group| {
MlsGroup::new(
self,
stored_group.id,
stored_group.created_at_ns,
stored_group.added_by_address,
)
})
.collect())
}

Expand Down
Loading

0 comments on commit 28df410

Please sign in to comment.