From cb6e43b340e293f041edc5e37d81007219abe5ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 22 Apr 2020 12:12:47 +0200 Subject: [PATCH] crypto: Allow devices to be deleted from the crypto store. --- src/crypto/machine.rs | 5 +++-- src/crypto/store/memorystore.rs | 12 ++++++++++++ src/crypto/store/mod.rs | 7 +++++++ src/crypto/store/sqlite.rs | 4 ++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/crypto/machine.rs b/src/crypto/machine.rs index f42b6aa8..cb2d7657 100644 --- a/src/crypto/machine.rs +++ b/src/crypto/machine.rs @@ -427,12 +427,13 @@ impl OlmMachine { for device_id in deleted_devices { if let Some(device) = stored_devices.get(device_id) { device.mark_as_deleted(); - // TODO change this to a delete device. - self.store.save_device(device).await?; + // TODO change this to a bulk deletion. + self.store.delete_device(device).await?; } } } + // TODO change this to a bulk operation. for device in &changed_devices { self.store.save_device(device.clone()).await?; } diff --git a/src/crypto/store/memorystore.rs b/src/crypto/store/memorystore.rs index 0e889786..3100a2e3 100644 --- a/src/crypto/store/memorystore.rs +++ b/src/crypto/store/memorystore.rs @@ -88,6 +88,11 @@ impl CryptoStore for MemoryStore { Ok(self.devices.get(user_id, device_id)) } + async fn delete_device(&self, device: Device) -> Result<()> { + self.devices.remove(device.user_id(), device.device_id()); + Ok(()) + } + async fn get_user_devices(&self, user_id: &UserId) -> Result { Ok(self.devices.user_devices(user_id)) } @@ -181,6 +186,13 @@ mod test { let loaded_device = user_devices.get(device.device_id()).unwrap(); assert_eq!(device, loaded_device); + + store.delete_device(device.clone()).await.unwrap(); + assert!(store + .get_device(device.user_id(), device.device_id()) + .await + .unwrap() + .is_none()); } #[tokio::test] diff --git a/src/crypto/store/mod.rs b/src/crypto/store/mod.rs index 7181aff1..cf92a1f2 100644 --- a/src/crypto/store/mod.rs +++ b/src/crypto/store/mod.rs @@ -133,6 +133,13 @@ pub trait CryptoStore: Debug + Send + Sync { /// * `device` - The device that should be stored. async fn save_device(&self, device: Device) -> Result<()>; + /// Delete the given device from the store. + /// + /// # Arguments + /// + /// * `device` - The device that should be stored. + async fn delete_device(&self, device: Device) -> Result<()>; + /// Get the device for the given user with the given device id. /// /// # Arguments diff --git a/src/crypto/store/sqlite.rs b/src/crypto/store/sqlite.rs index df7f336d..5f768e18 100644 --- a/src/crypto/store/sqlite.rs +++ b/src/crypto/store/sqlite.rs @@ -613,6 +613,10 @@ impl CryptoStore for SqliteStore { self.save_device_helper(device).await } + async fn delete_device(&self, device: Device) -> Result<()> { + todo!() + } + async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result> { Ok(self.devices.get(user_id, device_id)) }