Skip to content

Commit

Permalink
review: use test util instead of cfg(test) default + minor doc update
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Nov 27, 2024
1 parent 3dba012 commit 6c62de7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
41 changes: 21 additions & 20 deletions crates/matrix-sdk-crypto/src/types/events/utd_cause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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};
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -539,4 +533,11 @@ mod tests {
fn raw_event(value: serde_json::Value) -> Raw<AnySyncTimelineEvent> {
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,
}
}
}
9 changes: 6 additions & 3 deletions crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: RoomDataProvider>(
event: AnySyncTimelineEvent,
raw_event: &Raw<AnySyncTimelineEvent>,
Expand Down

0 comments on commit 6c62de7

Please sign in to comment.