crypto: move Store::mark_tracked_users_as_up_to_date to the StoreCache

This commit is contained in:
Benjamin Bouvier
2023-10-16 18:50:38 +02:00
parent 9ecc6ddd1e
commit 08450da5e1
2 changed files with 34 additions and 30 deletions

View File

@@ -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,
)

View File

@@ -246,6 +246,37 @@ impl StoreCache {
pub(crate) fn tracked_users(&self) -> HashSet<OwnedUserId> {
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<Item = &UserId>,
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<Item = &UserId>,
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.
///