From 4cd58837783c8b101c2fa277f899d628ad99e8db Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 11 Oct 2024 16:21:20 +0100 Subject: [PATCH] refactor(ui): add UTD info to `TimelineEventKind::UnableToDecrypt` Stash the reason for the decryption failure in `matrix-sdk-ui::event_handler::TimelineEventKind::UnableToDecrypt`. It's not yet used. --- .../src/timeline/controller/state.rs | 20 ++++++++--- .../src/timeline/event_handler.rs | 33 +++++++++++++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk-ui/src/timeline/controller/state.rs b/crates/matrix-sdk-ui/src/timeline/controller/state.rs index 4f23f58a426..b30102525fb 100644 --- a/crates/matrix-sdk-ui/src/timeline/controller/state.rs +++ b/crates/matrix-sdk-ui/src/timeline/controller/state.rs @@ -422,7 +422,17 @@ impl TimelineStateTransaction<'_> { settings: &TimelineSettings, day_divider_adjuster: &mut DayDividerAdjuster, ) -> HandleEventResult { - let raw = event.raw(); + let SyncTimelineEvent { push_actions, kind } = event; + let encryption_info = kind.encryption_info().cloned(); + + let (raw, utd_info) = match kind { + matrix_sdk::deserialized_responses::TimelineEventKind::UnableToDecrypt { + utd_info, + event, + } => (event, Some(utd_info)), + _ => (kind.into_raw(), None), + }; + let (event_id, sender, timestamp, txn_id, event_kind, should_add) = match raw.deserialize() { Ok(event) => { @@ -479,7 +489,7 @@ impl TimelineStateTransaction<'_> { event.sender().to_owned(), event.origin_server_ts(), event.transaction_id().map(ToOwned::to_owned), - TimelineEventKind::from_event(event, &room_version), + TimelineEventKind::from_event(event, &room_version, utd_info), should_add, ) } @@ -578,11 +588,11 @@ impl TimelineStateTransaction<'_> { } else { Default::default() }, - is_highlighted: event.push_actions.iter().any(Action::is_highlight), + is_highlighted: push_actions.iter().any(Action::is_highlight), flow: Flow::Remote { event_id: event_id.clone(), - raw_event: raw.clone(), - encryption_info: event.encryption_info().cloned(), + raw_event: raw, + encryption_info, txn_id, position, }, diff --git a/crates/matrix-sdk-ui/src/timeline/event_handler.rs b/crates/matrix-sdk-ui/src/timeline/event_handler.rs index 835ec1f5641..210fec68e39 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_handler.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_handler.rs @@ -18,8 +18,10 @@ use as_variant::as_variant; use eyeball_im::{ObservableVectorTransaction, ObservableVectorTransactionEntry}; use indexmap::IndexMap; use matrix_sdk::{ - crypto::types::events::UtdCause, deserialized_responses::EncryptionInfo, - ring_buffer::RingBuffer, send_queue::SendHandle, + crypto::types::events::UtdCause, + deserialized_responses::{EncryptionInfo, UnableToDecryptInfo}, + ring_buffer::RingBuffer, + send_queue::SendHandle, }; use ruma::{ events::{ @@ -139,7 +141,10 @@ pub(super) enum TimelineEventKind { }, /// An encrypted event that could not be decrypted - UnableToDecrypt { content: RoomEncryptedEventContent }, + UnableToDecrypt { + content: RoomEncryptedEventContent, + unable_to_decrypt_info: UnableToDecryptInfo, + }, /// Some remote event that was redacted a priori, i.e. we never had the /// original content, so we'll just display a dummy redacted timeline @@ -176,7 +181,11 @@ pub(super) enum TimelineEventKind { impl TimelineEventKind { /// Creates a new `TimelineEventKind` with the given event and room version. - pub fn from_event(event: AnySyncTimelineEvent, room_version: &RoomVersionId) -> Self { + pub fn from_event( + event: AnySyncTimelineEvent, + room_version: &RoomVersionId, + unable_to_decrypt_info: Option, + ) -> Self { match event { AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomRedaction(ev)) => { if let Some(redacts) = ev.redacts(room_version).map(ToOwned::to_owned) { @@ -188,7 +197,19 @@ impl TimelineEventKind { AnySyncTimelineEvent::MessageLike(ev) => match ev.original_content() { Some(AnyMessageLikeEventContent::RoomEncrypted(content)) => { // An event which is still encrypted. - Self::UnableToDecrypt { content } + if let Some(unable_to_decrypt_info) = unable_to_decrypt_info { + Self::UnableToDecrypt { content, unable_to_decrypt_info } + } else { + // If we get here, it means that some part of the code has created a + // `SyncTimelineEvent` containing an `m.room.encrypted` event + // without decrypting it. Possibly this means that encryption has not been + // configured. + // We treat it the same as any other message-like event. + Self::Message { + content: AnyMessageLikeEventContent::RoomEncrypted(content), + relations: ev.relations(), + } + } } Some(content) => Self::Message { content, relations: ev.relations() }, None => Self::RedactedMessage { event_type: ev.event_type() }, @@ -395,7 +416,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> { } }, - TimelineEventKind::UnableToDecrypt { content } => { + TimelineEventKind::UnableToDecrypt { content, .. } => { // TODO: Handle replacements if the replaced event is also UTD let raw_event = self.ctx.flow.raw_event(); let cause = UtdCause::determine(raw_event);