Skip to content

Commit

Permalink
Move all post-join logic to onRoomJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Nov 23, 2022
1 parent 7db89e9 commit 21fa31c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -925,14 +904,40 @@ export class Bridge {
// Only act on bot joins
return;
}

const adminAccountData = await this.as.botIntent.underlyingClient.getSafeRoomAccountData<AdminAccountData>(
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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,21 @@ 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 {
const conn = await this.createConnectionForState(roomId, new StateEvent(event), rollbackBadState);
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)[] {
Expand Down

0 comments on commit 21fa31c

Please sign in to comment.