Skip to content

Commit

Permalink
Manage admin rooms with bot-sdk DMs
Browse files Browse the repository at this point in the history
This also enables encryption for new admin rooms when appropriate.
  • Loading branch information
AndrewFerr committed Nov 22, 2022
1 parent b534ac0 commit 9853b8c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export class Bridge {
} as GitHubOAuthToken));

// Some users won't have an admin room and would have gone through provisioning.
const adminRoom = [...this.adminRooms.values()].find(r => r.userId === userId);
const adminRoom = this.getAdminRoomForUser(userId);
if (adminRoom) {
await adminRoom.sendNotice("Logged into GitHub");
}
Expand Down Expand Up @@ -540,7 +540,7 @@ export class Bridge {
});

// Some users won't have an admin room and would have gone through provisioning.
const adminRoom = [...this.adminRooms.values()].find(r => r.userId === userId);
const adminRoom = this.getAdminRoomForUser(userId);
if (adminRoom) {
await adminRoom.sendNotice("Logged into Jira");
}
Expand Down Expand Up @@ -873,7 +873,7 @@ export class Bridge {
github: this.github,
getAllConnectionsOfType: this.connectionManager.getAllConnectionsOfType.bind(this.connectionManager),
},
this.getOrCreateAdminRoom.bind(this),
this.getOrCreateAdminRoomForUser.bind(this),
this.connectionManager.push.bind(this.connectionManager),
)
).onMessageEvent(event, checkPermission);
Expand Down Expand Up @@ -1161,20 +1161,24 @@ export class Bridge {

}

private async getOrCreateAdminRoom(userId: string): Promise<AdminRoom> {
const existingRoom = [...this.adminRooms.values()].find(r => r.userId === userId);
private async getOrCreateAdminRoomForUser(userId: string): Promise<AdminRoom> {
const existingRoom = this.getAdminRoomForUser(userId);
if (existingRoom) {
return existingRoom;
}
// Otherwise, we need to create a room.
const roomId = await this.as.botClient.createRoom({
invite: [userId],
is_direct: true,
preset: "trusted_private_chat",
});
const roomId = await this.as.botClient.dms.getOrCreateDm(userId);
return this.setUpAdminRoom(roomId, {admin_user: userId}, NotifFilter.getDefaultContent());
}

private getAdminRoomForUser(userId: string): AdminRoom|null {
for (const adminRoom of this.adminRooms.values()) {
if (adminRoom.userId === userId) {
return adminRoom;
}
}
return null;
}

private async setUpAdminRoom(roomId: string, accountData: AdminAccountData, notifContent: NotificationFilterStateContent) {
if (!this.connectionManager) {
throw Error('setUpAdminRoom() called before connectionManager was ready');
Expand Down Expand Up @@ -1235,7 +1239,8 @@ export class Bridge {
} else {
return;
}
for (const adminRoom of [...this.adminRooms.values()].filter(r => r.userId === userId)) {
for (const adminRoom of this.adminRooms.values()) {
if (adminRoom.userId !== userId) continue;
if (adminRoom?.notificationsEnabled(type, instanceName)) {
log.debug(`Token was updated for ${userId} (${type}), notifying notification watcher`);
this.queue.push<NotificationsEnableEvent>({
Expand Down

0 comments on commit 9853b8c

Please sign in to comment.