From faea2b4532f3bf6b40182b327fd987b03d831cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 21 May 2023 11:20:34 +0200 Subject: [PATCH] identity-verification: Use widget to show errors --- po/POTFILES.in | 1 - .../identity_verification_widget.rs | 43 +++++++++++++++-- .../verification/identity_verification.rs | 46 ++----------------- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index 22f844cd..be5cebf3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -110,7 +110,6 @@ src/session/sidebar/category/category_type.rs src/session/sidebar/entry_type.rs src/session/sidebar/room_row.rs src/session/sidebar/row.rs -src/session/verification/identity_verification.rs src/user_facing_error.rs src/utils/media.rs src/utils/matrix.rs diff --git a/src/session/content/verification/identity_verification_widget.rs b/src/session/content/verification/identity_verification_widget.rs index 3a79b0c3..483921a0 100644 --- a/src/session/content/verification/identity_verification_widget.rs +++ b/src/session/content/verification/identity_verification_widget.rs @@ -5,6 +5,7 @@ use gettextrs::gettext; use gtk::{gio, glib, glib::clone, prelude::*, CompositeTemplate}; use log::{error, warn}; use matrix_sdk::encryption::verification::QrVerificationData; +use ruma::events::key::verification::cancel::CancelCode; use super::Emoji; use crate::{ @@ -19,7 +20,7 @@ use crate::{ VerificationSupportedMethods, }, }, - spawn, + spawn, toast, }; mod imp { @@ -456,10 +457,8 @@ impl IdentityVerificationWidget { obj.handle_completed().await; })); } - VerificationState::Cancelled - | VerificationState::Dismissed - | VerificationState::Error - | VerificationState::Passive => {} + VerificationState::Cancelled | VerificationState::Error => self.show_error(), + VerificationState::Dismissed | VerificationState::Passive => {} } } @@ -636,6 +635,40 @@ impl IdentityVerificationWidget { imp.main_stack.set_visible_child_name("completed"); } } + + fn show_error(&self) { + let Some(request) = self.request() else { + return; + }; + + if request.hide_error() { + return; + } + + let error_message = if let Some(info) = request.cancel_info() { + match info.cancel_code() { + CancelCode::User => Some(gettext("You cancelled the verification process.")), + CancelCode::Timeout => Some(gettext( + "The verification process failed because it reached a timeout.", + )), + CancelCode::Accepted => { + Some(gettext("You accepted the request from an other session.")) + } + _ => match info.cancel_code().as_str() { + "m.mismatched_sas" => Some(gettext("The emoji did not match.")), + _ => None, + }, + } + } else { + None + }; + + let error_message = error_message.unwrap_or_else(|| { + gettext("An unknown error occurred during the verification process.") + }); + + toast!(self, error_message); + } } /// Get the SAS emoji translations for the current locale. diff --git a/src/session/verification/identity_verification.rs b/src/session/verification/identity_verification.rs index f8bb80f6..e964e61b 100644 --- a/src/session/verification/identity_verification.rs +++ b/src/session/verification/identity_verification.rs @@ -1,6 +1,5 @@ use std::time::Duration; -use gettextrs::gettext; use gtk::{glib, glib::clone, prelude::*, subclass::prelude::*}; use log::{debug, error, warn}; use matrix_sdk::{ @@ -11,10 +10,7 @@ use matrix_sdk::{ VerificationRequest, }, }, - ruma::{ - events::key::verification::{cancel::CancelCode, VerificationMethod}, - UserId, - }, + ruma::{events::key::verification::VerificationMethod, UserId}, Client, }; use qrcode::QrCode; @@ -28,7 +24,7 @@ use crate::{ user::UserExt, Session, User, }, - spawn, spawn_tokio, toast, + spawn, spawn_tokio, }; #[derive(Debug, Default, Eq, PartialEq, Clone, Copy, glib::Enum)] @@ -591,11 +587,6 @@ impl IdentityVerification { return; } - match state { - State::Cancelled | State::Error => self.show_error(), - _ => {} - } - self.imp().state.set(state); self.notify("state"); } @@ -623,40 +614,11 @@ impl IdentityVerification { ) } - fn hide_error(&self) -> bool { + /// Whether to hide errors. + pub fn hide_error(&self) -> bool { self.imp().hide_error.get() } - fn show_error(&self) { - if self.hide_error() { - return; - } - - let error_message = if let Some(info) = self.cancel_info() { - match info.cancel_code() { - CancelCode::User => Some(gettext("You cancelled the verification process.")), - CancelCode::Timeout => Some(gettext( - "The verification process failed because it reached a timeout.", - )), - CancelCode::Accepted => { - Some(gettext("You accepted the request from an other session.")) - } - _ => match info.cancel_code().as_str() { - "m.mismatched_sas" => Some(gettext("The emoji did not match.")), - _ => None, - }, - } - } else { - None - }; - - let error_message = error_message.unwrap_or_else(|| { - gettext("An unknown error occurred during the verification process.") - }); - - toast!(self.session(), error_message); - } - /// The display name of this verification request. pub fn display_name(&self) -> String { if self.user() != self.session().user().unwrap() {