Commit 40ff17155c

40ff17155c61535ca42c831d4758c7587745ba2b

parent: b8c248a756

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-22 10:44 UTC
committer: <strk@kbt.io>

fix: suppress duplicate restored backlog events

Install the saved history cursor before member restoration awaits and avoid printing Matrix event IDs already present in room or thread buffers.

Co-authored-by: Jakub Panek <me@panekj.dev>

Layout: unified · split

src/room/mod.rs +24 −3
@@ -116,6 +116,10 @@ fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> {
116116 prev_batch.map(PrevBatch::Backwards)
117117}
118118
119fn should_render_event(already_rendered: bool) -> bool {
120 !already_rendered
121}
122
119123impl Deref for RoomHandle {
120124 type Target = MatrixRoom;
121125
@@ -477,6 +481,11 @@ impl RoomHandle {
477481
478482 debug!("Restoring room {}", room.room_id());
479483
484 // Sync callbacks can run while member restoration awaits the SDK. Put
485 // the saved history cursor in place first so a sync event cannot set a
486 // newer cursor which is then overwritten with the saved one.
487 *room_buffer.prev_batch.borrow_mut() = restored_prev_batch(prev_batch);
488
480489 let matrix_members = runtime
481490 .spawn(async move { room.joined_user_ids().await })
482491 .await
@@ -487,8 +496,6 @@ impl RoomHandle {
487496 room_buffer.members.restore_member(user_id).await;
488497 }
489498
490 *room_buffer.prev_batch.borrow_mut() = restored_prev_batch(prev_batch);
491
492499 room_buffer.buffer.update_buffer_name();
493500 room_buffer.buffer.set_topic();
494501 room_buffer.buffer.update_parent_spaces();
@@ -1523,6 +1530,10 @@ impl MatrixRoom {
15231530 self.verification.handle_room_verification(event).await;
15241531 } else if event.is_edit() {
15251532 self.handle_edits(event).await;
1533 } else if !should_render_event(
1534 self.buffer.contains_event(event.event_id()),
1535 ) {
1536 return;
15261537 } else if let Some(rendered) = self.render_sync_message(event).await {
15271538 let thread_root = thread_root_from_event(event);
15281539
@@ -1635,7 +1646,11 @@ impl MatrixRoom {
16351646 AnyTimelineEvent::MessageLike(event) => {
16361647 // TODO: Only print out historical events if they aren't edits of
16371648 // other events.
1638 if !event.is_edit() {
1649 if !event.is_edit()
1650 && should_render_event(
1651 self.buffer.contains_event(event.event_id()),
1652 )
1653 {
16391654 let sender = self.members.get(event.sender()).await.expect(
16401655 "Rendering a message but the sender isn't in the nicklist",
16411656 );
@@ -1721,6 +1736,12 @@ mod tests {
17211736 assert_eq!(restored_prev_batch(None), None);
17221737 }
17231738
1739 #[test]
1740 fn already_rendered_events_are_not_printed_again() {
1741 assert!(should_render_event(false));
1742 assert!(!should_render_event(true));
1743 }
1744
17241745 #[test]
17251746 fn thread_buffer_input_sends_thread_relation() {
17261747 let content = make_text_message_content(