1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Allow via_servers property in findPredecessor (update to MSC3946) (#3240)

This commit is contained in:
Andy Balaam
2023-03-31 13:51:10 +01:00
committed by GitHub
parent 5fc6b3ed17
commit d1cf98b177
3 changed files with 36 additions and 8 deletions

View File

@ -3343,11 +3343,16 @@ describe("Room", function () {
newRoomId: string, newRoomId: string,
predecessorRoomId: string, predecessorRoomId: string,
tombstoneEventId: string | null = null, tombstoneEventId: string | null = null,
viaServers: string[] = [],
): MatrixEvent { ): MatrixEvent {
const content = const content =
tombstoneEventId === null tombstoneEventId === null
? { predecessor_room_id: predecessorRoomId } ? { predecessor_room_id: predecessorRoomId, via_servers: viaServers }
: { predecessor_room_id: predecessorRoomId, last_known_event_id: tombstoneEventId }; : {
predecessor_room_id: predecessorRoomId,
last_known_event_id: tombstoneEventId,
via_servers: viaServers,
};
return new MatrixEvent({ return new MatrixEvent({
content, content,
@ -3387,6 +3392,7 @@ describe("Room", function () {
expect(room.findPredecessor(useMsc3946)).toEqual({ expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid", roomId: "otherreplacedroomid",
eventId: undefined, // m.predecessor did not include an event_id eventId: undefined, // m.predecessor did not include an event_id
viaServers: [],
}); });
}); });
@ -3394,12 +3400,13 @@ describe("Room", function () {
const room = new Room("roomid", client!, "@u:example.com"); const room = new Room("roomid", client!, "@u:example.com");
room.addLiveEvents([ room.addLiveEvents([
roomCreateEvent("roomid", "replacedroomid"), roomCreateEvent("roomid", "replacedroomid"),
predecessorEvent("roomid", "otherreplacedroomid", "lstevtid"), predecessorEvent("roomid", "otherreplacedroomid", "lstevtid", ["one.example.com", "two.example.com"]),
]); ]);
const useMsc3946 = true; const useMsc3946 = true;
expect(room.findPredecessor(useMsc3946)).toEqual({ expect(room.findPredecessor(useMsc3946)).toEqual({
roomId: "otherreplacedroomid", roomId: "otherreplacedroomid",
eventId: "lstevtid", eventId: "lstevtid",
viaServers: ["one.example.com", "two.example.com"],
}); });
}); });

View File

@ -972,13 +972,21 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
* @param msc3946ProcessDynamicPredecessor - if true, look for an * @param msc3946ProcessDynamicPredecessor - if true, look for an
* m.room.predecessor state event and use it if found (MSC3946). * m.room.predecessor state event and use it if found (MSC3946).
* @returns null if this room has no predecessor. Otherwise, returns * @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 * If msc3946ProcessDynamicPredecessor is true, use m.predecessor events
* as well as m.room.create events to find predecessors. * as well as m.room.create events to find predecessors.
*
* Note: if an m.predecessor event is used, eventId may be undefined * Note: if an m.predecessor event is used, eventId may be undefined
* since last_known_event_id is optional. * 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, // Note: the tests for this function are against Room.findPredecessor,
// which just calls through to here. // which just calls through to here.
@ -988,14 +996,19 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
const content = predecessorEvent.getContent<{ const content = predecessorEvent.getContent<{
predecessor_room_id: string; predecessor_room_id: string;
last_known_event_id?: string; last_known_event_id?: string;
via_servers?: string[];
}>(); }>();
const roomId = content.predecessor_room_id; const roomId = content.predecessor_room_id;
let eventId = content.last_known_event_id; let eventId = content.last_known_event_id;
if (typeof eventId !== "string") { if (typeof eventId !== "string") {
eventId = undefined; eventId = undefined;
} }
let viaServers = content.via_servers;
if (!Array.isArray(viaServers)) {
viaServers = undefined;
}
if (typeof roomId === "string") { if (typeof roomId === "string") {
return { roomId, eventId }; return { roomId, eventId, viaServers };
} }
} }
} }

View File

@ -3057,13 +3057,21 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
* @param msc3946ProcessDynamicPredecessor - if true, look for an * @param msc3946ProcessDynamicPredecessor - if true, look for an
* m.room.predecessor state event and use it if found (MSC3946). * m.room.predecessor state event and use it if found (MSC3946).
* @returns null if this room has no predecessor. Otherwise, returns * @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 * If msc3946ProcessDynamicPredecessor is true, use m.predecessor events
* as well as m.room.create events to find predecessors. * as well as m.room.create events to find predecessors.
*
* Note: if an m.predecessor event is used, eventId may be undefined * Note: if an m.predecessor event is used, eventId may be undefined
* since last_known_event_id is optional. * 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); const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
if (!currentState) { if (!currentState) {
return null; return null;