diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 36a65d3b..5979ad50 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -26,7 +26,7 @@ use super::{ event_enums::{AnyEvent, AnyVerificationContent}, requests::VerificationRequest, sas::{content_to_request, OutgoingContent, Sas}, - FlowId, VerificationResult, + FlowId, Verification, VerificationResult, }; use crate::{ olm::PrivateCrossSigningIdentity, @@ -37,74 +37,50 @@ use crate::{ #[derive(Clone, Debug)] pub struct VerificationCache { - sas_verification: Arc>, - room_sas_verifications: Arc>, + verification: Arc>, outgoing_requests: Arc>, } impl VerificationCache { pub fn new() -> Self { - Self { - sas_verification: DashMap::new().into(), - room_sas_verifications: DashMap::new().into(), - outgoing_requests: DashMap::new().into(), - } + Self { verification: DashMap::new().into(), outgoing_requests: DashMap::new().into() } } #[cfg(test)] fn is_empty(&self) -> bool { - self.room_sas_verifications.is_empty() && self.sas_verification.is_empty() + self.verification.is_empty() } pub fn insert_sas(&self, sas: Sas) { - match sas.flow_id() { - super::FlowId::ToDevice(t) => self.sas_verification.insert(t.to_owned(), sas), - super::FlowId::InRoom(_, e) => self.room_sas_verifications.insert(e.to_owned(), sas), - }; + self.verification.insert(sas.flow_id().as_str().to_string(), sas.into()); } pub fn garbage_collect(&self) -> Vec { - self.sas_verification.retain(|_, s| !(s.is_done() || s.is_cancelled())); - self.room_sas_verifications.retain(|_, s| !(s.is_done() || s.is_cancelled())); + self.verification.retain(|_, s| !(s.is_done() || s.is_cancelled())); - let mut requests: Vec = self - .sas_verification + self.verification .iter() .filter_map(|s| { - s.cancel_if_timed_out().map(|r| OutgoingRequest { - request_id: r.request_id(), - request: Arc::new(r.into()), - }) + if let Verification::SasV1(s) = s.value() { + s.cancel_if_timed_out().map(|r| OutgoingRequest { + request_id: r.request_id(), + request: Arc::new(r.into()), + }) + } else { + None + } }) - .collect(); - let room_requests: Vec = self - .room_sas_verifications - .iter() - .filter_map(|s| { - s.cancel_if_timed_out().map(|r| OutgoingRequest { - request_id: r.request_id(), - request: Arc::new(r.into()), - }) - }) - .collect(); - - requests.extend(room_requests); - - requests + .collect() } pub fn get_sas(&self, transaction_id: &str) -> Option { - let sas = if let Ok(e) = EventId::try_from(transaction_id) { - self.room_sas_verifications.get(&e).map(|s| s.clone()) - } else { - None - }; - - if sas.is_some() { - sas - } else { - self.sas_verification.get(transaction_id).map(|s| s.clone()) - } + self.verification.get(transaction_id).and_then(|v| { + if let Verification::SasV1(sas) = v.value() { + Some(sas.clone()) + } else { + None + } + }) } pub fn add_request(&self, request: OutgoingRequest) { @@ -195,9 +171,7 @@ impl VerificationMachine { let request = content_to_request(device.user_id(), device.device_id().to_owned(), c); - self.verifications - .sas_verification - .insert(sas.flow_id().as_str().to_owned(), sas.clone()); + self.verifications.insert_sas(sas.clone()); request.into() } diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 6bd33830..a94207f0 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -44,6 +44,31 @@ use crate::{ CryptoStoreError, LocalTrust, ReadOnlyDevice, UserIdentities, }; +#[derive(Clone, Debug)] +pub enum Verification { + SasV1(Sas), +} + +impl Verification { + pub fn is_done(&self) -> bool { + match self { + Verification::SasV1(s) => s.is_done(), + } + } + + pub fn is_cancelled(&self) -> bool { + match self { + Verification::SasV1(s) => s.is_cancelled(), + } + } +} + +impl From for Verification { + fn from(sas: Sas) -> Self { + Self::SasV1(sas) + } +} + /// The verification state indicating that the verification finished /// successfully. ///