From 0865e96f080f1f71bddd481771c94215b2fd195e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 5 Nov 2025 13:40:09 +0000 Subject: [PATCH] refactor(crypto): simplify UtdCause logic I find a single match statement easier to reason about than one nested in another. Also, import `UnableToDecryptReason::*`, to shorten the match lines. --- .../src/types/events/utd_cause.rs | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs b/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs index 1ca78d7bd..e9fcd17f5 100644 --- a/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs +++ b/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs @@ -142,21 +142,22 @@ impl UtdCause { crypto_context_info: CryptoContextInfo, unable_to_decrypt_info: &UnableToDecryptInfo, ) -> Self { - // TODO: in future, use more information to give a richer answer. E.g. + use UnableToDecryptReason::*; match &unable_to_decrypt_info.reason { - UnableToDecryptReason::MissingMegolmSession { withheld_code: Some(reason) } => { - match reason { - WithheldCode::Unverified => UtdCause::WithheldForUnverifiedOrInsecureDevice, - WithheldCode::Blacklisted - | WithheldCode::Unauthorised - | WithheldCode::Unavailable - | WithheldCode::HistoryNotShared - | WithheldCode::NoOlm - | WithheldCode::_Custom(_) => UtdCause::WithheldBySender, - } + MissingMegolmSession { withheld_code: Some(WithheldCode::Unverified) } => { + UtdCause::WithheldForUnverifiedOrInsecureDevice } - UnableToDecryptReason::MissingMegolmSession { withheld_code: None } - | UnableToDecryptReason::UnknownMegolmMessageIndex => { + + MissingMegolmSession { withheld_code: Some(WithheldCode::Blacklisted) } + | MissingMegolmSession { withheld_code: Some(WithheldCode::Unauthorised) } + | MissingMegolmSession { withheld_code: Some(WithheldCode::Unavailable) } + | MissingMegolmSession { withheld_code: Some(WithheldCode::HistoryNotShared) } + | MissingMegolmSession { withheld_code: Some(WithheldCode::NoOlm) } + | MissingMegolmSession { withheld_code: Some(WithheldCode::_Custom(_)) } => { + UtdCause::WithheldBySender + } + + MissingMegolmSession { withheld_code: None } | UnknownMegolmMessageIndex => { // Look in the unsigned area for a `membership` field. if let Some(unsigned) = raw_event.get_field::("unsigned").ok().flatten() @@ -177,17 +178,13 @@ impl UtdCause { UtdCause::Unknown } - UnableToDecryptReason::SenderIdentityNotTrusted( - VerificationLevel::VerificationViolation, - ) => UtdCause::VerificationViolation, - - UnableToDecryptReason::SenderIdentityNotTrusted(VerificationLevel::UnsignedDevice) => { - UtdCause::UnsignedDevice + SenderIdentityNotTrusted(VerificationLevel::VerificationViolation) => { + UtdCause::VerificationViolation } - UnableToDecryptReason::SenderIdentityNotTrusted(VerificationLevel::None(_)) => { - UtdCause::UnknownDevice - } + SenderIdentityNotTrusted(VerificationLevel::UnsignedDevice) => UtdCause::UnsignedDevice, + + SenderIdentityNotTrusted(VerificationLevel::None(_)) => UtdCause::UnknownDevice, _ => UtdCause::Unknown, }