Skip to content

Commit

Permalink
feat(timeline): Listen to the room keys stream to retry decryptions
Browse files Browse the repository at this point in the history
  • Loading branch information
poljar committed Nov 28, 2024
1 parent ad77d20 commit 66d6f49
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/matrix-sdk-ui/src/timeline/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use matrix_sdk::{
};
use ruma::{events::AnySyncTimelineEvent, RoomVersionId};
use tokio::sync::broadcast::error::RecvError;
use tokio_stream::wrappers::errors::BroadcastStreamRecvError;
use tracing::{info, info_span, trace, warn, Instrument, Span};

use super::{
Expand Down Expand Up @@ -426,6 +427,44 @@ impl TimelineBuilder {
})
};

// TODO: Technically this should™ be the only stream we need to listen to get
// notified when we should retry to decrypt an event. We sadly can't do that,
// since the cross-process support kills the `OlmMachine` which then in
// turn kills this stream. Once this is solved remove all the other ways we
// listen for room keys.
let room_keys_received_join_handle = {
let inner = controller.clone();
let stream = client.encryption().room_keys_received_stream().await.expect("");

spawn(async move {
pin_mut!(stream);

while let Some(room_keys) = stream.next().await {
let session_ids = match room_keys {
Ok(room_keys) => {
let session_ids: BTreeSet<String> = room_keys
.into_iter()
.filter(|info| info.room_id == inner.room().room_id())
.map(|info| info.session_id)
.collect();

Some(session_ids)
}
Err(BroadcastStreamRecvError::Lagged(missed_updates)) => {
// We lagged, let's retry to decrypt anything we have, maybe something
// was received.
warn!(missed_updates, "The room keys stream has lagged, retrying to decrypt the whole timeline");

None
}
};

let room = inner.room();
inner.retry_event_decryption(room, session_ids).await;
}
})
};

let timeline = Timeline {
controller,
event_cache: room_event_cache,
Expand All @@ -436,6 +475,7 @@ impl TimelineBuilder {
pinned_events_join_handle,
room_key_from_backups_join_handle,
room_key_backup_enabled_join_handle,
room_keys_received_join_handle,
local_echo_listener_handle,
_event_cache_drop_handle: event_cache_drop,
encryption_changes_handle,
Expand Down
2 changes: 2 additions & 0 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ struct TimelineDropHandle {
room_update_join_handle: JoinHandle<()>,
pinned_events_join_handle: Option<JoinHandle<()>>,
room_key_from_backups_join_handle: JoinHandle<()>,
room_keys_received_join_handle: JoinHandle<()>,
room_key_backup_enabled_join_handle: JoinHandle<()>,
local_echo_listener_handle: JoinHandle<()>,
_event_cache_drop_handle: Arc<EventCacheDropHandles>,
Expand All @@ -836,6 +837,7 @@ impl Drop for TimelineDropHandle {
self.room_update_join_handle.abort();
self.room_key_from_backups_join_handle.abort();
self.room_key_backup_enabled_join_handle.abort();
self.room_keys_received_join_handle.abort();
self.encryption_changes_handle.abort();
}
}
Expand Down

0 comments on commit 66d6f49

Please sign in to comment.