From c057d6c6ada03776f1d55bfd7c7e82e5c76a94c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 21 Apr 2020 09:45:20 +0200 Subject: [PATCH] crypto: Add a method to remove devices from our DeviceStore. --- src/crypto/memory_stores.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/crypto/memory_stores.rs b/src/crypto/memory_stores.rs index 2510edb9..ba1cd6f4 100644 --- a/src/crypto/memory_stores.rs +++ b/src/crypto/memory_stores.rs @@ -185,6 +185,16 @@ impl DeviceStore { .and_then(|m| m.get(device_id).map(|d| d.value().clone())) } + /// Remove the device with the given device_id and belonging to the given user. + /// + /// Returns the device if it was removed, None if it wasn't in the store. + pub fn remove(&self, user_id: &UserId, device_id: &str) -> Option { + self.entries + .get(user_id) + .and_then(|m| m.remove(device_id)) + .and_then(|(_, d)| Some(d)) + } + /// Get a read-only view over all devices of the given user. pub fn user_devices(&self, user_id: &UserId) -> UserDevices { if !self.entries.contains_key(user_id) { @@ -286,5 +296,10 @@ mod test { let loaded_device = user_devices.get(device.device_id()).unwrap(); assert_eq!(device, loaded_device); + + store.remove(device.user_id(), device.device_id()); + + let loaded_device = store.get(device.user_id(), device.device_id()); + assert!(loaded_device.is_none()); } }