diff --git a/spec/unit/room.spec.ts b/spec/unit/room.spec.ts index cdef4906b..fb5aca126 100644 --- a/spec/unit/room.spec.ts +++ b/spec/unit/room.spec.ts @@ -3356,19 +3356,19 @@ describe("Room", function () { it("Returns null if there is no create event", () => { const room = new Room("roomid", client!, "@u:example.com"); - expect(room.findPredecessorRoomId()).toBeNull(); + expect(room.findPredecessor()).toBeNull(); }); it("Returns null if the create event has no predecessor", () => { const room = new Room("roomid", client!, "@u:example.com"); room.addLiveEvents([roomCreateEvent("roomid", null)]); - expect(room.findPredecessorRoomId()).toBeNull(); + expect(room.findPredecessor()).toBeNull(); }); it("Returns the predecessor ID if one is provided via create event", () => { const room = new Room("roomid", client!, "@u:example.com"); room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]); - expect(room.findPredecessorRoomId()).toBe("replacedroomid"); + expect(room.findPredecessor()).toEqual({ roomId: "replacedroomid", eventId: "id_of_last_known_event" }); }); it("Prefers the m.predecessor event if one exists", () => { @@ -3378,7 +3378,10 @@ describe("Room", function () { predecessorEvent("roomid", "otherreplacedroomid"), ]); const useMsc3946 = true; - expect(room.findPredecessorRoomId(useMsc3946)).toBe("otherreplacedroomid"); + expect(room.findPredecessor(useMsc3946)).toEqual({ + roomId: "otherreplacedroomid", + eventId: null, // m.predecessor does not include an event_id + }); }); it("Ignores the m.predecessor event if we don't ask to use it", () => { @@ -3389,7 +3392,7 @@ describe("Room", function () { ]); // Don't provide an argument for msc3946ProcessDynamicPredecessor - // we should ignore the predecessor event. - expect(room.findPredecessorRoomId()).toBe("replacedroomid"); + expect(room.findPredecessor()).toEqual({ roomId: "replacedroomid", eventId: "id_of_last_known_event" }); }); it("Ignores the m.predecessor event and returns null if we don't ask to use it", () => { @@ -3400,7 +3403,7 @@ describe("Room", function () { ]); // Don't provide an argument for msc3946ProcessDynamicPredecessor - // we should ignore the predecessor event. - expect(room.findPredecessorRoomId()).toBeNull(); + expect(room.findPredecessor()).toBeNull(); }); }); }); diff --git a/src/client.ts b/src/client.ts index 005eefcd6..70ee1be6f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3791,7 +3791,7 @@ export class MatrixClient extends TypedEventEmitter { /** * @param msc3946ProcessDynamicPredecessor - if true, look for an - * m.room.predecessor state event and - * use it if found (MSC3946). - * @returns the ID of the room that was this room's predecessor, or null if - * this room has no predecessor. + * m.room.predecessor state event and use it if found (MSC3946). + * @returns null if this room has no predecessor. Otherwise, returns + * the roomId and last eventId of the predecessor room. + * If msc3946ProcessDynamicPredecessor is true, use m.predecessor events + * as well as m.room.create events to find predecessors. + * Note: if an m.predecessor event is used, eventId is null since those + * events do not include an event_id property. */ - public findPredecessorRoomId(msc3946ProcessDynamicPredecessor = false): string | null { + public findPredecessor( + msc3946ProcessDynamicPredecessor = false, + ): { roomId: string; eventId: string | null } | null { const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS); if (!currentState) { return null; @@ -3047,7 +3052,7 @@ export class Room extends ReadReceipt { if (predecessorEvent) { const roomId = predecessorEvent.getContent()["predecessor_room_id"]; if (roomId) { - return roomId; + return { roomId, eventId: null }; } } } @@ -3058,7 +3063,8 @@ export class Room extends ReadReceipt { if (predecessor) { const roomId = predecessor["room_id"]; if (roomId) { - return roomId; + const eventId = predecessor["event_id"] || null; + return { roomId, eventId }; } } }