From 2d94b76b4913cf5f17c0dff338cd6457b8dcb28e Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 1 Feb 2023 12:03:20 +0000 Subject: [PATCH] Handle optional last_known_event_id property in m.predecessor --- spec/unit/room.spec.ts | 32 ++++++++++++++++++++++++++------ src/models/room-state.ts | 6 +++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/spec/unit/room.spec.ts b/spec/unit/room.spec.ts index 83ae123ffdb..4fe46ee8dae 100644 --- a/spec/unit/room.spec.ts +++ b/spec/unit/room.spec.ts @@ -3340,11 +3340,18 @@ describe("Room", function () { }); } - function predecessorEvent(newRoomId: string, predecessorRoomId: string): MatrixEvent { + function predecessorEvent( + newRoomId: string, + predecessorRoomId: string, + tombstoneEventId: string | null = null, + ): MatrixEvent { + const content = + tombstoneEventId === null + ? { predecessor_room_id: predecessorRoomId } + : { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId }; + return new MatrixEvent({ - content: { - predecessor_room_id: predecessorRoomId, - }, + content, event_id: `predecessor_event_id_pred_${predecessorRoomId}`, origin_server_ts: 1432735824653, room_id: newRoomId, @@ -3380,7 +3387,20 @@ describe("Room", function () { const useMsc3946 = true; expect(room.findPredecessor(useMsc3946)).toEqual({ roomId: "otherreplacedroomid", - eventId: null, // m.predecessor does not include an event_id + eventId: null, // m.predecessor did not include an event_id + }); + }); + + it("uses the m.predecessor event ID if provided", () => { + const room = new Room("roomid", client!, "@u:example.com"); + room.addLiveEvents([ + roomCreateEvent("roomid", "replacedroomid"), + predecessorEvent("roomid", "otherreplacedroomid", "lstevtid"), + ]); + const useMsc3946 = true; + expect(room.findPredecessor(useMsc3946)).toEqual({ + roomId: "otherreplacedroomid", + eventId: "lstevtid", }); }); @@ -3399,7 +3419,7 @@ describe("Room", function () { const room = new Room("roomid", client!, "@u:example.com"); room.addLiveEvents([ roomCreateEvent("roomid", null), // Create event has no predecessor - predecessorEvent("roomid", "otherreplacedroomid"), + predecessorEvent("roomid", "otherreplacedroomid", "lastevtid"), ]); // Don't provide an argument for msc3946ProcessDynamicPredecessor - // we should ignore the predecessor event. diff --git a/src/models/room-state.ts b/src/models/room-state.ts index 77693e9743e..e704d27a8cf 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -986,8 +986,12 @@ export class RoomState extends TypedEventEmitter const predecessorEvent = this.getStateEvents(EventType.RoomPredecessor, ""); if (predecessorEvent) { const roomId = predecessorEvent.getContent()["predecessor_room_id"]; + let eventId = predecessorEvent.getContent()["last_known_event_id"]; + if (typeof eventId !== "string") { + eventId = null; + } if (typeof roomId === "string") { - return { roomId, eventId: null }; + return { roomId, eventId }; } } }