Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out a (public) function to find a room's predecessor #3039

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: "spec_is_not_clear_what_id_this_is",
andybalaam marked this conversation as resolved.
Show resolved Hide resolved
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