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

Handle optional last_known_event_id property in m.predecessor #3119

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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