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

Who Added Me #614

Merged
merged 21 commits into from
Apr 15, 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
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 @@ -472,6 +481,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 @@ -482,6 +492,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 @@ -1108,4 +1119,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zombieobject I think @richardhuaaa was suggesting that this is NOT NULL do you mind doing that in a follow up PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nplasterer I missed that comment just as I was merging the PR. I am more than happy to create a follow up PR to address this request.

For context, there were 2 reasons for the optionality of that parameter. The largest one IMHO was due to MLSGroup lifecycle, being that the address of "who added me" was not always available at group creation. The latter was breaking changes, but as you both stated, better to break things now, to which I concur.

I'll open up a follow up PR with the request immediately and see what I can do to address the lifecycle questions. 🙂

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
Loading