diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index 4cbe31db..2bccfb38 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -15,7 +15,7 @@ use std::{collections::HashSet, sync::Arc}; use async_trait::async_trait; -use dashmap::DashSet; +use dashmap::{DashMap, DashSet}; use matrix_sdk_common::{ identifiers::{DeviceId, RoomId, UserId}, locks::Mutex, @@ -34,6 +34,7 @@ pub struct MemoryStore { tracked_users: Arc>, users_for_key_query: Arc>, devices: DeviceStore, + identities: Arc>, } impl MemoryStore { @@ -44,6 +45,7 @@ impl MemoryStore { tracked_users: Arc::new(DashSet::new()), users_for_key_query: Arc::new(DashSet::new()), devices: DeviceStore::new(), + identities: Arc::new(DashMap::new()), } } } @@ -133,8 +135,18 @@ impl CryptoStore for MemoryStore { Ok(()) } - async fn get_user_identity(&self, _user_id: &UserId) -> Result> { - Ok(None) + async fn get_user_identity(&self, user_id: &UserId) -> Result> { + #[allow(clippy::map_clone)] + Ok(self.identities.get(user_id).map(|i| i.clone())) + } + + async fn save_user_identities(&self, identities: &[UserIdentities]) -> Result<()> { + for identity in identities { + let _ = self + .identities + .insert(identity.user_id().to_owned(), identity.clone()); + } + Ok(()) } } diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 6273a9ba..0bea21fc 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -199,6 +199,13 @@ pub trait CryptoStore: Debug { /// * `user_id` - The user for which we should get all the devices. async fn get_user_devices(&self, user_id: &UserId) -> Result; + /// Save the given user identities in the store. + /// + /// # Arguments + /// + /// * `identities` - The identities that should be saved in the store. + async fn save_user_identities(&self, identities: &[UserIdentities]) -> Result<()>; + /// Get the user identity that is attached to the given user id. /// /// # Arguments diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index a44417aa..218c2ea2 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -888,6 +888,10 @@ impl CryptoStore for SqliteStore { async fn get_user_identity(&self, _user_id: &UserId) -> Result> { Ok(None) } + + async fn save_user_identities(&self, _users: &[UserIdentities]) -> Result<()> { + Ok(()) + } } #[cfg(not(tarpaulin_include))] diff --git a/matrix_sdk_crypto/src/user_identity.rs b/matrix_sdk_crypto/src/user_identity.rs index 6141ea67..4c0f8197 100644 --- a/matrix_sdk_crypto/src/user_identity.rs +++ b/matrix_sdk_crypto/src/user_identity.rs @@ -153,6 +153,13 @@ pub enum UserIdentities { } impl UserIdentities { + pub fn user_id(&self) -> &UserId { + match self { + UserIdentities::Own(i) => i.user_id(), + UserIdentities::Other(i) => i.user_id(), + } + } + pub fn master_key(&self) -> &BTreeMap { match self { UserIdentities::Own(i) => i.master_key(), @@ -182,6 +189,10 @@ impl UserIdentity { }) } + pub fn user_id(&self) -> &UserId { + &self.user_id + } + pub fn master_key(&self) -> &BTreeMap { &self.master_key.0.keys } @@ -231,6 +242,10 @@ impl OwnUserIdentity { }) } + pub fn user_id(&self) -> &UserId { + &self.user_id + } + pub fn update( &mut self, master_key: MasterPubkey,