Skip to content

Commit

Permalink
Create widgets with service scope
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbot committed Nov 29, 2022
1 parent 5840b4c commit fd7bee2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,22 +829,23 @@ export class Bridge {
}
log.info(`Got invite roomId=${roomId} from=${event.sender} to=${invitedUserId}`);

if (!this.botUsersManager.isBotUser(invitedUserId)) {
const botUser = this.botUsersManager.getBotUser(invitedUserId);
if (!botUser) {
// We got an invite but it's not a configured bot user, must be for a ghost user
const client = this.as.getIntentForUserId(invitedUserId).underlyingClient;
return client.kickUser(invitedUserId, roomId, "Bridge does not support DMing ghosts");
}

const intent = this.as.getIntentForUserId(invitedUserId);
const intent = this.as.getIntentForUserId(botUser.userId);

// Don't accept invites from people who can't do anything
if (!this.config.checkPermissionAny(event.sender, BridgePermissionLevel.login)) {
return intent.underlyingClient.kickUser(invitedUserId, roomId, "You do not have permission to invite this bot.");
return intent.underlyingClient.kickUser(botUser.userId, roomId, "You do not have permission to invite this bot.");
}

if (event.content.is_direct && invitedUserId !== this.as.botUserId) {
if (event.content.is_direct && botUser.userId !== this.as.botUserId) {
// Service bots do not support direct messages (admin rooms)
return intent.underlyingClient.kickUser(invitedUserId, roomId, "This bot does not support admin rooms.");
return intent.underlyingClient.kickUser(botUser.userId, roomId, "This bot does not support admin rooms.");
}

// Accept the invite
Expand Down Expand Up @@ -880,7 +881,7 @@ export class Bridge {
await intent.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, intent, this.config.widgets);
await SetupWidget.SetupRoomConfigWidget(roomId, intent, this.config.widgets, botUser.services);
}
}
} catch (ex) {
Expand Down Expand Up @@ -960,6 +961,7 @@ export class Bridge {
const setupConnection = new SetupConnection(
roomId,
botUser.prefix,
botUser.services,
[
...botUser.services,
this.config.widgets?.roomSetupWidget ? "widget" : "",
Expand Down
3 changes: 2 additions & 1 deletion src/Connections/SetupConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class SetupConnection extends CommandConnection {
constructor(
readonly roomId: string,
readonly prefix: string,
readonly serviceTypes: string[],
readonly helpCategories: string[],
private readonly provisionOpts: ProvisionConnectionOpts,
private readonly getOrCreateAdminRoom: (intent: Intent, userId: string) => Promise<AdminRoom>,
Expand Down Expand Up @@ -321,7 +322,7 @@ export class SetupConnection extends CommandConnection {
if (!this.config.widgets?.roomSetupWidget) {
throw new CommandError("Not configured", "The bridge is not configured to support setup widgets");
}
if (!await SetupWidget.SetupRoomConfigWidget(this.roomId, this.intent, this.config.widgets)) {
if (!await SetupWidget.SetupRoomConfigWidget(this.roomId, this.intent, this.config.widgets, this.serviceTypes)) {
await this.client.sendNotice(this.roomId, `This room already has a setup widget, please open the "Hookshot Configuration" widget.`);
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Widgets/SetupWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ export class SetupWidget {
return false;
}

static async SetupRoomConfigWidget(roomId: string, botIntent: Intent, config: BridgeWidgetConfig): Promise<boolean> {
if (await SetupWidget.createWidgetInRoom(roomId, botIntent, config, HookshotWidgetKind.RoomConfiguration, "hookshot_room_config")) {
static async SetupRoomConfigWidget(roomId: string, botIntent: Intent, config: BridgeWidgetConfig, serviceTypes: string[]): Promise<boolean> {
// If this is for a single service, scope the widget
const serviceScope = serviceTypes.length === 1 ? serviceTypes[0] : undefined;
if (await SetupWidget.createWidgetInRoom(roomId, botIntent, config, HookshotWidgetKind.RoomConfiguration, "hookshot_room_config", serviceScope)) {
await botIntent.sendText(roomId, `Please open the ${config.branding.widgetTitle} widget to set up integrations.`);
return true;
}
return false;
}

private static async createWidgetInRoom(roomId: string, botIntent: Intent, config: BridgeWidgetConfig, kind: HookshotWidgetKind, stateKey: string): Promise<boolean> {
private static async createWidgetInRoom(
roomId: string,
botIntent: Intent,
config: BridgeWidgetConfig,
kind: HookshotWidgetKind,
stateKey: string,
serviceScope?: string,
): Promise<boolean> {
log.info(`Running SetupRoomConfigWidget for ${roomId}`);
if (!await botIntent.underlyingClient.userHasPowerLevelFor(botIntent.userId, roomId, "im.vector.modular.widgets", true)) {
throw new CommandError("Bot lacks power level to set room state", "I do not have permission to create a widget in this room. Please promote me to an Admin/Moderator.");
Expand Down Expand Up @@ -58,10 +67,10 @@ export class SetupWidget {
"id": stateKey,
"name": config.branding.widgetTitle,
"type": "m.custom",
"url": new URL(`#/?kind=${kind}&roomId=$matrix_room_id&widgetId=$matrix_widget_id`, config.parsedPublicUrl).href,
"url": new URL(`#/?kind=${kind}&roomId=$matrix_room_id&widgetId=$matrix_widget_id${serviceScope ? `&serviceScope=${serviceScope}` : ''}`, config.parsedPublicUrl).href,
"waitForIframeLoad": true,
}
);
return true;
}
}
}

0 comments on commit fd7bee2

Please sign in to comment.