From eee0dc4e87a819b3eacab459c330e16969454889 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 22 May 2024 13:28:12 +0100 Subject: [PATCH] crypto: indicate when a device was updated update `ReadOnlyDevice::update_device` to return a bool indicating whether anything is changing. --- .../src/identities/device.rs | 20 ++++++++++++++----- .../src/types/device_keys.rs | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/identities/device.rs b/crates/matrix-sdk-crypto/src/identities/device.rs index 90f88735d..97097f89d 100644 --- a/crates/matrix-sdk-crypto/src/identities/device.rs +++ b/crates/matrix-sdk-crypto/src/identities/device.rs @@ -848,7 +848,12 @@ impl ReadOnlyDevice { } /// Update a device with a new device keys struct. - pub(crate) fn update_device(&mut self, device_keys: &DeviceKeys) -> Result<(), SignatureError> { + /// + /// Returns `true` if any changes were made to the data. + pub(crate) fn update_device( + &mut self, + device_keys: &DeviceKeys, + ) -> Result { self.verify_device_keys(device_keys)?; if self.user_id() != device_keys.user_id || self.device_id() != device_keys.device_id { @@ -858,10 +863,13 @@ impl ReadOnlyDevice { self.ed25519_key().map(Box::new), device_keys.ed25519_key().map(Box::new), )) - } else { + } else if self.inner.as_ref() != device_keys { self.inner = device_keys.clone().into(); - Ok(()) + Ok(true) + } else { + // no changes needed + Ok(false) } } @@ -1066,9 +1074,11 @@ pub(crate) mod tests { let mut device_keys = device_keys(); device_keys.unsigned.device_display_name = Some(display_name.clone()); - device.update_device(&device_keys).unwrap(); - + assert!(device.update_device(&device_keys).unwrap()); assert_eq!(&display_name, device.display_name().as_ref().unwrap()); + + // A second call to `update_device` with the same data should return `false`. + assert!(!device.update_device(&device_keys).unwrap()); } #[test] diff --git a/crates/matrix-sdk-crypto/src/types/device_keys.rs b/crates/matrix-sdk-crypto/src/types/device_keys.rs index 212f86407..6dc5cdfcf 100644 --- a/crates/matrix-sdk-crypto/src/types/device_keys.rs +++ b/crates/matrix-sdk-crypto/src/types/device_keys.rs @@ -39,7 +39,7 @@ use super::{EventEncryptionAlgorithm, Signatures}; /// identity keys. /// /// [device_keys_spec]: https://spec.matrix.org/v1.10/client-server-api/#_matrixclientv3keysupload_devicekeys -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(try_from = "DeviceKeyHelper", into = "DeviceKeyHelper")] pub struct DeviceKeys { /// The ID of the user the device belongs to. @@ -130,7 +130,7 @@ impl DeviceKeys { } /// Additional data added to device key information by intermediate servers. -#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)] pub struct UnsignedDeviceInfo { /// The display name which the user set on the device. #[serde(skip_serializing_if = "Option::is_none")]