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

Reduce our reliance on an up-to-date joined rooms cache #16

Merged
merged 6 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/appservice/Appservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,15 @@ export class Appservice extends EventEmitter {
const botDomain = new UserID(this.botUserId).domain;
if (domain !== botDomain) return; // can't be impersonated, so don't try

// Update the target intent's joined rooms (fixes transition errors with the cache, like join->kick->join)
const intent = this.getIntentForUserId(event['state_key']);
await intent.refreshJoinedRooms();

const targetMembership = event["content"]["membership"];
if (targetMembership === "join") {
intent.onRoomJoin(event["room_id"]);
this.emit("room.join", event["room_id"], event);
await intent.underlyingClient.crypto?.onRoomJoin(event["room_id"]);
} else if (targetMembership === "ban" || targetMembership === "leave") {
intent.onRoomLeave(event["room_id"]);
this.emit("room.leave", event["room_id"], event);
} else if (targetMembership === "invite") {
this.emit("room.invite", event["room_id"], event);
Expand Down
42 changes: 22 additions & 20 deletions src/appservice/Intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export class Intent {

private client: MatrixClient;
private unstableApisInstance: UnstableAppserviceApis;
private knownJoinedRooms: string[] = [];
/**
* If initialized, contains a set of rooms we know that we're joined to.
*/
private knownJoinedRooms: Set<string>|undefined;
private cryptoSetupPromise: Promise<void>;

/**
Expand Down Expand Up @@ -172,7 +175,6 @@ export class Intent {
await this.client.crypto.prepare(await this.refreshJoinedRooms());

this.appservice.on("room.event", (roomId, event) => {
if (!this.knownJoinedRooms.includes(roomId)) return;
this.client.crypto.onRoomEvent(roomId, event);
});

Expand All @@ -186,16 +188,14 @@ export class Intent {
}

/**
* Gets the joined rooms for the intent. Note that by working around
* the intent to join rooms may yield inaccurate results.
* Gets the joined rooms for the intent.
* @returns {Promise<string[]>} Resolves to an array of room IDs where
* the intent is joined.
*/
@timedIntentFunctionCall()
public async getJoinedRooms(): Promise<string[]> {
await this.ensureRegistered();
tadzik marked this conversation as resolved.
Show resolved Hide resolved
if (this.knownJoinedRooms.length === 0) await this.refreshJoinedRooms();
return this.knownJoinedRooms.map(r => r); // clone
return await this.refreshJoinedRooms();
}

/**
Expand All @@ -208,8 +208,7 @@ export class Intent {
public async leaveRoom(roomId: string, reason?: string): Promise<any> {
await this.ensureRegistered();
return this.client.leaveRoom(roomId, reason).then(async () => {
// Recalculate joined rooms now that we've left a room
await this.refreshJoinedRooms();
this.knownJoinedRooms?.delete(roomId);
});
}

Expand All @@ -222,8 +221,7 @@ export class Intent {
public async joinRoom(roomIdOrAlias: string): Promise<string> {
await this.ensureRegistered();
return this.client.joinRoom(roomIdOrAlias).then(async roomId => {
// Recalculate joined rooms now that we've joined a room
await this.refreshJoinedRooms();
this.knownJoinedRooms?.add(roomId);
return roomId;
});
}
Expand Down Expand Up @@ -270,20 +268,16 @@ export class Intent {
*/
@timedIntentFunctionCall()
public async ensureJoined(roomId: string) {
tadzik marked this conversation as resolved.
Show resolved Hide resolved
if (this.knownJoinedRooms.indexOf(roomId) !== -1) {
return;
if (!this.knownJoinedRooms) {
await this.refreshJoinedRooms();
}

await this.refreshJoinedRooms();

if (this.knownJoinedRooms.indexOf(roomId) !== -1) {
if (this.knownJoinedRooms!.has(roomId)) {
AndrewFerr marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const returnedRoomId = await this.client.joinRoom(roomId);
if (!this.knownJoinedRooms.includes(returnedRoomId)) {
this.knownJoinedRooms.push(returnedRoomId);
}
this.knownJoinedRooms!.add(returnedRoomId);
return returnedRoomId;
}

Expand All @@ -294,8 +288,16 @@ export class Intent {
*/
@timedIntentFunctionCall()
public async refreshJoinedRooms(): Promise<string[]> {
this.knownJoinedRooms = await this.client.getJoinedRooms();
return this.knownJoinedRooms.map(r => r); // clone
this.knownJoinedRooms = new Set(await this.client.getJoinedRooms());
return Array.from(this.knownJoinedRooms); // clone
}

onRoomJoin(roomId: string) {
this.knownJoinedRooms?.add(roomId);
}

onRoomLeave(roomId: string) {
this.knownJoinedRooms?.delete(roomId);
}

/**
Expand Down