Skip to content

Commit

Permalink
Move the logic of roomPredecessor into the RoomState class (#3114)
Browse files Browse the repository at this point in the history
* Move the logic of roomPredecessor into the RoomState class

* Fix review comments
  • Loading branch information
andybalaam authored Feb 1, 2023
1 parent 6c6304a commit fbd2c97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
45 changes: 45 additions & 0 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,51 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
return guestAccessContent["guest_access"] || GuestAccess.Forbidden;
}

/**
* Find the predecessor room based on this room state.
*
* @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.
* 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 findPredecessor(
msc3946ProcessDynamicPredecessor = false,
): { roomId: string; eventId: string | null } | null {
// Note: the tests for this function are against Room.findPredecessor,
// which just calls through to here.

if (msc3946ProcessDynamicPredecessor) {
const predecessorEvent = this.getStateEvents(EventType.RoomPredecessor, "");
if (predecessorEvent) {
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
if (typeof roomId === "string") {
return { roomId, eventId: null };
}
}
}

const createEvent = this.getStateEvents(EventType.RoomCreate, "");
if (createEvent) {
const predecessor = createEvent.getContent()["predecessor"];
if (predecessor) {
const roomId = predecessor["room_id"];
if (typeof roomId === "string") {
let eventId = predecessor["event_id"];
if (typeof eventId !== "string" || eventId === "") {
eventId = null;
}
return { roomId, eventId };
}
}
}
return null;
}

private updateThirdPartyTokenCache(memberEvent: MatrixEvent): void {
if (!memberEvent.getContent().third_party_invite) {
return;
Expand Down
25 changes: 3 additions & 22 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
}

/**
* Find the predecessor of this room.
*
* @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
Expand All @@ -3041,28 +3043,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
if (!currentState) {
return null;
}
if (msc3946ProcessDynamicPredecessor) {
const predecessorEvent = currentState.getStateEvents(EventType.RoomPredecessor, "");
if (predecessorEvent) {
const roomId = predecessorEvent.getContent()["predecessor_room_id"];
if (roomId) {
return { roomId, eventId: null };
}
}
}

const createEvent = currentState.getStateEvents(EventType.RoomCreate, "");
if (createEvent) {
const predecessor = createEvent.getContent()["predecessor"];
if (predecessor) {
const roomId = predecessor["room_id"];
if (roomId) {
const eventId = predecessor["event_id"] || null;
return { roomId, eventId };
}
}
}
return null;
return currentState.findPredecessor(msc3946ProcessDynamicPredecessor);
}

private roomNameGenerator(state: RoomNameState): string {
Expand Down

0 comments on commit fbd2c97

Please sign in to comment.