Skip to content

Commit

Permalink
refactor(ui): add UTD info to TimelineEventKind::UnableToDecrypt
Browse files Browse the repository at this point in the history
Stash the reason for the decryption failure in
`matrix-sdk-ui::event_handler::TimelineEventKind::UnableToDecrypt`.

It's not yet used.
  • Loading branch information
richvdh committed Oct 17, 2024
1 parent c21bdd5 commit 4cd5883
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
20 changes: 15 additions & 5 deletions crates/matrix-sdk-ui/src/timeline/controller/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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,
},
Expand Down
33 changes: 27 additions & 6 deletions crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<UnableToDecryptInfo>,
) -> Self {
match event {
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomRedaction(ev)) => {
if let Some(redacts) = ev.redacts(room_version).map(ToOwned::to_owned) {
Expand All @@ -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() },
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4cd5883

Please sign in to comment.