From 55a9e6836df65061b36a5bc950e792a306e5489e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 19 Jul 2021 09:45:47 +0200 Subject: [PATCH] crypto: Introduce a CancelInfo struct This replaces the separate methods to fetch info about the cancellation. It was a bit annoying to gather all the different info where each method can return None. --- matrix_sdk_crypto/src/lib.rs | 4 ++- matrix_sdk_crypto/src/verification/mod.rs | 32 +++++++++++++++++++ matrix_sdk_crypto/src/verification/qrcode.rs | 18 +++-------- .../src/verification/requests.rs | 14 ++++---- .../src/verification/sas/inner_sas.rs | 16 ---------- matrix_sdk_crypto/src/verification/sas/mod.rs | 18 +++++------ 6 files changed, 57 insertions(+), 45 deletions(-) diff --git a/matrix_sdk_crypto/src/lib.rs b/matrix_sdk_crypto/src/lib.rs index a3d7a244..37251fda 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -57,4 +57,6 @@ pub use requests::{ OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest, }; pub use store::CryptoStoreError; -pub use verification::{AcceptSettings, QrVerification, Sas, Verification, VerificationRequest}; +pub use verification::{ + AcceptSettings, CancelInfo, QrVerification, Sas, Verification, VerificationRequest, +}; diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 49ae8e70..20ed6105 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -165,6 +165,38 @@ impl Done { } } +/// Information about the cancellation of a verification request or verification +/// flow. +#[derive(Clone, Debug)] +pub struct CancelInfo { + cancelled_by_us: bool, + cancel_code: CancelCode, + reason: &'static str, +} + +impl CancelInfo { + /// Get the human readable reason of the cancellation. + pub fn reason(&self) -> &'static str { + &self.reason + } + + /// Get the `CancelCode` that cancelled this verification. + pub fn cancel_code(&self) -> &CancelCode { + &self.cancel_code + } + + /// Was the verification cancelled by us? + pub fn cancelled_by_us(&self) -> bool { + self.cancelled_by_us + } +} + +impl From for CancelInfo { + fn from(c: Cancelled) -> Self { + Self { cancelled_by_us: c.cancelled_by_us, cancel_code: c.cancel_code, reason: c.reason } + } +} + #[derive(Clone, Debug)] pub struct Cancelled { cancelled_by_us: bool, diff --git a/matrix_sdk_crypto/src/verification/qrcode.rs b/matrix_sdk_crypto/src/verification/qrcode.rs index baff6fab..a645a8e8 100644 --- a/matrix_sdk_crypto/src/verification/qrcode.rs +++ b/matrix_sdk_crypto/src/verification/qrcode.rs @@ -41,7 +41,7 @@ use tracing::trace; use super::{ event_enums::{CancelContent, DoneContent, OutgoingContent, OwnedStartContent, StartContent}, requests::RequestHandle, - Cancelled, Done, FlowId, IdentitiesBeingVerified, VerificationResult, + CancelInfo, Cancelled, Done, FlowId, IdentitiesBeingVerified, VerificationResult, }; use crate::{ olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, @@ -134,19 +134,11 @@ impl QrVerification { self.we_started } - /// Get the `CancelCode` that cancelled this verification request. - pub fn cancel_code(&self) -> Option { + /// Get info about the cancellation if the verification flow has been + /// cancelled. + pub fn cancel_info(&self) -> Option { if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() { - Some(c.state.cancel_code.to_owned()) - } else { - None - } - } - - /// Has the verification flow been cancelled by us. - pub fn cancelled_by_us(&self) -> Option { - if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() { - Some(c.state.cancelled_by_us) + Some(c.state.clone().into()) } else { None } diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index 087728b6..e12abe10 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -42,7 +42,7 @@ use super::{ CancelContent, DoneContent, OutgoingContent, ReadyContent, RequestContent, StartContent, }, qrcode::{QrVerification, ScanError}, - Cancelled, FlowId, IdentitiesBeingVerified, + CancelInfo, Cancelled, FlowId, IdentitiesBeingVerified, }; use crate::{ olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, @@ -219,11 +219,13 @@ impl VerificationRequest { } } - /// Get the `CancelCode` that cancelled this verification request. - pub fn cancel_code(&self) -> Option { - match &*self.inner.lock().unwrap() { - InnerRequest::Cancelled(c) => Some(c.state.cancel_code.to_owned()), - _ => None, + /// Get info about the cancellation if the verification request has been + /// cancelled. + pub fn cancel_info(&self) -> Option { + if let InnerRequest::Cancelled(c) = &*self.inner.lock().unwrap() { + Some(c.state.clone().into()) + } else { + None } } diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index 161181b6..53677e33 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -350,22 +350,6 @@ impl InnerSas { matches!(self, InnerSas::Confirmed(_) | InnerSas::WaitingForDone(_) | InnerSas::Done(_)) } - pub fn cancel_code(&self) -> Option { - if let InnerSas::Cancelled(c) = self { - Some(c.state.cancel_code.clone()) - } else { - None - } - } - - pub fn cancelled_by_us(&self) -> Option { - if let InnerSas::Cancelled(c) = self { - Some(c.state.cancelled_by_us) - } else { - None - } - } - pub fn timed_out(&self) -> bool { match self { InnerSas::Created(s) => s.timed_out(), diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index 56159d94..c382205f 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -35,7 +35,7 @@ use tracing::trace; use super::{ event_enums::{AnyVerificationContent, OutgoingContent, OwnedAcceptContent, StartContent}, requests::RequestHandle, - FlowId, IdentitiesBeingVerified, VerificationResult, + CancelInfo, FlowId, IdentitiesBeingVerified, VerificationResult, }; use crate::{ identities::{ReadOnlyDevice, ReadOnlyUserIdentities}, @@ -122,14 +122,14 @@ impl Sas { self.inner.lock().unwrap().has_been_accepted() } - /// Get the cancel code of this SAS verification if it has been cancelled - pub fn cancel_code(&self) -> Option { - self.inner.lock().unwrap().cancel_code() - } - - /// Has the verification flow been cancelled by us. - pub fn cancelled_by_us(&self) -> Option { - self.inner.lock().unwrap().cancelled_by_us() + /// Get info about the cancellation if the verification flow has been + /// cancelled. + pub fn cancel_info(&self) -> Option { + if let InnerSas::Cancelled(c) = &*self.inner.lock().unwrap() { + Some(c.state.as_ref().clone().into()) + } else { + None + } } /// Did we initiate the verification flow.