From 5dc0842f491ffca43d5ba0372322dba525232377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 30 Apr 2020 14:33:41 +0200 Subject: [PATCH] crypto: Implmement device deletion for the sqlite store. --- matrix_sdk_crypto/src/store/sqlite.rs | 39 ++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index c1ab6025..bf73c0d7 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -622,7 +622,21 @@ impl CryptoStore for SqliteStore { } async fn delete_device(&self, device: Device) -> Result<()> { - todo!() + let account_id = self.account_id.ok_or(CryptoStoreError::AccountUnset)?; + let mut connection = self.connection.lock().await; + + query( + "DELETE FROM devices + WHERE account_id = ?1 and user_id = ?2 and device_id = ?3 + ", + ) + .bind(account_id) + .bind(&device.user_id().to_string()) + .bind(&device.device_id()) + .execute(&mut *connection) + .await?; + + Ok(()) } #[allow(clippy::ptr_arg)] @@ -975,4 +989,27 @@ mod test { assert_eq!(user_devices.keys().nth(0).unwrap(), device.device_id()); assert_eq!(user_devices.devices().nth(0).unwrap(), &device); } + + #[tokio::test] + async fn device_deleting() { + let (_account, store, dir) = get_loaded_store().await; + let device = get_device(); + + store.save_devices(&[device.clone()]).await.unwrap(); + store.delete_device(device.clone()).await.unwrap(); + + let mut store = + SqliteStore::open(&UserId::try_from(USER_ID).unwrap(), DEVICE_ID, dir.path()) + .await + .expect("Can't create store"); + + store.load_account().await.unwrap(); + + let loaded_device = store + .get_device(device.user_id(), device.device_id()) + .await + .unwrap(); + + assert!(loaded_device.is_none()); + } }