From c6100404e55421460679bcc5a59a2e56a437b01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 9 Sep 2021 10:20:50 +0200 Subject: [PATCH] feat(sdk): Expose the list of tracked users publicly --- matrix_sdk/src/client.rs | 15 ++++++++++++++- matrix_sdk_crypto/src/machine.rs | 11 ++++++++++- matrix_sdk_crypto/src/store/memorystore.rs | 4 ++++ matrix_sdk_crypto/src/store/mod.rs | 3 +++ matrix_sdk_crypto/src/store/sled.rs | 4 ++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index d7cd11b2..390c458e 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -14,7 +14,10 @@ // limitations under the License. #[cfg(feature = "encryption")] -use std::io::{Cursor, Write}; +use std::{ + collections::HashSet, + io::{Cursor, Write}, +}; #[cfg(all(feature = "encryption", not(target_arch = "wasm32")))] use std::path::PathBuf; use std::{ @@ -716,6 +719,16 @@ impl Client { self.base_client.olm_machine().await.map(|o| o.identity_keys().ed25519().to_owned()) } + /// Get all the tracked users we know about + /// + /// Tracked users are users for which we keep the device list of E2EE + /// capable devices up to date. + #[cfg(feature = "encryption")] + #[cfg_attr(feature = "docs", doc(cfg(encryption)))] + pub async fn tracked_users(&self) -> HashSet { + self.base_client.olm_machine().await.map(|o| o.tracked_users()).unwrap_or_default() + } + /// Fetches the display name of the owner of the client. /// /// # Example diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 48f7b406..9b098e9e 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -14,7 +14,11 @@ #[cfg(feature = "sled_cryptostore")] use std::path::Path; -use std::{collections::BTreeMap, mem, sync::Arc}; +use std::{ + collections::{BTreeMap, HashSet}, + mem, + sync::Arc, +}; use dashmap::DashMap; use matrix_sdk_common::{ @@ -293,6 +297,11 @@ impl OlmMachine { self.store.device_display_name().await } + /// Get all the tracked users we know about + pub fn tracked_users(&self) -> HashSet { + self.store.tracked_users() + } + /// Get the outgoing requests that need to be sent out. /// /// This returns a list of `OutGoingRequest`, those requests need to be sent diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index e1dc0672..7771a965 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -183,6 +183,10 @@ impl CryptoStore for MemoryStore { self.users_for_key_query.iter().map(|u| u.clone()).collect() } + fn tracked_users(&self) -> HashSet { + self.tracked_users.iter().map(|u| u.to_owned()).collect() + } + async fn update_tracked_user(&self, user: &UserId, dirty: bool) -> Result { // TODO to prevent a race between the sync and a key query in flight we // need to have an additional state to mention that the user changed. diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 57d79c69..3984180f 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -584,6 +584,9 @@ pub trait CryptoStore: AsyncTraitDeps { /// the tracked users. fn users_for_key_query(&self) -> HashSet; + /// Get all tracked users we know about. + fn tracked_users(&self) -> HashSet; + /// Add an user for tracking. /// /// Returns true if the user wasn't already tracked, false otherwise. diff --git a/matrix_sdk_crypto/src/store/sled.rs b/matrix_sdk_crypto/src/store/sled.rs index d24bd4c8..e2838c62 100644 --- a/matrix_sdk_crypto/src/store/sled.rs +++ b/matrix_sdk_crypto/src/store/sled.rs @@ -673,6 +673,10 @@ impl CryptoStore for SledStore { !self.users_for_key_query_cache.is_empty() } + fn tracked_users(&self) -> HashSet { + self.tracked_users_cache.to_owned().iter().map(|u| u.clone()).collect() + } + fn users_for_key_query(&self) -> HashSet { #[allow(clippy::map_clone)] self.users_for_key_query_cache.iter().map(|u| u.clone()).collect()