This commit is contained in:
Benjamin Kampmann
2022-02-25 15:19:57 +01:00
parent e1f898e6a5
commit c97f7e9ed0
4 changed files with 41 additions and 21 deletions

View File

@@ -43,14 +43,23 @@ use crate::{
/// `Account`s
#[derive(Clone)]
pub struct Session {
/// The `UserId` associated with this session
pub user_id: Arc<UserId>,
/// The specific `DeviceId` associated with this session
pub device_id: Arc<DeviceId>,
/// The `IdentityKeys` associated with this session
pub our_identity_keys: Arc<IdentityKeys>,
/// The OlmSession
pub inner: Arc<Mutex<OlmSession>>,
/// Our sessionId
pub session_id: Arc<str>,
/// The Key of the sender
pub sender_key: Arc<str>,
/// Has this been creaed using the fallback key
pub created_using_fallback_key: bool,
/// When the session was created
pub creation_time: Arc<Instant>,
/// When the session was last used
pub last_use_time: Arc<Instant>,
}

View File

@@ -544,6 +544,8 @@ impl PrivateCrossSigningIdentity {
#[cfg(any(test, feature = "testing"))]
#[allow(dead_code)]
/// Testing helper to reset this CrossSigning with a fresh one using the
/// local ideniy
pub async fn reset(&mut self) {
let new = Self::new(self.user_id().to_owned()).await;
*self = new

View File

@@ -233,6 +233,7 @@ pub enum SecretImportError {
}
impl Store {
/// Create a new Store
pub fn new(
user_id: Arc<UserId>,
identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
@@ -242,27 +243,33 @@ impl Store {
Self { user_id, identity, inner: store, verification_machine }
}
/// UserId associated with this store
pub fn user_id(&self) -> &UserId {
&self.user_id
}
/// DeviceId associated with this store
pub fn device_id(&self) -> &DeviceId {
self.verification_machine.own_device_id()
}
/// The Account associated with this store
pub fn account(&self) -> &ReadOnlyAccount {
&self.verification_machine.store.account
}
#[cfg(test)]
/// test helper to reset the cross signing identity
pub async fn reset_cross_signing_identity(&self) {
self.identity.lock().await.reset().await;
}
/// PrivateCrossSigningIdentity associated with this store
pub fn private_identity(&self) -> Arc<Mutex<PrivateCrossSigningIdentity>> {
self.identity.clone()
}
/// Save the given Sessions to the store
pub async fn save_sessions(&self, sessions: &[Session]) -> Result<()> {
let changes = Changes { sessions: sessions.to_vec(), ..Default::default() };
@@ -270,6 +277,7 @@ impl Store {
}
#[cfg(test)]
/// Testing helper to allo to save only a set of devices
pub async fn save_devices(&self, devices: &[ReadOnlyDevice]) -> Result<()> {
let changes = Changes {
devices: DeviceChanges { changed: devices.to_vec(), ..Default::default() },
@@ -280,6 +288,7 @@ impl Store {
}
#[cfg(test)]
/// Testing helper to allo to save only a set of InboundGroupSession
pub async fn save_inbound_group_sessions(
&self,
sessions: &[InboundGroupSession],
@@ -298,6 +307,7 @@ impl Store {
.and_then(|d| d.display_name().map(|d| d.to_string())))
}
/// Get the read-only device associated with `device_id` for `user_id`
pub async fn get_readonly_device(
&self,
user_id: &UserId,
@@ -346,6 +356,7 @@ impl Store {
})
}
/// Get all devices associated with the given `user_id`
pub async fn get_user_devices(&self, user_id: &UserId) -> Result<UserDevices> {
let devices = self.inner.get_user_devices(user_id).await?;
@@ -361,6 +372,7 @@ impl Store {
})
}
/// Get a Device copy associated with `device_id` for `user_id`
pub async fn get_device(
&self,
user_id: &UserId,
@@ -378,6 +390,7 @@ impl Store {
}))
}
/// Get the Identity of `user_id`
pub async fn get_identity(&self, user_id: &UserId) -> Result<Option<UserIdentities>> {
// let own_identity =
// self.inner.get_user_identity(self.user_id()).await?.and_then(|i| i.own());
@@ -444,6 +457,7 @@ impl Store {
}
}
/// Import the Cross Signing Keys
pub async fn import_cross_signing_keys(
&self,
export: CrossSigningKeyExport,
@@ -473,6 +487,7 @@ impl Store {
Ok(self.identity.lock().await.status().await)
}
/// Import the given `secret` named `secret_name` into the keystore.
pub async fn import_secret(
&self,
secret_name: &SecretName,

View File

@@ -87,6 +87,7 @@ impl From<TransactionError<SledStoreError>> for SledStoreError {
}
}
#[allow(clippy::from_over_into)]
impl Into<StoreError> for SledStoreError {
fn into(self) -> StoreError {
match self {
@@ -639,10 +640,8 @@ impl SledStore {
pub async fn get_presence_event(&self, user_id: &UserId) -> Result<Option<Raw<PresenceEvent>>> {
let db = self.clone();
let key = user_id.encode();
spawn_blocking(move || {
Ok(db.presence.get(key)?.map(|e| db.deserialize_event(&e)).transpose()?)
})
.await?
spawn_blocking(move || db.presence.get(key)?.map(|e| db.deserialize_event(&e)).transpose())
.await?
}
pub async fn get_state_event(
@@ -654,7 +653,7 @@ impl SledStore {
let db = self.clone();
let key = (room_id.as_str(), event_type.as_str(), state_key).encode();
spawn_blocking(move || {
Ok(db.room_state.get(key)?.map(|e| db.deserialize_event(&e)).transpose()?)
db.room_state.get(key)?.map(|e| db.deserialize_event(&e)).transpose()
})
.await?
}
@@ -667,11 +666,10 @@ impl SledStore {
let db = self.clone();
let key = (room_id.as_str(), event_type.as_str()).encode();
spawn_blocking(move || {
Ok(db
.room_state
db.room_state
.scan_prefix(key)
.flat_map(|e| e.map(|(_, e)| db.deserialize_event(&e)))
.collect::<Result<_, _>>()?)
.collect::<Result<_, _>>()
})
.await?
}
@@ -683,10 +681,8 @@ impl SledStore {
) -> Result<Option<RoomMemberEventContent>> {
let db = self.clone();
let key = (room_id.as_str(), user_id.as_str()).encode();
spawn_blocking(move || {
Ok(db.profiles.get(key)?.map(|p| db.deserialize_event(&p)).transpose()?)
})
.await?
spawn_blocking(move || db.profiles.get(key)?.map(|p| db.deserialize_event(&p)).transpose())
.await?
}
pub async fn get_member_event(
@@ -696,10 +692,8 @@ impl SledStore {
) -> Result<Option<MemberEvent>> {
let db = self.clone();
let key = (room_id.as_str(), state_key.as_str()).encode();
spawn_blocking(move || {
Ok(db.members.get(key)?.map(|v| db.deserialize_event(&v)).transpose()?)
})
.await?
spawn_blocking(move || db.members.get(key)?.map(|v| db.deserialize_event(&v)).transpose())
.await?
}
pub async fn get_user_ids_stream(
@@ -773,7 +767,7 @@ impl SledStore {
let db = self.clone();
spawn_blocking(move || {
stream::iter(
db.room_info.iter().map(move |r| db.deserialize_event(&r?.1).map_err(|e| e.into())),
db.room_info.iter().map(move |r| db.deserialize_event(&r?.1).map_err(|e| e)),
)
})
.await
@@ -786,7 +780,7 @@ impl SledStore {
stream::iter(
db.stripped_room_infos
.iter()
.map(move |r| db.deserialize_event(&r?.1).map_err(|e| e.into())),
.map(move |r| db.deserialize_event(&r?.1).map_err(|e| e)),
)
})
.await
@@ -818,7 +812,7 @@ impl SledStore {
let db = self.clone();
let key = event_type.encode();
spawn_blocking(move || {
Ok(db.account_data.get(key)?.map(|m| db.deserialize_event(&m)).transpose()?)
db.account_data.get(key)?.map(|m| db.deserialize_event(&m)).transpose()
})
.await?
}
@@ -831,7 +825,7 @@ impl SledStore {
let db = self.clone();
let key = (room_id.as_str(), event_type.as_str()).encode();
spawn_blocking(move || {
Ok(db.room_account_data.get(key)?.map(|m| db.deserialize_event(&m)).transpose()?)
db.room_account_data.get(key)?.map(|m| db.deserialize_event(&m)).transpose()
})
.await?
}
@@ -845,7 +839,7 @@ impl SledStore {
let db = self.clone();
let key = (room_id.as_str(), receipt_type.as_ref(), user_id.as_str()).encode();
spawn_blocking(move || {
Ok(db.room_user_receipts.get(key)?.map(|m| db.deserialize_event(&m)).transpose()?)
db.room_user_receipts.get(key)?.map(|m| db.deserialize_event(&m)).transpose()
})
.await?
}