crypto: Implement storage for the user identities in the memory store.

master
Damir Jelić 2020-08-18 15:13:56 +02:00
parent 38cf771f1f
commit 37a7f69e03
4 changed files with 41 additions and 3 deletions

View File

@ -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<DashSet<UserId>>,
users_for_key_query: Arc<DashSet<UserId>>,
devices: DeviceStore,
identities: Arc<DashMap<UserId, UserIdentities>>,
}
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<Option<UserIdentities>> {
Ok(None)
async fn get_user_identity(&self, user_id: &UserId) -> Result<Option<UserIdentities>> {
#[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(())
}
}

View File

@ -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<ReadOnlyUserDevices>;
/// 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

View File

@ -888,6 +888,10 @@ impl CryptoStore for SqliteStore {
async fn get_user_identity(&self, _user_id: &UserId) -> Result<Option<UserIdentities>> {
Ok(None)
}
async fn save_user_identities(&self, _users: &[UserIdentities]) -> Result<()> {
Ok(())
}
}
#[cfg(not(tarpaulin_include))]

View File

@ -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<String, String> {
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<String, String> {
&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,