diff --git a/crates/matrix-sdk-base/src/sliding_sync/mod.rs b/crates/matrix-sdk-base/src/sliding_sync/mod.rs index 819d1662eec..17efc70323e 100644 --- a/crates/matrix-sdk-base/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk-base/src/sliding_sync/mod.rs @@ -876,7 +876,10 @@ mod tests { }; use assert_matches::assert_matches; - use matrix_sdk_common::{deserialized_responses::SyncTimelineEvent, ring_buffer::RingBuffer}; + use matrix_sdk_common::{ + deserialized_responses::{SyncTimelineEvent, UnableToDecryptInfo, UnableToDecryptReason}, + ring_buffer::RingBuffer, + }; use matrix_sdk_test::async_test; use ruma::{ api::client::sync::sync_events::UnreadNotificationsCount, @@ -2494,7 +2497,7 @@ mod tests { } fn make_encrypted_event(id: &str) -> SyncTimelineEvent { - SyncTimelineEvent::new( + SyncTimelineEvent::new_utd_event( Raw::from_json_string( json!({ "type": "m.room.encrypted", @@ -2512,6 +2515,10 @@ mod tests { .to_string(), ) .unwrap(), + UnableToDecryptInfo { + session_id: Some("".to_owned()), + reason: UnableToDecryptReason::MissingMegolmSession, + }, ) } diff --git a/crates/matrix-sdk-ui/src/timeline/tests/encryption.rs b/crates/matrix-sdk-ui/src/timeline/tests/encryption.rs index ce038e61574..4b0683529d8 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/encryption.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/encryption.rs @@ -27,7 +27,7 @@ use matrix_sdk::{ crypto::{decrypt_room_key_export, types::events::UtdCause, OlmMachine}, test_utils::test_client_builder, }; -use matrix_sdk_base::deserialized_responses::SyncTimelineEvent; +use matrix_sdk_base::deserialized_responses::{SyncTimelineEvent, UnableToDecryptReason}; use matrix_sdk_test::{async_test, BOB}; use ruma::{ assign, @@ -105,7 +105,8 @@ async fn test_retry_message_decryption() { ), None, )) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -215,7 +216,11 @@ async fn test_retry_edit_decryption() { .into(), ); timeline - .handle_live_event(f.event(RoomEncryptedEventContent::new(encrypted, None)).sender(&BOB)) + .handle_live_event( + f.event(RoomEncryptedEventContent::new(encrypted, None)) + .sender(&BOB) + .into_utd_sync_timeline_event(), + ) .await; let event_id = @@ -242,7 +247,8 @@ async fn test_retry_edit_decryption() { f.event(assign!(RoomEncryptedEventContent::new(encrypted, None), { relates_to: Some(Relation::Replacement(Replacement::new(event_id))), })) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -321,7 +327,8 @@ async fn test_retry_edit_and_more() { mBZdKIaqDTUBFvcvbn2gQaWtUipQdJQRKyv2h0AWveVkv75lp5hRb7jolCi08oMX8cM+V3Zzyi7\ mlPAzZjDz0PaRbQwfbMTTHkcL7TZybBi4vLX4f5ZR2Iiysc7gw", )) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -337,7 +344,9 @@ async fn test_retry_edit_and_more() { ); timeline .handle_live_event( - f.event(assign!(msg2, { relates_to: Some(Relation::Replacement(Replacement::new(event_id))) })).sender(&BOB), + f.event(assign!(msg2, { relates_to: Some(Relation::Replacement(Replacement::new(event_id))) })) + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -349,7 +358,8 @@ async fn test_retry_edit_and_more() { 2r/fEvAW/9QB+N6fX4g9729bt5ftXRqa5QI7NA351RNUveRHxVvx+2x0WJArQjYGRk7tMS2rUto\ IYt2ZY17nE1UJjN7M87STnCF9c9qy4aGNqIpeVIht6XbtgD7gQ", )) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -422,7 +432,8 @@ async fn test_retry_message_decryption_highlighted() { ), None, )) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; @@ -547,7 +558,7 @@ async fn test_utd_cause_for_missing_membership_is_unknown() { } fn utd_event_with_unsigned(unsigned: serde_json::Value) -> SyncTimelineEvent { - SyncTimelineEvent::new(Raw::from_json( + let raw = Raw::from_json( to_raw_value(&json!({ "event_id": "$myevent", "sender": "@u:s", @@ -564,5 +575,13 @@ fn utd_event_with_unsigned(unsigned: serde_json::Value) -> SyncTimelineEvent { })) .unwrap(), - )) + ); + + SyncTimelineEvent::new_utd_event( + raw, + matrix_sdk::deserialized_responses::UnableToDecryptInfo { + session_id: Some("SESSION_ID".into()), + reason: UnableToDecryptReason::MissingMegolmSession, + }, + ) } diff --git a/crates/matrix-sdk-ui/src/timeline/tests/read_receipts.rs b/crates/matrix-sdk-ui/src/timeline/tests/read_receipts.rs index 574a8bec6a4..6f0c2e02a17 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/read_receipts.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/read_receipts.rs @@ -400,7 +400,8 @@ async fn test_read_receipts_updates_on_message_decryption() { ), None, )) - .sender(&BOB), + .sender(&BOB) + .into_utd_sync_timeline_event(), ) .await; diff --git a/crates/matrix-sdk-ui/src/timeline/tests/shields.rs b/crates/matrix-sdk-ui/src/timeline/tests/shields.rs index 5cc973c4016..33c90d0e25c 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/shields.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/shields.rs @@ -151,7 +151,8 @@ async fn test_utd_shield() { ), None, )) - .sender(&ALICE), + .sender(&ALICE) + .into_utd_sync_timeline_event(), ) .await; diff --git a/crates/matrix-sdk/src/test_utils/events.rs b/crates/matrix-sdk/src/test_utils/events.rs index 9ace4ccd49f..3ecaebb5b47 100644 --- a/crates/matrix-sdk/src/test_utils/events.rs +++ b/crates/matrix-sdk/src/test_utils/events.rs @@ -16,7 +16,9 @@ use std::sync::atomic::{AtomicU64, Ordering::SeqCst}; +use as_variant::as_variant; use matrix_sdk_base::deserialized_responses::{SyncTimelineEvent, TimelineEvent}; +use matrix_sdk_common::deserialized_responses::UnableToDecryptReason; use ruma::{ events::{ message::TextContentBlock, @@ -31,6 +33,7 @@ use ruma::{ reaction::ReactionEventContent, relation::{Annotation, InReplyTo, Replacement, Thread}, room::{ + encrypted::{EncryptedEventScheme, RoomEncryptedEventContent}, message::{Relation, RoomMessageEventContent, RoomMessageEventContentWithoutRelation}, redaction::RoomRedactionEventContent, }, @@ -185,6 +188,23 @@ where } } +impl EventBuilder { + /// Turn this event into a SyncTimelineEvent representing a decryption + /// failure + pub fn into_utd_sync_timeline_event(self) -> SyncTimelineEvent { + let session_id = as_variant!(&self.content.scheme, EncryptedEventScheme::MegolmV1AesSha2) + .map(|content| content.session_id.clone()); + + SyncTimelineEvent::new_utd_event( + self.into(), + crate::deserialized_responses::UnableToDecryptInfo { + session_id, + reason: UnableToDecryptReason::MissingMegolmSession, + }, + ) + } +} + impl EventBuilder { /// Adds a reply relation to the current event. pub fn reply_to(mut self, event_id: &EventId) -> Self {