fractal/session/model/verification/
mod.rs

1use gtk::{glib, prelude::*};
2use matrix_sdk::encryption::verification::VerificationRequest;
3use ruma::{OwnedUserId, UserId, events::key::verification::VerificationMethod};
4
5mod identity_verification;
6mod verification_list;
7
8pub(crate) use self::{
9    identity_verification::{
10        IdentityVerification, VerificationState, VerificationSupportedMethods,
11    },
12    verification_list::VerificationList,
13};
14use crate::{components::Camera, prelude::*};
15
16/// A unique key to identify an identity verification.
17#[derive(Debug, Clone, Hash, PartialEq, Eq)]
18pub(crate) struct VerificationKey {
19    /// The ID of the user being verified.
20    pub(crate) user_id: OwnedUserId,
21    /// The ID of the verification.
22    pub(crate) flow_id: String,
23}
24
25impl VerificationKey {
26    /// Create a new `VerificationKey` with the given user ID and flow ID.
27    pub(crate) fn new(user_id: OwnedUserId, flow_id: String) -> Self {
28        Self { user_id, flow_id }
29    }
30
31    /// Create a new `VerificationKey` from the given [`VerificationRequest`].
32    pub(crate) fn from_request(request: &VerificationRequest) -> Self {
33        Self::new(
34            request.other_user_id().to_owned(),
35            request.flow_id().to_owned(),
36        )
37    }
38}
39
40impl StaticVariantType for VerificationKey {
41    fn static_variant_type() -> std::borrow::Cow<'static, glib::VariantTy> {
42        <(String, String)>::static_variant_type()
43    }
44}
45
46impl ToVariant for VerificationKey {
47    fn to_variant(&self) -> glib::Variant {
48        (self.user_id.as_str(), self.flow_id.as_str()).to_variant()
49    }
50}
51
52impl FromVariant for VerificationKey {
53    fn from_variant(variant: &glib::Variant) -> Option<Self> {
54        let (user_id_str, flow_id) = variant.get::<(String, String)>()?;
55        let user_id = UserId::parse(user_id_str).ok()?;
56        Some(Self { user_id, flow_id })
57    }
58}
59
60/// Load the supported verification methods on this system.
61async fn load_supported_verification_methods() -> Vec<VerificationMethod> {
62    let mut methods = vec![
63        VerificationMethod::SasV1,
64        VerificationMethod::QrCodeShowV1,
65        VerificationMethod::ReciprocateV1,
66    ];
67
68    let has_cameras = Camera::has_cameras().await;
69
70    if has_cameras {
71        methods.push(VerificationMethod::QrCodeScanV1);
72    }
73
74    methods
75}