From 6c62de71454c1246cc7d1485d4f01b21a41d5a28 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 27 Nov 2024 17:34:02 +0100 Subject: [PATCH] review: use test util instead of cfg(test) default + minor doc update --- .../src/types/events/utd_cause.rs | 41 ++++++++++--------- .../src/timeline/event_handler.rs | 9 ++-- 2 files changed, 27 insertions(+), 23 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 a347526f945..685dc24d03b 100644 --- a/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs +++ b/crates/matrix-sdk-crypto/src/types/events/utd_cause.rs @@ -50,11 +50,12 @@ pub enum UtdCause { /// We are missing the keys for this event, but it is a "device-historical" /// message and no backup is accessible or usable. + /// /// Device-historical means that the message was sent before the current /// device existed (but the current user was probably a member of the room /// at the time the message was sent). Not to /// be confused with pre-join or pre-invite messages (see - /// [`SentBeforeWeJoined`] for that). + /// [`UtdCause::SentBeforeWeJoined`] for that). HistoricalMessage = 5, } @@ -88,13 +89,6 @@ pub struct CryptoContextInfo { pub is_backup_configured: bool, } -#[cfg(test)] -impl Default for CryptoContextInfo { - fn default() -> Self { - Self { device_creation_ts: MilliSecondsSinceUnixEpoch::now(), is_backup_configured: false } - } -} - impl UtdCause { /// Decide the cause of this UTD, based on the evidence we have. pub fn determine( @@ -153,7 +147,7 @@ mod tests { use matrix_sdk_common::deserialized_responses::{ DeviceLinkProblem, UnableToDecryptInfo, UnableToDecryptReason, VerificationLevel, }; - use ruma::{events::AnySyncTimelineEvent, serde::Raw, MilliSecondsSinceUnixEpoch}; + use ruma::{events::AnySyncTimelineEvent, serde::Raw, uint, MilliSecondsSinceUnixEpoch}; use serde_json::{json, value::to_raw_value}; use crate::types::events::{utd_cause::CryptoContextInfo, UtdCause}; @@ -165,7 +159,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({})), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession, @@ -181,7 +175,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({})), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -198,7 +192,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "membership": 3 } })), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -215,7 +209,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "membership": "invite" } }),), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -232,7 +226,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "membership": "join" } })), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -249,7 +243,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "membership": "leave" } })), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -267,7 +261,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "membership": "leave" } })), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MalformedEncryptedEvent @@ -283,7 +277,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({ "unsigned": { "io.element.msc4115.membership": "leave" } })), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::MissingMegolmSession @@ -298,7 +292,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({})), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::SenderIdentityNotTrusted( @@ -315,7 +309,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({})), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::SenderIdentityNotTrusted( @@ -332,7 +326,7 @@ mod tests { assert_eq!( UtdCause::determine( &raw_event(json!({})), - CryptoContextInfo::default(), + some_crypto_context_info(), &UnableToDecryptInfo { session_id: None, reason: UnableToDecryptReason::SenderIdentityNotTrusted( @@ -539,4 +533,11 @@ mod tests { fn raw_event(value: serde_json::Value) -> Raw { Raw::from_json(to_raw_value(&value).unwrap()) } + + fn some_crypto_context_info() -> CryptoContextInfo { + CryptoContextInfo { + device_creation_ts: MilliSecondsSinceUnixEpoch(uint!(42)), + is_backup_configured: false, + } + } } diff --git a/crates/matrix-sdk-ui/src/timeline/event_handler.rs b/crates/matrix-sdk-ui/src/timeline/event_handler.rs index 4bc3b3e9e84..900f9748659 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_handler.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_handler.rs @@ -183,9 +183,12 @@ impl TimelineEventKind { /// # Arguments /// /// * `event` - The event for which we should create a `TimelineEventKind`. - /// * `raw_event` - The [`Raw`] JSON for `event`. (Required so that we can access `unsigned` data.) - /// * `room_data_provider` - An object which will provide information about the room containing the event. - /// * `unable_to_decrypt_info` - If `event` represents a failure to decrypt, information about that failure. Otherwise, `None`. + /// * `raw_event` - The [`Raw`] JSON for `event`. (Required so that we can + /// access `unsigned` data.) + /// * `room_data_provider` - An object which will provide information about + /// the room containing the event. + /// * `unable_to_decrypt_info` - If `event` represents a failure to decrypt, + /// information about that failure. Otherwise, `None`. pub async fn from_event( event: AnySyncTimelineEvent, raw_event: &Raw,