diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 97c21629..7cf3e449 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -147,7 +147,7 @@ impl OlmMachine { pub async fn new_with_store( user_id: UserId, device_id: Box, - mut store: Box, + store: Box, ) -> StoreResult { let account = match store.load_account().await? { Some(a) => { diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index 92d81314..3653905e 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -49,7 +49,7 @@ impl MemoryStore { #[async_trait] impl CryptoStore for MemoryStore { - async fn load_account(&mut self) -> Result> { + async fn load_account(&self) -> Result> { Ok(None) } diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index f58ca246..f7784db7 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -95,7 +95,7 @@ pub type Result = std::result::Result; /// keys. pub trait CryptoStore: Debug { /// Load an account that was previously stored. - async fn load_account(&mut self) -> Result>; + async fn load_account(&self) -> Result>; /// Save the given account in the store. /// diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index 9c781e3d..6beefa1a 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -17,7 +17,7 @@ use std::{ convert::TryFrom, path::{Path, PathBuf}, result::Result as StdResult, - sync::Arc, + sync::{Arc, Mutex as SyncMutex}, }; use async_trait::async_trait; @@ -44,7 +44,7 @@ use crate::{ pub struct SqliteStore { user_id: Arc, device_id: Arc>, - account_info: Option, + account_info: Arc>>, path: PathBuf, sessions: SessionStore, @@ -57,6 +57,7 @@ pub struct SqliteStore { pickle_passphrase: Option>, } +#[derive(Clone)] struct AccountInfo { account_id: i64, identity_keys: Arc, @@ -131,7 +132,7 @@ impl SqliteStore { let store = SqliteStore { user_id: Arc::new(user_id.to_owned()), device_id: Arc::new(device_id.into()), - account_info: None, + account_info: Arc::new(SyncMutex::new(None)), sessions: SessionStore::new(), inbound_group_sessions: GroupSessionStore::new(), devices: DeviceStore::new(), @@ -146,7 +147,11 @@ impl SqliteStore { } fn account_id(&self) -> Option { - self.account_info.as_ref().map(|i| i.account_id) + self.account_info + .lock() + .unwrap() + .as_ref() + .map(|i| i.account_id) } async fn create_tables(&self) -> Result<()> { @@ -322,7 +327,9 @@ impl SqliteStore { async fn load_sessions_for(&self, sender_key: &str) -> Result> { let account_info = self .account_info - .as_ref() + .lock() + .unwrap() + .clone() .ok_or(CryptoStoreError::AccountUnset)?; let mut connection = self.connection.lock().await; @@ -656,7 +663,7 @@ impl SqliteStore { #[async_trait] impl CryptoStore for SqliteStore { - async fn load_account(&mut self) -> Result> { + async fn load_account(&self) -> Result> { let mut connection = self.connection.lock().await; let row: Option<(i64, String, bool, i64)> = query_as( @@ -678,7 +685,7 @@ impl CryptoStore for SqliteStore { &self.device_id, )?; - self.account_info = Some(AccountInfo { + *self.account_info.lock().unwrap() = Some(AccountInfo { account_id: id, identity_keys: account.identity_keys.clone(), }); @@ -725,7 +732,7 @@ impl CryptoStore for SqliteStore { .fetch_one(&mut *connection) .await?; - self.account_info = Some(AccountInfo { + *self.account_info.lock().unwrap() = Some(AccountInfo { account_id: account_id.0, identity_keys: account.identity_keys.clone(), }); @@ -1113,7 +1120,7 @@ mod test { drop(store); - let mut store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) + let store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) .await .expect("Can't create store"); @@ -1199,7 +1206,7 @@ mod test { assert!(store.users_for_key_query().contains(device.user_id())); drop(store); - let mut store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) + let store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) .await .expect("Can't create store"); @@ -1214,7 +1221,7 @@ mod test { .unwrap(); assert!(!store.users_for_key_query().contains(device.user_id())); - let mut store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) + let store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) .await .expect("Can't create store"); @@ -1232,7 +1239,7 @@ mod test { drop(store); - let mut store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) + let store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) .await .expect("Can't create store"); @@ -1265,7 +1272,7 @@ mod test { store.save_devices(&[device.clone()]).await.unwrap(); store.delete_device(device.clone()).await.unwrap(); - let mut store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) + let store = SqliteStore::open(&example_user_id(), example_device_id(), dir.path()) .await .expect("Can't create store");