From 7aa930b81c892c8962d6bb8766cf29b42d0db806 Mon Sep 17 00:00:00 2001 From: Timo <16718859+toger5@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:55:25 +0100 Subject: [PATCH] feat(WidgetDriver): Send state from state sync and not from timeline to widget (#4254) --- crates/matrix-sdk/src/widget/matrix.rs | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/matrix-sdk/src/widget/matrix.rs b/crates/matrix-sdk/src/widget/matrix.rs index e30e342d60e..db840dd673e 100644 --- a/crates/matrix-sdk/src/widget/matrix.rs +++ b/crates/matrix-sdk/src/widget/matrix.rs @@ -26,8 +26,9 @@ use ruma::{ }, assign, events::{ - AnyMessageLikeEventContent, AnyStateEventContent, AnySyncTimelineEvent, AnyTimelineEvent, - MessageLikeEventType, StateEventType, TimelineEventType, + AnyMessageLikeEventContent, AnyStateEventContent, AnySyncMessageLikeEvent, + AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, MessageLikeEventType, + StateEventType, TimelineEventType, }, serde::{from_raw_json_value, Raw}, EventId, RoomId, TransactionId, @@ -177,13 +178,31 @@ impl MatrixDriver { pub(crate) fn events(&self) -> EventReceiver { let (tx, rx) = unbounded_channel(); let room_id = self.room.room_id().to_owned(); - let handle = self.room.add_event_handler(move |raw: Raw| { - let _ = tx.send(attach_room_id(&raw, &room_id)); + + // Get only message like events from the timeline section of the sync. + let _tx = tx.clone(); + let _room_id = room_id.clone(); + let handle_msg_like = + self.room.add_event_handler(move |raw: Raw| { + let _ = _tx.send(attach_room_id(raw.cast_ref(), &_room_id)); + async {} + }); + let drop_guard_msg_like = self.room.client().event_handler_drop_guard(handle_msg_like); + + // Get only all state events from the state section of the sync. + let handle_state = self.room.add_event_handler(move |raw: Raw| { + let _ = tx.send(attach_room_id(raw.cast_ref(), &room_id)); async {} }); - - let drop_guard = self.room.client().event_handler_drop_guard(handle); - EventReceiver { rx, _drop_guard: drop_guard } + let drop_guard_state = self.room.client().event_handler_drop_guard(handle_state); + + // The receiver will get a combination of state and message like events. + // The state events will come from the state section of the sync (to always + // represent current resolved state). All state events in the timeline + // section of the sync will not be forwarded to the widget. + // TODO annotate the events and send both timeline and state section state + // events. + EventReceiver { rx, _drop_guards: [drop_guard_msg_like, drop_guard_state] } } } @@ -191,7 +210,7 @@ impl MatrixDriver { /// along with the drop guard for the room event handler. pub(crate) struct EventReceiver { rx: UnboundedReceiver>, - _drop_guard: EventHandlerDropGuard, + _drop_guards: [EventHandlerDropGuard; 2], } impl EventReceiver {