Skip to content

Commit

Permalink
Merge pull request #3039 from andybalaam/factor-out-findPredecessor
Browse files Browse the repository at this point in the history
Factor out a (public) function to find a room's predecessor
  • Loading branch information
andybalaam committed Jan 10, 2023
2 parents e2ce379 + c609032 commit 3564a35
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
49 changes: 49 additions & 0 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
IRelationsRequestOpts,
IStateEventWithRoomId,
JoinRule,
MatrixClient,
MatrixEvent,
MatrixEventEvent,
PendingEventOrdering,
Expand Down Expand Up @@ -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");
});
});
});
10 changes: 3 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3770,13 +3770,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const replacedRooms = new Set();
for (const r of allRooms) {
const createEvent = r.currentState.getStateEvents(EventType.RoomCreate, "");
// invites are included in this list and we don't know their create events yet
if (createEvent) {
const predecessor = createEvent.getContent()["predecessor"];
if (predecessor && predecessor["room_id"]) {
replacedRooms.add(predecessor["room_id"]);
}
const predecessor = r.findPredecessorRoomId();
if (predecessor) {
replacedRooms.add(predecessor);
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,29 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
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);
Expand Down

0 comments on commit 3564a35

Please sign in to comment.