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.
This commit is contained in:
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,
};
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)]
pub struct Cancelled {
cancelled_by_us: bool,

View file

@ -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<CancelCode> {
/// Get info about the cancellation if the verification flow has been
/// cancelled.
pub fn cancel_info(&self) -> Option<CancelInfo> {
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<bool> {
if let InnerState::Cancelled(c) = &*self.state.lock().unwrap() {
Some(c.state.cancelled_by_us)
Some(c.state.clone().into())
} else {
None
}

View file

@ -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<CancelCode> {
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<CancelInfo> {
if let InnerRequest::Cancelled(c) = &*self.inner.lock().unwrap() {
Some(c.state.clone().into())
} else {
None
}
}

View file

@ -350,22 +350,6 @@ impl InnerSas {
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 {
match self {
InnerSas::Created(s) => s.timed_out(),

View file

@ -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<CancelCode> {
self.inner.lock().unwrap().cancel_code()
}
/// Has the verification flow been cancelled by us.
pub fn cancelled_by_us(&self) -> Option<bool> {
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<CancelInfo> {
if let InnerSas::Cancelled(c) = &*self.inner.lock().unwrap() {
Some(c.state.as_ref().clone().into())
} else {
None
}
}
/// Did we initiate the verification flow.