Skip to content

Commit

Permalink
feat(WidgetDriver): Send state from state sync and not from timeline …
Browse files Browse the repository at this point in the history
…to widget (#4254)
  • Loading branch information
toger5 authored Nov 14, 2024
1 parent c02d8ce commit 7aa930b
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions crates/matrix-sdk/src/widget/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -177,21 +178,39 @@ 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<AnySyncTimelineEvent>| {
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<AnySyncMessageLikeEvent>| {
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<AnySyncStateEvent>| {
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] }
}
}

/// A simple entity that wraps an `UnboundedReceiver`
/// along with the drop guard for the room event handler.
pub(crate) struct EventReceiver {
rx: UnboundedReceiver<Raw<AnyTimelineEvent>>,
_drop_guard: EventHandlerDropGuard,
_drop_guards: [EventHandlerDropGuard; 2],
}

impl EventReceiver {
Expand Down

0 comments on commit 7aa930b

Please sign in to comment.