Skip to content

Commit

Permalink
more store refs
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Oct 30, 2024
1 parent bb232f7 commit 76b11bb
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 54 deletions.
11 changes: 8 additions & 3 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ impl FfiConversations {

pub async fn sync(&self) -> Result<(), GenericError> {
let inner = self.inner_client.as_ref();
inner.sync_welcomes().await?;
let conn = inner.store().conn()?;
inner.sync_welcomes(&conn).await?;
Ok(())
}

Expand Down Expand Up @@ -3019,6 +3020,8 @@ mod tests {
let bo = new_test_client().await;
let caro = new_test_client().await;

let caro_conn = caro.inner_client.store().conn().unwrap();

let alix_group = alix
.conversations()
.create_group(
Expand Down Expand Up @@ -3047,7 +3050,7 @@ mod tests {
)
.await
.unwrap();
let _ = caro.inner_client.sync_welcomes().await.unwrap();
let _ = caro.inner_client.sync_welcomes(&caro_conn).await.unwrap();

bo_group.send("second".as_bytes().to_vec()).await.unwrap();
stream_callback.wait_for_delivery(None).await.unwrap();
Expand All @@ -3066,6 +3069,8 @@ mod tests {
let amal = new_test_client().await;
let bola = new_test_client().await;

let bola_conn = bola.inner_client.store().conn().unwrap();

let amal_group: Arc<FfiConversation> = amal
.conversations()
.create_group(
Expand All @@ -3075,7 +3080,7 @@ mod tests {
.await
.unwrap();

bola.inner_client.sync_welcomes().await.unwrap();
bola.inner_client.sync_welcomes(&bola_conn).await.unwrap();
let bola_group = bola.conversation(amal_group.id()).unwrap();

let stream_callback = RustStreamCallback::default();
Expand Down
7 changes: 6 additions & 1 deletion bindings_node/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,14 @@ impl NapiConversations {

#[napi]
pub async fn sync(&self) -> Result<()> {
let conn = self
.inner_client
.store()
.conn()
.map_err(ErrorWrapper::from)?;
self
.inner_client
.sync_welcomes()
.sync_welcomes(&conn)
.await
.map_err(ErrorWrapper::from)?;
Ok(())
Expand Down
7 changes: 6 additions & 1 deletion bindings_wasm/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ impl WasmConversations {

#[wasm_bindgen]
pub async fn sync(&self) -> Result<(), JsError> {
let conn = self
.inner_client
.store()
.conn()
.map_err(|e| JsError::new(format!("{}", e).as_str()))?;
self
.inner_client
.sync_welcomes()
.sync_welcomes(&conn)
.await
.map_err(|e| JsError::new(format!("{}", e).as_str()))?;
Ok(())
Expand Down
18 changes: 12 additions & 6 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ async fn main() {
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let conn = client.store().conn().unwrap();

client
.sync_welcomes()
.sync_welcomes(&conn)
.await
.expect("failed to sync welcomes");

Expand Down Expand Up @@ -351,8 +352,9 @@ async fn main() {
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let conn = client.store().conn().unwrap();
let provider = client.mls_provider().unwrap();
client.sync_welcomes().await.unwrap();
client.sync_welcomes(&conn).await.unwrap();
client.enable_history_sync(&provider).await.unwrap();
let (group_id, _) = client.send_history_sync_request().await.unwrap();
let group_id_str = hex::encode(group_id);
Expand All @@ -377,8 +379,9 @@ async fn main() {
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let conn = client.store().conn().unwrap();
let provider = client.mls_provider().unwrap();
client.sync_welcomes().await.unwrap();
client.sync_welcomes(&conn).await.unwrap();
client.enable_history_sync(&provider).await.unwrap();
client.process_history_sync_reply(&provider).await.unwrap();

Expand All @@ -388,8 +391,9 @@ async fn main() {
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let conn = client.store().conn().unwrap();
let provider = client.mls_provider().unwrap();
client.sync_welcomes().await.unwrap();
client.sync_welcomes(&conn).await.unwrap();
client.enable_history_sync(&provider).await.unwrap();
client.process_consent_sync_reply(&provider).await.unwrap();

Expand All @@ -399,8 +403,9 @@ async fn main() {
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let conn = client.store().conn().unwrap();
let provider = client.mls_provider().unwrap();
client.sync_welcomes().await.unwrap();
client.sync_welcomes(&conn).await.unwrap();
client.enable_history_sync(&provider).await.unwrap();
let group = client.get_sync_group().unwrap();
let group_id_str = hex::encode(group.group_id.clone());
Expand Down Expand Up @@ -502,7 +507,8 @@ async fn register(cli: &Cli, maybe_seed_phrase: Option<String>) -> Result<(), Cl
}

async fn get_group(client: &Client, group_id: Vec<u8>) -> Result<MlsGroup, CliError> {
client.sync_welcomes().await?;
let conn = client.store().conn().unwrap();
client.sync_welcomes(&conn).await?;
let group = client.group(group_id)?;
group
.sync()
Expand Down
37 changes: 26 additions & 11 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,11 @@ where

/// Download all unread welcome messages and converts to a group struct, ignoring malformed messages.
/// Returns any new groups created in the operation
pub async fn sync_welcomes(&self) -> Result<Vec<MlsGroup<Self>>, ClientError> {
let envelopes = self.query_welcome_messages(&self.store().conn()?).await?;
pub async fn sync_welcomes(
&self,
conn: &DbConnection,
) -> Result<Vec<MlsGroup<Self>>, ClientError> {
let envelopes = self.query_welcome_messages(conn).await?;
let num_envelopes = envelopes.len();
let id = self.installation_public_key();

Expand Down Expand Up @@ -1120,14 +1123,20 @@ pub(crate) mod tests {
.await
.unwrap();

let bob_received_groups = bob.sync_welcomes().await.unwrap();
let bob_received_groups = bob
.sync_welcomes(&bob.store().conn().unwrap())
.await
.unwrap();
assert_eq!(bob_received_groups.len(), 1);
assert_eq!(
bob_received_groups.first().unwrap().group_id,
alice_bob_group.group_id
);

let duplicate_received_groups = bob.sync_welcomes().await.unwrap();
let duplicate_received_groups = bob
.sync_welcomes(&bob.store().conn().unwrap())
.await
.unwrap();
assert_eq!(duplicate_received_groups.len(), 0);
}

Expand Down Expand Up @@ -1155,7 +1164,7 @@ pub(crate) mod tests {
.await
.unwrap();

let bob_received_groups = bo.sync_welcomes().await.unwrap();
let bob_received_groups = bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();
assert_eq!(bob_received_groups.len(), 2);

let bo_groups = bo.find_groups(FindGroupParams::default()).unwrap();
Expand Down Expand Up @@ -1231,7 +1240,9 @@ pub(crate) mod tests {
assert_eq!(amal_group.members().await.unwrap().len(), 1);
tracing::info!("Syncing bolas welcomes");
// See if Bola can see that they were added to the group
bola.sync_welcomes().await.unwrap();
bola.sync_welcomes(&bola.store().conn().unwrap())
.await
.unwrap();
let bola_groups = bola.find_groups(FindGroupParams::default()).unwrap();
assert_eq!(bola_groups.len(), 1);
let bola_group = bola_groups.first().unwrap();
Expand All @@ -1250,7 +1261,9 @@ pub(crate) mod tests {
.add_members_by_inbox_id(&[bola.inbox_id()])
.await
.unwrap();
bola.sync_welcomes().await.unwrap();
bola.sync_welcomes(&bola.store().conn().unwrap())
.await
.unwrap();

// Send a message from Amal, now that Bola is back in the group
amal_group
Expand Down Expand Up @@ -1340,18 +1353,20 @@ pub(crate) mod tests {
.await
.unwrap();

bo.sync_welcomes().await.unwrap();
bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();

let bo_new_key = get_key_package_init_key(&bo, &bo.installation_public_key()).await;
// Bo's key should have changed
assert_ne!(bo_original_init_key, bo_new_key);

bo.sync_welcomes().await.unwrap();
bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();
let bo_new_key_2 = get_key_package_init_key(&bo, &bo.installation_public_key()).await;
// Bo's key should not have changed syncing the second time.
assert_eq!(bo_new_key, bo_new_key_2);

alix.sync_welcomes().await.unwrap();
alix.sync_welcomes(&alix.store().conn().unwrap())
.await
.unwrap();
let alix_key_2 = get_key_package_init_key(&alix, &alix.installation_public_key()).await;
// Alix's key should not have changed at all
assert_eq!(alix_original_init_key, alix_key_2);
Expand All @@ -1363,7 +1378,7 @@ pub(crate) mod tests {
)
.await
.unwrap();
bo.sync_welcomes().await.unwrap();
bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();

// Bo should have two groups now
let bo_groups = bo.find_groups(FindGroupParams::default()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/device_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ where
let enc_payload = download_history_payload(&reply.url).await?;
insert_encrypted_syncables(conn, enc_payload, &enc_key.try_into()?)?;

self.sync_welcomes().await?;
self.sync_welcomes(provider.conn_ref()).await?;

let groups = conn.find_groups(None, None, None, None, Some(ConversationType::Group))?;
for crate::storage::group::StoredGroup { id, .. } in groups.into_iter() {
Expand Down
8 changes: 6 additions & 2 deletions xmtp_mls/src/groups/device_sync/consent_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ pub(crate) mod tests {
// Turn on history sync for the second installation.
assert_ok!(amal_b.enable_history_sync(&amal_b_provider).await);
// Check for new welcomes to new groups in the first installation (should be welcomed to a new sync group from amal_b).
amal_a.sync_welcomes().await.expect("sync_welcomes");
amal_a
.sync_welcomes(amal_a_conn)
.await
.expect("sync_welcomes");

// Have the second installation request for a consent sync.
let (_group_id, _pin_code) = amal_b
.send_consent_sync_request()
.await
.expect("history request");

// The first installation should now be a part of the sync group created by the second installation.
let amal_a_sync_groups = amal_a.store().conn().unwrap().latest_sync_group().unwrap();
let amal_a_sync_groups = amal_a_conn.latest_sync_group().unwrap();
assert!(amal_a_sync_groups.is_some());

// Have first installation reply.
Expand Down
15 changes: 10 additions & 5 deletions xmtp_mls/src/groups/device_sync/message_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub(crate) mod tests {
let mut amal_a = ClientBuilder::new_test_client(&wallet).await;
amal_a.history_sync_url = Some(history_sync_url.clone());
let amal_a_provider = amal_a.mls_provider().unwrap();
let amal_a_conn = amal_a.store().conn().unwrap();
let amal_a_conn = amal_a_provider.conn_ref();

// Create an alix client.
let alix_wallet = generate_local_wallet();
Expand Down Expand Up @@ -149,11 +149,14 @@ pub(crate) mod tests {
// Create a second installation for amal.
let amal_b = ClientBuilder::new_test_client(&wallet).await;
let amal_b_provider = amal_b.mls_provider().unwrap();
let amal_b_conn = amal_b.store().conn().unwrap();
let amal_b_conn = amal_b_provider.conn_ref();
// Turn on history sync for the second installation.
assert_ok!(amal_b.enable_history_sync(&amal_b_provider).await);
// Check for new welcomes to new groups in the first installation (should be welcomed to a new sync group from amal_b).
amal_a.sync_welcomes().await.expect("sync_welcomes");
amal_a
.sync_welcomes(amal_a_conn)
.await
.expect("sync_welcomes");
// Have the second installation request for a consent sync.
let (_group_id, _pin_code) = amal_b
.send_history_sync_request()
Expand Down Expand Up @@ -246,7 +249,9 @@ pub(crate) mod tests {
amal.enable_history_sync(&amal.mls_provider().unwrap())
.await
);
amal.sync_welcomes().await.expect("sync welcomes");
amal.sync_welcomes(&amal.store().conn().unwrap())
.await
.expect("sync welcomes");

let external_wallet = generate_local_wallet();
let external_client = ClientBuilder::new_test_client(&external_wallet).await;
Expand All @@ -256,7 +261,7 @@ pub(crate) mod tests {
.await
);
external_client
.sync_welcomes()
.sync_welcomes(&external_client.store().conn().unwrap())
.await
.expect("sync welcomes");

Expand Down
5 changes: 4 additions & 1 deletion xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,10 @@ pub(crate) mod tests {
verify_num_payloads_in_group(&group_a, 2).await;

// Client B sends a message to Client A
let groups_b = client_b.sync_welcomes().await.unwrap();
let groups_b = client_b
.sync_welcomes(&client_b.store().conn().unwrap())
.await
.unwrap();
assert_eq!(groups_b.len(), 1);
let group_b = groups_b[0].clone();
group_b
Expand Down
Loading

0 comments on commit 76b11bb

Please sign in to comment.