Skip to content

Commit

Permalink
Handle optional last_known_event_id property in m.predecessor (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Feb 1, 2023
1 parent fbd2c97 commit b2a9e6f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
32 changes: 26 additions & 6 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3340,11 +3340,18 @@ describe("Room", function () {
});
}

function predecessorEvent(newRoomId: string, predecessorRoomId: string): MatrixEvent {
function predecessorEvent(
newRoomId: string,
predecessorRoomId: string,
tombstoneEventId: string | null = null,
): MatrixEvent {
const content =
tombstoneEventId === null
? { predecessor_room_id: predecessorRoomId }
: { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId };

return new MatrixEvent({
content: {
predecessor_room_id: predecessorRoomId,
},
content,
event_id: `predecessor_event_id_pred_${predecessorRoomId}`,
origin_server_ts: 1432735824653,
room_id: newRoomId,
Expand Down Expand Up @@ -3380,7 +3387,20 @@ describe("Room", function () {
const useMsc3946 = true;
expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid",
eventId: null, // m.predecessor does not include an event_id
eventId: null, // m.predecessor did not include an event_id
});
});

it("uses the m.predecessor event ID if provided", () => {
const room = new Room("roomid", client!, "@u:example.com");
room.addLiveEvents([
roomCreateEvent("roomid", "replacedroomid"),
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid"),
]);
const useMsc3946 = true;
expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid",
eventId: "lstevtid",
});
});

Expand All @@ -3399,7 +3419,7 @@ describe("Room", function () {
const room = new Room("roomid", client!, "@u:example.com");
room.addLiveEvents([
roomCreateEvent("roomid", null), // Create event has no predecessor
predecessorEvent("roomid", "otherreplacedroomid"),
predecessorEvent("roomid", "otherreplacedroomid", "lastevtid"),
]);
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
// we should ignore the predecessor event.
Expand Down
6 changes: 5 additions & 1 deletion src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,12 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
const predecessorEvent = this.getStateEvents(EventType.RoomPredecessor, "");
if (predecessorEvent) {
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
let eventId = predecessorEvent.getContent()["last_known_event_id"];
if (typeof eventId !== "string") {
eventId = null;
}
if (typeof roomId === "string") {
return { roomId, eventId: null };
return { roomId, eventId };
}
}
}
Expand Down

0 comments on commit b2a9e6f

Please sign in to comment.