Skip to content

Commit 05d2393

Browse files
committed
Allow via_servers property in findPredecessor (update to MSC3946)
1 parent 5fc6b3e commit 05d2393

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

spec/unit/room.spec.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -3343,11 +3343,16 @@ describe("Room", function () {
33433343
newRoomId: string,
33443344
predecessorRoomId: string,
33453345
tombstoneEventId: string | null = null,
3346+
viaServers: string[] = [],
33463347
): MatrixEvent {
33473348
const content =
33483349
tombstoneEventId === null
3349-
? { predecessor_room_id: predecessorRoomId }
3350-
: { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId };
3350+
? { predecessor_room_id: predecessorRoomId, via_servers: viaServers }
3351+
: {
3352+
predecessor_room_id: predecessorRoomId,
3353+
last_known_event_id: tombstoneEventId,
3354+
via_servers: viaServers,
3355+
};
33513356

33523357
return new MatrixEvent({
33533358
content,
@@ -3387,19 +3392,21 @@ describe("Room", function () {
33873392
expect(room.findPredecessor(useMsc3946)).toEqual({
33883393
roomId: "otherreplacedroomid",
33893394
eventId: undefined, // m.predecessor did not include an event_id
3395+
viaServers: [],
33903396
});
33913397
});
33923398

33933399
it("uses the m.predecessor event ID if provided", () => {
33943400
const room = new Room("roomid", client!, "@u:example.com");
33953401
room.addLiveEvents([
33963402
roomCreateEvent("roomid", "replacedroomid"),
3397-
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid"),
3403+
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid", ["one.example.com", "two.example.com"]),
33983404
]);
33993405
const useMsc3946 = true;
34003406
expect(room.findPredecessor(useMsc3946)).toEqual({
34013407
roomId: "otherreplacedroomid",
34023408
eventId: "lstevtid",
3409+
viaServers: ["one.example.com", "two.example.com"],
34033410
});
34043411
});
34053412

src/models/room-state.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -972,13 +972,21 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
972972
* @param msc3946ProcessDynamicPredecessor - if true, look for an
973973
* m.room.predecessor state event and use it if found (MSC3946).
974974
* @returns null if this room has no predecessor. Otherwise, returns
975-
* the roomId and last eventId of the predecessor room.
975+
* the roomId, last eventId and viaServers of the predecessor room.
976+
*
976977
* If msc3946ProcessDynamicPredecessor is true, use m.predecessor events
977978
* as well as m.room.create events to find predecessors.
979+
*
978980
* Note: if an m.predecessor event is used, eventId may be undefined
979981
* since last_known_event_id is optional.
982+
*
983+
* Note: viaServers may be undefined, and will definitely be undefined if
984+
* this predecessor comes from a RoomCreate event (rather than a
985+
* RoomPredecessor, which has the optional via_servers property).
980986
*/
981-
public findPredecessor(msc3946ProcessDynamicPredecessor = false): { roomId: string; eventId?: string } | null {
987+
public findPredecessor(
988+
msc3946ProcessDynamicPredecessor = false,
989+
): { roomId: string; eventId?: string; viaServers?: string[] } | null {
982990
// Note: the tests for this function are against Room.findPredecessor,
983991
// which just calls through to here.
984992

@@ -988,14 +996,19 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
988996
const content = predecessorEvent.getContent<{
989997
predecessor_room_id: string;
990998
last_known_event_id?: string;
999+
via_servers?: string[];
9911000
}>();
9921001
const roomId = content.predecessor_room_id;
9931002
let eventId = content.last_known_event_id;
9941003
if (typeof eventId !== "string") {
9951004
eventId = undefined;
9961005
}
1006+
let viaServers = content.via_servers;
1007+
if (!Array.isArray(viaServers)) {
1008+
viaServers = undefined;
1009+
}
9971010
if (typeof roomId === "string") {
998-
return { roomId, eventId };
1011+
return { roomId, eventId, viaServers };
9991012
}
10001013
}
10011014
}

src/models/room.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -3057,13 +3057,21 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
30573057
* @param msc3946ProcessDynamicPredecessor - if true, look for an
30583058
* m.room.predecessor state event and use it if found (MSC3946).
30593059
* @returns null if this room has no predecessor. Otherwise, returns
3060-
* the roomId and last eventId of the predecessor room.
3060+
* the roomId, last eventId and viaServers of the predecessor room.
3061+
*
30613062
* If msc3946ProcessDynamicPredecessor is true, use m.predecessor events
30623063
* as well as m.room.create events to find predecessors.
3064+
*
30633065
* Note: if an m.predecessor event is used, eventId may be undefined
30643066
* since last_known_event_id is optional.
3067+
*
3068+
* Note: viaServers may be undefined, and will definitely be undefined if
3069+
* this predecessor comes from a RoomCreate event (rather than a
3070+
* RoomPredecessor, which has the optional via_servers property).
30653071
*/
3066-
public findPredecessor(msc3946ProcessDynamicPredecessor = false): { roomId: string; eventId?: string } | null {
3072+
public findPredecessor(
3073+
msc3946ProcessDynamicPredecessor = false,
3074+
): { roomId: string; eventId?: string; viaServers?: string[] } | null {
30673075
const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
30683076
if (!currentState) {
30693077
return null;

0 commit comments

Comments
 (0)