You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Provide eventId as well as roomId from Room.findPredecessor (#3095)
This commit is contained in:
@@ -3356,19 +3356,19 @@ describe("Room", function () {
|
|||||||
|
|
||||||
it("Returns null if there is no create event", () => {
|
it("Returns null if there is no create event", () => {
|
||||||
const room = new Room("roomid", client!, "@u:example.com");
|
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", () => {
|
it("Returns null if the create event has no predecessor", () => {
|
||||||
const room = new Room("roomid", client!, "@u:example.com");
|
const room = new Room("roomid", client!, "@u:example.com");
|
||||||
room.addLiveEvents([roomCreateEvent("roomid", null)]);
|
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", () => {
|
it("Returns the predecessor ID if one is provided via create event", () => {
|
||||||
const room = new Room("roomid", client!, "@u:example.com");
|
const room = new Room("roomid", client!, "@u:example.com");
|
||||||
room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]);
|
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", () => {
|
it("Prefers the m.predecessor event if one exists", () => {
|
||||||
@@ -3378,7 +3378,10 @@ describe("Room", function () {
|
|||||||
predecessorEvent("roomid", "otherreplacedroomid"),
|
predecessorEvent("roomid", "otherreplacedroomid"),
|
||||||
]);
|
]);
|
||||||
const useMsc3946 = true;
|
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", () => {
|
it("Ignores the m.predecessor event if we don't ask to use it", () => {
|
||||||
@@ -3389,7 +3392,7 @@ describe("Room", function () {
|
|||||||
]);
|
]);
|
||||||
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
|
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
|
||||||
// we should ignore the predecessor event.
|
// 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", () => {
|
it("Ignores the m.predecessor event and returns null if we don't ask to use it", () => {
|
||||||
@@ -3400,7 +3403,7 @@ describe("Room", function () {
|
|||||||
]);
|
]);
|
||||||
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
|
// Don't provide an argument for msc3946ProcessDynamicPredecessor -
|
||||||
// we should ignore the predecessor event.
|
// we should ignore the predecessor event.
|
||||||
expect(room.findPredecessorRoomId()).toBeNull();
|
expect(room.findPredecessor()).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3791,7 +3791,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
|
|
||||||
const replacedRooms = new Set();
|
const replacedRooms = new Set();
|
||||||
for (const r of allRooms) {
|
for (const r of allRooms) {
|
||||||
const predecessor = r.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
|
const predecessor = r.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
|
||||||
if (predecessor) {
|
if (predecessor) {
|
||||||
replacedRooms.add(predecessor);
|
replacedRooms.add(predecessor);
|
||||||
}
|
}
|
||||||
@@ -5014,7 +5014,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
const ret: Room[] = [];
|
const ret: Room[] = [];
|
||||||
|
|
||||||
// Work backwards from newer to older rooms
|
// Work backwards from newer to older rooms
|
||||||
let predecessorRoomId = room.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
|
let predecessorRoomId = room.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
|
||||||
while (predecessorRoomId !== null) {
|
while (predecessorRoomId !== null) {
|
||||||
const predecessorRoom = this.getRoom(predecessorRoomId);
|
const predecessorRoom = this.getRoom(predecessorRoomId);
|
||||||
if (predecessorRoom === null) {
|
if (predecessorRoom === null) {
|
||||||
@@ -5031,7 +5031,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
ret.splice(0, 0, predecessorRoom);
|
ret.splice(0, 0, predecessorRoom);
|
||||||
|
|
||||||
room = predecessorRoom;
|
room = predecessorRoom;
|
||||||
predecessorRoomId = room.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
|
predecessorRoomId = room.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -5047,7 +5047,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
if (successorRoom.roomId === room.roomId) break; // Tombstone is referencing its own room
|
if (successorRoom.roomId === room.roomId) break; // Tombstone is referencing its own room
|
||||||
|
|
||||||
if (verifyLinks) {
|
if (verifyLinks) {
|
||||||
const predecessorRoomId = successorRoom.findPredecessorRoomId(msc3946ProcessDynamicPredecessor);
|
const predecessorRoomId = successorRoom.findPredecessor(msc3946ProcessDynamicPredecessor)?.roomId;
|
||||||
if (!predecessorRoomId || predecessorRoomId !== room.roomId) {
|
if (!predecessorRoomId || predecessorRoomId !== room.roomId) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3032,12 +3032,17 @@ 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
|
* m.room.predecessor state event and use it if found (MSC3946).
|
||||||
* use it if found (MSC3946).
|
* @returns null if this room has no predecessor. Otherwise, returns
|
||||||
* @returns the ID of the room that was this room's predecessor, or null if
|
* the roomId and last eventId of the predecessor room.
|
||||||
* this room has no predecessor.
|
* 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 findPredecessorRoomId(msc3946ProcessDynamicPredecessor = false): string | null {
|
public findPredecessor(
|
||||||
|
msc3946ProcessDynamicPredecessor = false,
|
||||||
|
): { roomId: string; eventId: string | null } | null {
|
||||||
const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
|
const currentState = this.getLiveTimeline().getState(EventTimeline.FORWARDS);
|
||||||
if (!currentState) {
|
if (!currentState) {
|
||||||
return null;
|
return null;
|
||||||
@@ -3047,7 +3052,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
|||||||
if (predecessorEvent) {
|
if (predecessorEvent) {
|
||||||
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
|
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
|
||||||
if (roomId) {
|
if (roomId) {
|
||||||
return roomId;
|
return { roomId, eventId: null };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3058,7 +3063,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
|||||||
if (predecessor) {
|
if (predecessor) {
|
||||||
const roomId = predecessor["room_id"];
|
const roomId = predecessor["room_id"];
|
||||||
if (roomId) {
|
if (roomId) {
|
||||||
return roomId;
|
const eventId = predecessor["event_id"] || null;
|
||||||
|
return { roomId, eventId };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user