Skip to content

Commit 1060335

Browse files
committed
Factor out a (public) function to find a room's predecessor
1 parent 8a4c95e commit 1060335

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

spec/unit/room.spec.ts

+49
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
IRelationsRequestOpts,
3434
IStateEventWithRoomId,
3535
JoinRule,
36+
MatrixClient,
3637
MatrixEvent,
3738
MatrixEventEvent,
3839
PendingEventOrdering,
@@ -3225,4 +3226,52 @@ describe("Room", function () {
32253226
expect(room.getBlacklistUnverifiedDevices()).toBe(false);
32263227
});
32273228
});
3229+
3230+
describe("findPredecessorRoomId", () => {
3231+
function roomCreateEvent(newRoomId: string, predecessorRoomId: string | null): MatrixEvent {
3232+
const content: {
3233+
creator: string;
3234+
["m.federate"]: boolean;
3235+
room_version: string;
3236+
predecessor: { event_id: string; room_id: string } | undefined;
3237+
} = {
3238+
"creator": "@daryl:alexandria.example.com",
3239+
"predecessor": undefined,
3240+
"m.federate": true,
3241+
"room_version": "9",
3242+
};
3243+
if (predecessorRoomId) {
3244+
content.predecessor = {
3245+
event_id: "spec_is_not_clear_what_id_this_is",
3246+
room_id: predecessorRoomId,
3247+
};
3248+
}
3249+
return new MatrixEvent({
3250+
content,
3251+
event_id: `create_event_id_pred_${predecessorRoomId}`,
3252+
origin_server_ts: 1432735824653,
3253+
room_id: newRoomId,
3254+
sender: "@daryl:alexandria.example.com",
3255+
state_key: "",
3256+
type: "m.room.create",
3257+
});
3258+
}
3259+
3260+
it("Returns null if there is no create event", () => {
3261+
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
3262+
expect(room.findPredecessorRoomId()).toBeNull();
3263+
});
3264+
3265+
it("Returns null if the create event has no predecessor", () => {
3266+
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
3267+
room.addLiveEvents([roomCreateEvent("roomid", null)]);
3268+
expect(room.findPredecessorRoomId()).toBeNull();
3269+
});
3270+
3271+
it("Returns the predecessor ID if one is provided via create event", () => {
3272+
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
3273+
room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]);
3274+
expect(room.findPredecessorRoomId()).toBe("replacedroomid");
3275+
});
3276+
});
32283277
});

src/client.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -3770,13 +3770,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
37703770

37713771
const replacedRooms = new Set();
37723772
for (const r of allRooms) {
3773-
const createEvent = r.currentState.getStateEvents(EventType.RoomCreate, "");
3774-
// invites are included in this list and we don't know their create events yet
3775-
if (createEvent) {
3776-
const predecessor = createEvent.getContent()["predecessor"];
3777-
if (predecessor && predecessor["room_id"]) {
3778-
replacedRooms.add(predecessor["room_id"]);
3779-
}
3773+
const predecessor = r.findPredecessorRoomId();
3774+
if (predecessor) {
3775+
replacedRooms.add(predecessor);
37803776
}
37813777
}
37823778

src/models/room.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2969,6 +2969,24 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
29692969
return this.getType() === RoomType.ElementVideo;
29702970
}
29712971

2972+
/**
2973+
* @returns the ID of the room that was this room's predecessor, or null if
2974+
* this room has no predecessor.
2975+
*/
2976+
public findPredecessorRoomId(): string | null {
2977+
const createEvent = this.currentState.getStateEvents(EventType.RoomCreate, "");
2978+
if (createEvent) {
2979+
const predecessor = createEvent.getContent()["predecessor"];
2980+
if (predecessor) {
2981+
const roomId = predecessor["room_id"];
2982+
if (roomId) {
2983+
return roomId;
2984+
}
2985+
}
2986+
}
2987+
return null;
2988+
}
2989+
29722990
private roomNameGenerator(state: RoomNameState): string {
29732991
if (this.client.roomNameGenerator) {
29742992
const name = this.client.roomNameGenerator(this.roomId, state);

0 commit comments

Comments
 (0)