diff --git a/src/Bridge.ts b/src/Bridge.ts index 5e6ff81b1..bca441bd0 100644 --- a/src/Bridge.ts +++ b/src/Bridge.ts @@ -776,30 +776,9 @@ export class Bridge { await retry(() => this.as.botIntent.joinRoom(roomId), 5); if (event.content.is_direct) { - const room = await this.setUpAdminRoom(roomId, {admin_user: event.sender}, NotifFilter.getDefaultContent()); await this.as.botClient.setRoomAccountData( - BRIDGE_ROOM_TYPE, roomId, room.accountData, + BRIDGE_ROOM_TYPE, roomId, {admin_user: event.sender}, ); - return; - } - - if (this.connectionManager?.isRoomConnected(roomId)) { - // Room has connections, don't set up a wizard. - return; - } - - try { - // Otherwise it's a new room - if (this.config.widgets?.roomSetupWidget?.addOnInvite) { - if (await this.as.botClient.userHasPowerLevelFor(this.as.botUserId, roomId, "im.vector.modular.widgets", true) === false) { - await this.as.botIntent.sendText(roomId, "Hello! To set up new integrations in this room, please promote me to a Moderator/Admin."); - } else { - // Set up the widget - await SetupWidget.SetupRoomConfigWidget(roomId, this.as.botIntent, this.config.widgets); - } - } - } catch (ex) { - log.error(`Failed to set up new widget for room`, ex); } } @@ -925,14 +904,40 @@ export class Bridge { // Only act on bot joins return; } + + const adminAccountData = await this.as.botIntent.underlyingClient.getSafeRoomAccountData( + BRIDGE_ROOM_TYPE, roomId, + ); + if (!!adminAccountData) { + const room = await this.setUpAdminRoom(roomId, adminAccountData, NotifFilter.getDefaultContent()); + await this.as.botClient.setRoomAccountData( + BRIDGE_ROOM_TYPE, roomId, room.accountData, + ); + } + if (!this.connectionManager) { // Not ready yet. return; } // Only fetch rooms we have no connections in yet. - if (!this.connectionManager.isRoomConnected(roomId)) { + const roomHasConnection = + this.connectionManager.isRoomConnected(roomId) || await this.connectionManager.createConnectionsForRoomId(roomId, true); + + // If room has connections or is an admin room, don't setup a wizard. + // Otherwise it's a new room + if (!roomHasConnection && !adminAccountData && this.config.widgets?.roomSetupWidget?.addOnInvite) { + try { + if (await this.as.botClient.userHasPowerLevelFor(this.as.botUserId, roomId, "im.vector.modular.widgets", true) === false) { + await this.as.botIntent.sendText(roomId, "Hello! To setup new integrations in this room, please promote me to a Moderator/Admin"); + } else { + // Setup the widget + await SetupWidget.SetupRoomConfigWidget(roomId, this.as.botIntent, this.config.widgets); + } + } catch (ex) { + log.error(`Failed to setup new widget for room`, ex); + } } } diff --git a/src/ConnectionManager.ts b/src/ConnectionManager.ts index a2570fe6f..bdf7b183a 100644 --- a/src/ConnectionManager.ts +++ b/src/ConnectionManager.ts @@ -171,6 +171,7 @@ export class ConnectionManager extends EventEmitter { } public async createConnectionsForRoomId(roomId: string, rollbackBadState: boolean) { + let connectionCreated = false; const state = await this.as.botClient.getRoomState(roomId); for (const event of state) { try { @@ -178,11 +179,13 @@ export class ConnectionManager extends EventEmitter { if (conn) { log.debug(`Room ${roomId} is connected to: ${conn}`); this.push(conn); + connectionCreated = true; } } catch (ex) { log.error(`Failed to create connection for ${roomId}:`, ex); } } + return connectionCreated; } public getConnectionsForGithubIssue(org: string, repo: string, issueNumber: number): (GitHubIssueConnection|GitHubRepoConnection)[] {