Skip to content

Commit b2a9e6f

Browse files
authored
Handle optional last_known_event_id property in m.predecessor (#3119)
1 parent fbd2c97 commit b2a9e6f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

spec/unit/room.spec.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -3340,11 +3340,18 @@ describe("Room", function () {
33403340
});
33413341
}
33423342

3343-
function predecessorEvent(newRoomId: string, predecessorRoomId: string): MatrixEvent {
3343+
function predecessorEvent(
3344+
newRoomId: string,
3345+
predecessorRoomId: string,
3346+
tombstoneEventId: string | null = null,
3347+
): MatrixEvent {
3348+
const content =
3349+
tombstoneEventId === null
3350+
? { predecessor_room_id: predecessorRoomId }
3351+
: { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId };
3352+
33443353
return new MatrixEvent({
3345-
content: {
3346-
predecessor_room_id: predecessorRoomId,
3347-
},
3354+
content,
33483355
event_id: `predecessor_event_id_pred_${predecessorRoomId}`,
33493356
origin_server_ts: 1432735824653,
33503357
room_id: newRoomId,
@@ -3380,7 +3387,20 @@ describe("Room", function () {
33803387
const useMsc3946 = true;
33813388
expect(room.findPredecessor(useMsc3946)).toEqual({
33823389
roomId: "otherreplacedroomid",
3383-
eventId: null, // m.predecessor does not include an event_id
3390+
eventId: null, // m.predecessor did not include an event_id
3391+
});
3392+
});
3393+
3394+
it("uses the m.predecessor event ID if provided", () => {
3395+
const room = new Room("roomid", client!, "@u:example.com");
3396+
room.addLiveEvents([
3397+
roomCreateEvent("roomid", "replacedroomid"),
3398+
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid"),
3399+
]);
3400+
const useMsc3946 = true;
3401+
expect(room.findPredecessor(useMsc3946)).toEqual({
3402+
roomId: "otherreplacedroomid",
3403+
eventId: "lstevtid",
33843404
});
33853405
});
33863406

@@ -3399,7 +3419,7 @@ describe("Room", function () {
33993419
const room = new Room("roomid", client!, "@u:example.com");
34003420
room.addLiveEvents([
34013421
roomCreateEvent("roomid", null), // Create event has no predecessor
3402-
predecessorEvent("roomid", "otherreplacedroomid"),
3422+
predecessorEvent("roomid", "otherreplacedroomid", "lastevtid"),
34033423
]);
34043424
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
34053425
// we should ignore the predecessor event.

src/models/room-state.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,12 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
986986
const predecessorEvent = this.getStateEvents(EventType.RoomPredecessor, "");
987987
if (predecessorEvent) {
988988
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
989+
let eventId = predecessorEvent.getContent()["last_known_event_id"];
990+
if (typeof eventId !== "string") {
991+
eventId = null;
992+
}
989993
if (typeof roomId === "string") {
990-
return { roomId, eventId: null };
994+
return { roomId, eventId };
991995
}
992996
}
993997
}

0 commit comments

Comments
 (0)