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.
master
Damir Jelić 2021-07-19 09:45:47 +02:00
parent cf30c42563
commit 55a9e6836d
6 changed files with 57 additions and 45 deletions

View File

@ -57,4 +57,6 @@ pub use requests::{
OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest, OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest,
}; };
pub use store::CryptoStoreError; pub use store::CryptoStoreError;
pub use verification::{AcceptSettings, QrVerification, Sas, Verification, VerificationRequest}; pub use verification::{
AcceptSettings, CancelInfo, QrVerification, Sas, Verification, VerificationRequest,
};

View File

@ -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<Cancelled> 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)] #[derive(Clone, Debug)]
pub struct Cancelled { pub struct Cancelled {
cancelled_by_us: bool, cancelled_by_us: bool,

View File

@ -41,7 +41,7 @@ use tracing::trace;
use super::{ use super::{
event_enums::{CancelContent, DoneContent, OutgoingContent, OwnedStartContent, StartContent}, event_enums::{CancelContent, DoneContent, OutgoingContent, OwnedStartContent, StartContent},
requests::RequestHandle, requests::RequestHandle,
Cancelled, Done, FlowId, IdentitiesBeingVerified, VerificationResult, CancelInfo, Cancelled, Done, FlowId, IdentitiesBeingVerified, VerificationResult,
}; };
use crate::{ use crate::{
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
@ -134,19 +134,11 @@ impl QrVerification {
self.we_started self.we_started
} }
/// Get the `CancelCode` that cancelled this verification request. /// Get info about the cancellation if the verification flow has been
pub fn cancel_code(&self) -> Option<CancelCode> { /// cancelled.
pub fn cancel_info(&self) -> Option<CancelInfo> {
if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() { if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() {
Some(c.state.cancel_code.to_owned()) Some(c.state.clone().into())
} else {
None
}
}
/// Has the verification flow been cancelled by us.
pub fn cancelled_by_us(&self) -> Option<bool> {
if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() {
Some(c.state.cancelled_by_us)
} else { } else {
None None
} }

View File

@ -42,7 +42,7 @@ use super::{
CancelContent, DoneContent, OutgoingContent, ReadyContent, RequestContent, StartContent, CancelContent, DoneContent, OutgoingContent, ReadyContent, RequestContent, StartContent,
}, },
qrcode::{QrVerification, ScanError}, qrcode::{QrVerification, ScanError},
Cancelled, FlowId, IdentitiesBeingVerified, CancelInfo, Cancelled, FlowId, IdentitiesBeingVerified,
}; };
use crate::{ use crate::{
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
@ -219,11 +219,13 @@ impl VerificationRequest {
} }
} }
/// Get the `CancelCode` that cancelled this verification request. /// Get info about the cancellation if the verification request has been
pub fn cancel_code(&self) -> Option<CancelCode> { /// cancelled.
match &*self.inner.lock().unwrap() { pub fn cancel_info(&self) -> Option<CancelInfo> {
InnerRequest::Cancelled(c) => Some(c.state.cancel_code.to_owned()), if let InnerRequest::Cancelled(c) = &*self.inner.lock().unwrap() {
_ => None, Some(c.state.clone().into())
} else {
None
} }
} }

View File

@ -350,22 +350,6 @@ impl InnerSas {
matches!(self, InnerSas::Confirmed(_) | InnerSas::WaitingForDone(_) | InnerSas::Done(_)) matches!(self, InnerSas::Confirmed(_) | InnerSas::WaitingForDone(_) | InnerSas::Done(_))
} }
pub fn cancel_code(&self) -> Option<CancelCode> {
if let InnerSas::Cancelled(c) = self {
Some(c.state.cancel_code.clone())
} else {
None
}
}
pub fn cancelled_by_us(&self) -> Option<bool> {
if let InnerSas::Cancelled(c) = self {
Some(c.state.cancelled_by_us)
} else {
None
}
}
pub fn timed_out(&self) -> bool { pub fn timed_out(&self) -> bool {
match self { match self {
InnerSas::Created(s) => s.timed_out(), InnerSas::Created(s) => s.timed_out(),

View File

@ -35,7 +35,7 @@ use tracing::trace;
use super::{ use super::{
event_enums::{AnyVerificationContent, OutgoingContent, OwnedAcceptContent, StartContent}, event_enums::{AnyVerificationContent, OutgoingContent, OwnedAcceptContent, StartContent},
requests::RequestHandle, requests::RequestHandle,
FlowId, IdentitiesBeingVerified, VerificationResult, CancelInfo, FlowId, IdentitiesBeingVerified, VerificationResult,
}; };
use crate::{ use crate::{
identities::{ReadOnlyDevice, ReadOnlyUserIdentities}, identities::{ReadOnlyDevice, ReadOnlyUserIdentities},
@ -122,14 +122,14 @@ impl Sas {
self.inner.lock().unwrap().has_been_accepted() self.inner.lock().unwrap().has_been_accepted()
} }
/// Get the cancel code of this SAS verification if it has been cancelled /// Get info about the cancellation if the verification flow has been
pub fn cancel_code(&self) -> Option<CancelCode> { /// cancelled.
self.inner.lock().unwrap().cancel_code() pub fn cancel_info(&self) -> Option<CancelInfo> {
} if let InnerSas::Cancelled(c) = &*self.inner.lock().unwrap() {
Some(c.state.as_ref().clone().into())
/// Has the verification flow been cancelled by us. } else {
pub fn cancelled_by_us(&self) -> Option<bool> { None
self.inner.lock().unwrap().cancelled_by_us() }
} }
/// Did we initiate the verification flow. /// Did we initiate the verification flow.