identity-verification: Use widget to show errors

This commit is contained in:
Kévin Commaille 2023-05-21 11:20:34 +02:00
parent af92711df0
commit faea2b4532
No known key found for this signature in database
GPG key ID: 29A48C1F03620416
3 changed files with 42 additions and 48 deletions

View file

@ -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

View file

@ -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.

View file

@ -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() {