From cc0388929a632fd2495d1c208176a92d83b2b3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 10 Jun 2021 16:23:16 +0200 Subject: [PATCH] crypto: Add some more accessors for the fields in the verification types --- matrix_sdk_crypto/src/verification/mod.rs | 16 ++++++------ .../src/verification/requests.rs | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index aa0a99e8..0a8f789e 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -57,14 +57,6 @@ pub enum Verification { } impl Verification { - /// Has this verification finished. - pub fn is_done(&self) -> bool { - match self { - Verification::SasV1(s) => s.is_done(), - Verification::QrV1(qr) => qr.is_done(), - } - } - /// Try to deconstruct this verification enum into a SAS verification. pub fn sas_v1(self) -> Option { if let Verification::SasV1(sas) = self { @@ -83,6 +75,14 @@ impl Verification { } } + /// Has this verification finished. + pub fn is_done(&self) -> bool { + match self { + Verification::SasV1(s) => s.is_done(), + Verification::QrV1(qr) => qr.is_done(), + } + } + /// Get the ID that uniquely identifies this verification flow. pub fn flow_id(&self) -> &str { match self { diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index aea0dde0..363e4f29 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -166,6 +166,11 @@ impl VerificationRequest { ) } + /// Our own user id. + pub fn own_user_id(&self) -> &UserId { + self.account.user_id() + } + /// The id of the other user that is participating in this verification /// request. pub fn other_user(&self) -> &UserId { @@ -275,6 +280,27 @@ impl VerificationRequest { }) } + /// Cancel the verification request + pub fn cancel(&self) -> Option { + let mut inner = self.inner.lock().unwrap(); + inner.cancel(&CancelCode::User); + + let content = if let InnerRequest::Cancelled(c) = &*inner { + Some(c.state.as_content(self.flow_id())) + } else { + None + }; + + content.map(|c| match c { + OutgoingContent::ToDevice(content) => { + ToDeviceRequest::new(&self.other_user(), inner.other_device_id(), content).into() + } + OutgoingContent::Room(room_id, content) => { + RoomMessageRequest { room_id, txn_id: Uuid::new_v4(), content }.into() + } + }) + } + #[allow(clippy::unnecessary_wraps)] pub(crate) fn receive_ready(&self, sender: &UserId, content: &ReadyContent) -> Result<(), ()> { let mut inner = self.inner.lock().unwrap();