From ce93869915800ad401336093c4c525c340009d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 21 Aug 2020 09:50:01 +0200 Subject: [PATCH] crypto: Return an Option instead of an empty result for the key uploads. --- matrix_sdk_base/src/client.rs | 4 ++-- matrix_sdk_crypto/src/machine.rs | 24 +++++++++++------------- matrix_sdk_crypto/src/olm/account.rs | 17 +++++++---------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index ebaf9af3..f6d15d22 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -1337,12 +1337,12 @@ impl BaseClient { /// Returns an empty error if no keys need to be uploaded. #[cfg(feature = "encryption")] #[cfg_attr(feature = "docs", doc(cfg(encryption)))] - pub async fn keys_for_upload(&self) -> StdResult { + pub async fn keys_for_upload(&self) -> Option { let olm = self.olm.lock().await; match &*olm { Some(o) => o.keys_for_upload().await, - None => Err(()), + None => None, } } diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 12c5c431..6c71ba28 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -18,7 +18,6 @@ use std::{ collections::{BTreeMap, HashSet}, convert::{TryFrom, TryInto}, mem, - result::Result as StdResult, sync::Arc, }; @@ -26,15 +25,14 @@ use dashmap::DashMap; use serde_json::Value; use tracing::{debug, error, info, instrument, trace, warn}; -use api::r0::{ - keys::{claim_keys, get_keys, upload_keys, OneTimeKey}, - sync::sync_events::Response as SyncResponse, - to_device::{ - send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices, - }, -}; use matrix_sdk_common::{ - api, + api::r0::{ + keys::{claim_keys, get_keys, upload_keys, OneTimeKey}, + sync::sync_events::Response as SyncResponse, + to_device::{ + send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices, + }, + }, encryption::DeviceKeys, events::{ forwarded_room_key::ForwardedRoomKeyEventContent, @@ -631,17 +629,17 @@ impl OlmMachine { /// Get a request to upload E2EE keys to the server. /// - /// Returns an empty error if no keys need to be uploaded. + /// Returns None if no keys need to be uploaded. /// /// The response of a successful key upload requests needs to be passed to /// the [`OlmMachine`] with the [`receive_keys_upload_response`]. /// /// [`receive_keys_upload_response`]: #method.receive_keys_upload_response /// [`OlmMachine`]: struct.OlmMachine.html - pub async fn keys_for_upload(&self) -> StdResult { + pub async fn keys_for_upload(&self) -> Option { let (device_keys, one_time_keys) = self.account.keys_for_upload().await?; - Ok(upload_keys::Request { + Some(upload_keys::Request { device_keys, one_time_keys, }) @@ -1813,7 +1811,7 @@ pub(crate) mod test { .unwrap(); let ret = machine.keys_for_upload().await; - assert!(ret.is_err()); + assert!(ret.is_none()); } #[tokio::test] diff --git a/matrix_sdk_crypto/src/olm/account.rs b/matrix_sdk_crypto/src/olm/account.rs index 2e60b493..86cd7d54 100644 --- a/matrix_sdk_crypto/src/olm/account.rs +++ b/matrix_sdk_crypto/src/olm/account.rs @@ -200,18 +200,15 @@ impl Account { /// Get a tuple of device and one-time keys that need to be uploaded. /// - /// Returns an empty error if no keys need to be uploaded. + /// Returns None if no keys need to be uploaded. pub(crate) async fn keys_for_upload( &self, - ) -> Result< - ( - Option, - Option>, - ), - (), - > { + ) -> Option<( + Option, + Option>, + )> { if !self.should_upload_keys().await { - return Err(()); + return None; } let device_keys = if !self.shared() { @@ -222,7 +219,7 @@ impl Account { let one_time_keys = self.signed_one_time_keys().await.ok(); - Ok((device_keys, one_time_keys)) + Some((device_keys, one_time_keys)) } /// Mark the current set of one-time keys as being published.