Skip to content

Commit

Permalink
Allow via_servers property in findPredecessor (update to MSC3946)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Mar 31, 2023
1 parent 5fc6b3e commit 05d2393
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
13 changes: 10 additions & 3 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3343,11 +3343,16 @@ describe("Room", function () {
newRoomId: string,
predecessorRoomId: string,
tombstoneEventId: string | null = null,
viaServers: string[] = [],
): MatrixEvent {
const content =
tombstoneEventId === null
? { predecessor_room_id: predecessorRoomId }
: { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId };
? { predecessor_room_id: predecessorRoomId, via_servers: viaServers }
: {
predecessor_room_id: predecessorRoomId,
last_known_event_id: tombstoneEventId,
via_servers: viaServers,
};

return new MatrixEvent({
content,
Expand Down Expand Up @@ -3387,19 +3392,21 @@ describe("Room", function () {
expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid",
eventId: undefined, // m.predecessor did not include an event_id
viaServers: [],
});
});

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"),
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid", ["one.example.com", "two.example.com"]),
]);
const useMsc3946 = true;
expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid",
eventId: "lstevtid",
viaServers: ["one.example.com", "two.example.com"],
});
});

Expand Down
19 changes: 16 additions & 3 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,21 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
* @param msc3946ProcessDynamicPredecessor - if true, look for an
* 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.
* the roomId, last eventId and viaServers 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 may be undefined
* since last_known_event_id is optional.
*
* Note: viaServers may be undefined, and will definitely be undefined if
* this predecessor comes from a RoomCreate event (rather than a
* RoomPredecessor, which has the optional via_servers property).
*/
public findPredecessor(msc3946ProcessDynamicPredecessor = false): { roomId: string; eventId?: string } | null {
public findPredecessor(
msc3946ProcessDynamicPredecessor = false,
): { roomId: string; eventId?: string; viaServers?: string[] } | null {
// Note: the tests for this function are against Room.findPredecessor,
// which just calls through to here.

Expand All @@ -988,14 +996,19 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
const content = predecessorEvent.getContent<{
predecessor_room_id: string;
last_known_event_id?: string;
via_servers?: string[];
}>();
const roomId = content.predecessor_room_id;
let eventId = content.last_known_event_id;
if (typeof eventId !== "string") {
eventId = undefined;
}
let viaServers = content.via_servers;
if (!Array.isArray(viaServers)) {
viaServers = undefined;
}
if (typeof roomId === "string") {
return { roomId, eventId };
return { roomId, eventId, viaServers };
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3057,13 +3057,21 @@ 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 null if this room has no predecessor. Otherwise, returns
* the roomId and last eventId of the predecessor room.
* the roomId, last eventId and viaServers 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 may be undefined
* since last_known_event_id is optional.
*
* Note: viaServers may be undefined, and will definitely be undefined if
* this predecessor comes from a RoomCreate event (rather than a
* RoomPredecessor, which has the optional via_servers property).
*/
public findPredecessor(msc3946ProcessDynamicPredecessor = false): { roomId: string; eventId?: string } | null {
public findPredecessor(
msc3946ProcessDynamicPredecessor = false,
): { roomId: string; eventId?: string; viaServers?: string[] } | null {
const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
if (!currentState) {
return null;
Expand Down

0 comments on commit 05d2393

Please sign in to comment.