diff --git a/spec/unit/room.spec.ts b/spec/unit/room.spec.ts index 81f6602e784..5f6f355a315 100644 --- a/spec/unit/room.spec.ts +++ b/spec/unit/room.spec.ts @@ -33,6 +33,7 @@ import { IRelationsRequestOpts, IStateEventWithRoomId, JoinRule, + MatrixClient, MatrixEvent, MatrixEventEvent, PendingEventOrdering, @@ -3225,4 +3226,52 @@ describe("Room", function () { expect(room.getBlacklistUnverifiedDevices()).toBe(false); }); }); + + describe("findPredecessorRoomId", () => { + function roomCreateEvent(newRoomId: string, predecessorRoomId: string | null): MatrixEvent { + const content: { + creator: string; + ["m.federate"]: boolean; + room_version: string; + predecessor: { event_id: string; room_id: string } | undefined; + } = { + "creator": "@daryl:alexandria.example.com", + "predecessor": undefined, + "m.federate": true, + "room_version": "9", + }; + if (predecessorRoomId) { + content.predecessor = { + event_id: "id_of_last_known_event", + room_id: predecessorRoomId, + }; + } + return new MatrixEvent({ + content, + event_id: `create_event_id_pred_${predecessorRoomId}`, + origin_server_ts: 1432735824653, + room_id: newRoomId, + sender: "@daryl:alexandria.example.com", + state_key: "", + type: "m.room.create", + }); + } + + it("Returns null if there is no create event", () => { + const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com"); + expect(room.findPredecessorRoomId()).toBeNull(); + }); + + it("Returns null if the create event has no predecessor", () => { + const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com"); + room.addLiveEvents([roomCreateEvent("roomid", null)]); + expect(room.findPredecessorRoomId()).toBeNull(); + }); + + it("Returns the predecessor ID if one is provided via create event", () => { + const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com"); + room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]); + expect(room.findPredecessorRoomId()).toBe("replacedroomid"); + }); + }); }); diff --git a/src/client.ts b/src/client.ts index 6a7735b51bf..551119307e2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3770,13 +3770,9 @@ export class MatrixClient extends TypedEventEmitter { return this.getType() === RoomType.ElementVideo; } + /** + * @returns the ID of the room that was this room's predecessor, or null if + * this room has no predecessor. + */ + public findPredecessorRoomId(): string | null { + const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS); + if (!currentState) { + return null; + } + + const createEvent = currentState.getStateEvents(EventType.RoomCreate, ""); + if (createEvent) { + const predecessor = createEvent.getContent()["predecessor"]; + if (predecessor) { + const roomId = predecessor["room_id"]; + if (roomId) { + return roomId; + } + } + } + return null; + } + private roomNameGenerator(state: RoomNameState): string { if (this.client.roomNameGenerator) { const name = this.client.roomNameGenerator(this.roomId, state);