You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-04 05:02:41 +03:00
Factor out a (public) function to find a room's predecessor
This commit is contained in:
@@ -33,6 +33,7 @@ import {
|
|||||||
IRelationsRequestOpts,
|
IRelationsRequestOpts,
|
||||||
IStateEventWithRoomId,
|
IStateEventWithRoomId,
|
||||||
JoinRule,
|
JoinRule,
|
||||||
|
MatrixClient,
|
||||||
MatrixEvent,
|
MatrixEvent,
|
||||||
MatrixEventEvent,
|
MatrixEventEvent,
|
||||||
PendingEventOrdering,
|
PendingEventOrdering,
|
||||||
@@ -3225,4 +3226,52 @@ describe("Room", function () {
|
|||||||
expect(room.getBlacklistUnverifiedDevices()).toBe(false);
|
expect(room.getBlacklistUnverifiedDevices()).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("findPredecessorRoomId", () => {
|
||||||
|
function roomCreateEvent(newRoomId: string, predecessorRoomId: string | null): MatrixEvent {
|
||||||
|
const content: {
|
||||||
|
creator: string;
|
||||||
|
["m.federate"]: boolean;
|
||||||
|
room_version: string;
|
||||||
|
predecessor: { event_id: string; room_id: string } | undefined;
|
||||||
|
} = {
|
||||||
|
"creator": "@daryl:alexandria.example.com",
|
||||||
|
"predecessor": undefined,
|
||||||
|
"m.federate": true,
|
||||||
|
"room_version": "9",
|
||||||
|
};
|
||||||
|
if (predecessorRoomId) {
|
||||||
|
content.predecessor = {
|
||||||
|
event_id: "spec_is_not_clear_what_id_this_is",
|
||||||
|
room_id: predecessorRoomId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return new MatrixEvent({
|
||||||
|
content,
|
||||||
|
event_id: `create_event_id_pred_${predecessorRoomId}`,
|
||||||
|
origin_server_ts: 1432735824653,
|
||||||
|
room_id: newRoomId,
|
||||||
|
sender: "@daryl:alexandria.example.com",
|
||||||
|
state_key: "",
|
||||||
|
type: "m.room.create",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it("Returns null if there is no create event", () => {
|
||||||
|
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
|
||||||
|
expect(room.findPredecessorRoomId()).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Returns null if the create event has no predecessor", () => {
|
||||||
|
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
|
||||||
|
room.addLiveEvents([roomCreateEvent("roomid", null)]);
|
||||||
|
expect(room.findPredecessorRoomId()).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Returns the predecessor ID if one is provided via create event", () => {
|
||||||
|
const room = new Room("roomid", null as unknown as MatrixClient, "@u:example.com");
|
||||||
|
room.addLiveEvents([roomCreateEvent("roomid", "replacedroomid")]);
|
||||||
|
expect(room.findPredecessorRoomId()).toBe("replacedroomid");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3770,13 +3770,9 @@ 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 createEvent = r.currentState.getStateEvents(EventType.RoomCreate, "");
|
const predecessor = r.findPredecessorRoomId();
|
||||||
// invites are included in this list and we don't know their create events yet
|
if (predecessor) {
|
||||||
if (createEvent) {
|
replacedRooms.add(predecessor);
|
||||||
const predecessor = createEvent.getContent()["predecessor"];
|
|
||||||
if (predecessor && predecessor["room_id"]) {
|
|
||||||
replacedRooms.add(predecessor["room_id"]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2969,6 +2969,24 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
|||||||
return this.getType() === RoomType.ElementVideo;
|
return this.getType() === RoomType.ElementVideo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns the ID of the room that was this room's predecessor, or null if
|
||||||
|
* this room has no predecessor.
|
||||||
|
*/
|
||||||
|
public findPredecessorRoomId(): string | null {
|
||||||
|
const createEvent = this.currentState.getStateEvents(EventType.RoomCreate, "");
|
||||||
|
if (createEvent) {
|
||||||
|
const predecessor = createEvent.getContent()["predecessor"];
|
||||||
|
if (predecessor) {
|
||||||
|
const roomId = predecessor["room_id"];
|
||||||
|
if (roomId) {
|
||||||
|
return roomId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private roomNameGenerator(state: RoomNameState): string {
|
private roomNameGenerator(state: RoomNameState): string {
|
||||||
if (this.client.roomNameGenerator) {
|
if (this.client.roomNameGenerator) {
|
||||||
const name = this.client.roomNameGenerator(this.roomId, state);
|
const name = this.client.roomNameGenerator(this.roomId, state);
|
||||||
|
|||||||
Reference in New Issue
Block a user