Skip to content

Commit

Permalink
Provide eventId as well as roomId from Room.findPredecessor
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Jan 25, 2023
1 parent cb7f327 commit 652b152
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
15 changes: 9 additions & 6 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3282,19 +3282,19 @@ describe("Room", function () {

it("Returns null if there is no create event", () => {
const room = new Room("roomid", client!, "@u:example.com");
expect(room.findPredecessorRoomId()).toBeNull();
expect(room.findPredecessor()).toBeNull();
});

it("Returns null if the create event has no predecessor", () => {
const room = new Room("roomid", client!, "@u:example.com");
room.addLiveEvents([roomCreateEvent("roomid", null)]);
expect(room.findPredecessorRoomId()).toBeNull();
expect(room.findPredecessor()).toBeNull();
});

it("Returns the predecessor ID if one is provided via create event", () => {
const room = new Room("roomid", client!, "@u:example.com");
room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]);
expect(room.findPredecessorRoomId()).toBe("replacedroomid");
expect(room.findPredecessor()).toEqual({ roomId: "replacedroomid", eventId: "id_of_last_known_event" });
});

it("Prefers the m.predecessor event if one exists", () => {
Expand All @@ -3304,7 +3304,10 @@ describe("Room", function () {
predecessorEvent("roomid", "otherreplacedroomid"),
]);
const useMsc3946 = true;
expect(room.findPredecessorRoomId(useMsc3946)).toBe("otherreplacedroomid");
expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid",
eventId: null, // m.predecessor does not include an event_id
});
});

it("Ignores the m.predecessor event if we don't ask to use it", () => {
Expand All @@ -3315,7 +3318,7 @@ describe("Room", function () {
]);
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
// we should ignore the predecessor event.
expect(room.findPredecessorRoomId()).toBe("replacedroomid");
expect(room.findPredecessor()).toEqual({ roomId: "replacedroomid", eventId: "id_of_last_known_event" });
});

it("Ignores the m.predecessor event and returns null if we don't ask to use it", () => {
Expand All @@ -3326,7 +3329,7 @@ describe("Room", function () {
]);
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
// we should ignore the predecessor event.
expect(room.findPredecessorRoomId()).toBeNull();
expect(room.findPredecessor()).toBeNull();
});
});
});
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3791,7 +3791,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const replacedRooms = new Set();
for (const r of allRooms) {
const predecessor = r.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
const predecessor = r.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
if (predecessor) {
replacedRooms.add(predecessor);
}
Expand Down Expand Up @@ -5014,7 +5014,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
const ret: Room[] = [];

// Work backwards from newer to older rooms
let predecessorRoomId = room.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
let predecessorRoomId = room.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
while (predecessorRoomId !== null) {
const predecessorRoom = this.getRoom(predecessorRoomId);
if (predecessorRoom === null) {
Expand All @@ -5031,7 +5031,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
ret.splice(0, 0, predecessorRoom);

room = predecessorRoom;
predecessorRoomId = room.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
predecessorRoomId = room.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
}
return ret;
}
Expand All @@ -5047,7 +5047,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (successorRoom.roomId === room.roomId) break; // Tombstone is referencing its own room

if (verifyLinks) {
const predecessorRoomId = successorRoom.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
const predecessorRoomId = successorRoom.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
if (!predecessorRoomId || predecessorRoomId !== room.roomId) {
break;
}
Expand Down
22 changes: 14 additions & 8 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2992,12 +2992,17 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {

/**
* @param msc3946ProcessDynamicPredecessor - if true, look for an
* m.room.predecessor state event and
* use it if found (MSC3946).
* @returns the ID of the room that was this room's predecessor, or null if
* this room has no predecessor.
*/
public findPredecessorRoomId(msc3946ProcessDynamicPredecessor = false): string | null {
* m.room.predecessor state event and use it if found (MSC3946).
* @returns null if this room has no predecessor. Otherwise, returns
* the roomId and last eventId of the predecessor room.
* If msc3946ProcessDynamicPredecessor is true, use m.predecessor events
* as well as m.room.create events to find predecessors.
* Note: if an m.predecessor event is used, eventId is null since those
* events do not include an event_id property.
*/
public findPredecessor(
msc3946ProcessDynamicPredecessor = false,
): { roomId: string; eventId: string | null } | null {
const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
if (!currentState) {
return null;
Expand All @@ -3007,7 +3012,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
if (predecessorEvent) {
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
if (roomId) {
return roomId;
return { roomId, eventId: null };
}
}
}
Expand All @@ -3018,7 +3023,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
if (predecessor) {
const roomId = predecessor["room_id"];
if (roomId) {
return roomId;
const eventId = predecessor["event_id"] || null;
return { roomId, eventId };
}
}
}
Expand Down

0 comments on commit 652b152

Please sign in to comment.