feat(sdk): Retry event decryption on forwarded room keys
use std::{iter, sync::Arc};
-use ruma::{events::room_key::ToDeviceRoomKeyEvent, OwnedRoomId};
+use ruma::{
+ events::{forwarded_room_key::ToDeviceForwardedRoomKeyEvent, room_key::ToDeviceRoomKeyEvent},
+ OwnedRoomId,
+};
use tracing::{debug_span, error, trace, Instrument};
use super::inner::TimelineInner;
@@ -14,9 +17,38 @@ pub(super) fn handle_room_key_event(
let inner = inner.clone();
let room_id = room_id.clone();
async move {
- if event.content.room_id != room_id {
- let event_room_id = &event.content.room_id;
- let session_id = &event.content.session_id;
+ let event_room_id = event.content.room_id;
+ let session_id = event.content.session_id;
+ retry_decryption(client, inner, room_id, event_room_id, session_id).await;
+ }
+ .instrument(debug_span!("handle_room_key_event"))
+ }
+}
+
+pub(super) fn handle_forwarded_room_key_event(
+ inner: Arc<TimelineInner>,
+ room_id: OwnedRoomId,
+) -> impl EventHandler<ToDeviceForwardedRoomKeyEvent, (Client,)> {
+ move |event: ToDeviceForwardedRoomKeyEvent, client: Client| {
+ let inner = inner.clone();
+ let room_id = room_id.clone();
+ async move {
+ let event_room_id = event.content.room_id;
+ let session_id = event.content.session_id;
+ retry_decryption(client, inner, room_id, event_room_id, session_id).await;
+ }
+ .instrument(debug_span!("handle_forwarded_room_key_event"))
+ }
+}
+
+async fn retry_decryption(
+ client: Client,
+ inner: Arc<TimelineInner>,
+ room_id: OwnedRoomId,
+ event_room_id: OwnedRoomId,
+ session_id: String,
+) {
+ if event_room_id != room_id {
trace!(
%event_room_id, timeline_room_id = %room_id, %session_id,
"Received to-device room key event for a different room, ignoring"
@@ -29,7 +61,6 @@ pub(super) fn handle_room_key_event(
return;
};
- let session_id = event.content.session_id;
let Some(own_user_id) = client.user_id() else {
error!("The user's own ID isn't available");
return;
@@ -44,6 +75,3 @@ pub(super) fn handle_room_key_event(
)
.await;
}
- .instrument(debug_span!("handle_room_key_event"))
- }
-}
