From 08450da5e1cec4cc8e91c6bc5838fd68386d7583 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 16 Oct 2023 18:50:38 +0200 Subject: [PATCH] crypto: move `Store::mark_tracked_users_as_up_to_date` to the `StoreCache` --- .../src/identities/manager.rs | 3 + crates/matrix-sdk-crypto/src/store/mod.rs | 61 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/identities/manager.rs b/crates/matrix-sdk-crypto/src/identities/manager.rs index 908582a25..7f3e5ab2a 100644 --- a/crates/matrix-sdk-crypto/src/identities/manager.rs +++ b/crates/matrix-sdk-crypto/src/identities/manager.rs @@ -167,7 +167,10 @@ impl IdentityManager { if let Some(sequence_number) = sequence_number { self.store + .cache() + .await? .mark_tracked_users_as_up_to_date( + &self.store, response.device_keys.keys().map(Deref::deref), sequence_number, ) diff --git a/crates/matrix-sdk-crypto/src/store/mod.rs b/crates/matrix-sdk-crypto/src/store/mod.rs index dbbb5f994..a6d64a710 100644 --- a/crates/matrix-sdk-crypto/src/store/mod.rs +++ b/crates/matrix-sdk-crypto/src/store/mod.rs @@ -246,6 +246,37 @@ impl StoreCache { pub(crate) fn tracked_users(&self) -> HashSet { self.tracked_users.read().unwrap().iter().cloned().collect() } + + /// Flag that the given users devices are now up-to-date. + /// + /// This is called after processing the response to a /keys/query request. + /// Any users whose device lists we are tracking are removed from the + /// list of those pending a /keys/query. + pub(crate) async fn mark_tracked_users_as_up_to_date( + &self, + store: &Store, + users: impl Iterator, + sequence_number: SequenceNumber, + ) -> Result<()> { + let mut store_updates: Vec<(&UserId, bool)> = Vec::new(); + let mut key_query_lock = store.inner.users_for_key_query.lock().await; + + { + let tracked_users = self.tracked_users.read().unwrap(); + for user_id in users { + if tracked_users.contains(user_id) { + let clean = key_query_lock.maybe_remove_user(user_id, sequence_number); + store_updates.push((user_id, !clean)); + } + } + } + + store.inner.store.save_tracked_users(&store_updates).await?; + // wake up any tasks that may have been waiting for updates + store.inner.users_for_key_query_condvar.notify_all(); + + Ok(()) + } } pub(crate) struct StoreCacheGuard { @@ -1126,36 +1157,6 @@ impl Store { Ok(()) } - /// Flag that the given users devices are now up-to-date. - /// - /// This is called after processing the response to a /keys/query request. - /// Any users whose device lists we are tracking are removed from the - /// list of those pending a /keys/query. - pub(crate) async fn mark_tracked_users_as_up_to_date( - &self, - users: impl Iterator, - sequence_number: SequenceNumber, - ) -> Result<()> { - let mut store_updates: Vec<(&UserId, bool)> = Vec::new(); - let mut key_query_lock = self.inner.users_for_key_query.lock().await; - - { - let cache = self.cache().await?; - let tracked_users = cache.tracked_users.read().unwrap(); - for user_id in users { - if tracked_users.contains(user_id) { - let clean = key_query_lock.maybe_remove_user(user_id, sequence_number); - store_updates.push((user_id, !clean)); - } - } - } - self.inner.store.save_tracked_users(&store_updates).await?; - // wake up any tasks that may have been waiting for updates - self.inner.users_for_key_query_condvar.notify_all(); - - Ok(()) - } - /// Get the set of users that has the outdate/dirty flag set for their list /// of devices. ///