Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the logic of roomPredecessor into the RoomState class #3114

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are also missing type checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, fixed in 558392c

}
}
}
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 @@ -3028,6 +3028,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 @@ -3044,28 +3046,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