Support the webhooks bridge in private rooms

This commit is contained in:
Travis Ralston 2018-10-20 18:57:54 -06:00
parent 3823788cc2
commit b8a01cc848
5 changed files with 40 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import * as request from "request";
import {
ListWebhooksResponse,
SuccessResponse,
WebhookBridgeInfo,
WebhookConfiguration,
WebhookOptions,
WebhookResponse
@ -27,12 +28,22 @@ export class WebhooksBridge {
return !!bridges;
}
public async getBridgeInfo(): Promise<WebhookBridgeInfo> {
const bridge = await this.getDefaultBridge();
return this.doProvisionRequest<WebhookBridgeInfo>(bridge, "GET", "/api/v1/provision/info");
}
public async getHooks(roomId: string): Promise<WebhookConfiguration[]> {
const bridge = await this.getDefaultBridge();
const response = await this.doProvisionRequest<ListWebhooksResponse>(bridge, "GET", `/api/v1/provision/${roomId}/hooks`);
if (!response.success) throw new Error("Failed to get webhooks");
return response.results;
try {
const response = await this.doProvisionRequest<ListWebhooksResponse>(bridge, "GET", `/api/v1/provision/${roomId}/hooks`);
if (!response.success) throw new Error("Failed to get webhooks");
return response.results;
} catch (e) {
LogService.error("WebhooksBridge", e);
return [];
}
}
public async createWebhook(roomId: string, options: WebhookOptions): Promise<WebhookConfiguration> {

View File

@ -20,4 +20,8 @@ export interface WebhookOptions {
export interface SuccessResponse {
success: boolean;
}
export interface WebhookBridgeInfo {
botUserId: string;
}

View File

@ -86,8 +86,10 @@ export class BridgeStore {
if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs
const webhooks = new WebhooksBridge(requestingUserId);
const hooks = await webhooks.getHooks(inRoomId);
const info = await webhooks.getBridgeInfo();
return <WebhookBridgeConfiguration>{
webhooks: hooks,
botUserId: info.botUserId,
};
} else return {};
}

View File

@ -8,7 +8,9 @@ export class Bridge extends Integration {
constructor(bridge: BridgeRecord, public config: any) {
super(bridge);
this.category = "bridge";
this.requirements = [{
if (bridge.type === "webhooks") this.requirements = [];
else this.requirements = [{
condition: "publicRoom",
expectedValue: true,
argument: null, // not used
@ -33,4 +35,5 @@ export interface TelegramBridgeConfiguration {
export interface WebhookBridgeConfiguration {
webhooks: WebhookConfiguration[];
botUserId: string;
}

View File

@ -2,9 +2,11 @@ import { Component } from "@angular/core";
import { BridgeComponent } from "../bridge.component";
import { FE_Webhook } from "../../../shared/models/webhooks";
import { WebhooksApiService } from "../../../shared/services/integrations/webhooks-api.service";
import { ScalarClientApiService } from "../../../shared/services/scalar/scalar-client-api.service";
interface WebhooksConfig {
webhooks: FE_Webhook[];
botUserId: string;
}
@Component({
@ -16,12 +18,24 @@ export class WebhooksBridgeConfigComponent extends BridgeComponent<WebhooksConfi
public webhookName: string;
public isBusy = false;
constructor(private webhooks: WebhooksApiService) {
constructor(private webhooks: WebhooksApiService, private scalar: ScalarClientApiService) {
super("webhooks");
}
public newHook() {
public async newHook() {
this.isBusy = true;
try {
await this.scalar.inviteUser(this.roomId, this.newConfig.botUserId);
} catch (e) {
if (!e.response || !e.response.error || !e.response.error._error ||
e.response.error._error.message.indexOf("already in the room") === -1) {
this.isBusy = false;
this.toaster.pop("error", "Error inviting bridge");
return;
}
}
this.webhooks.createWebhook(this.roomId, {label: this.webhookName}).then(hook => {
this.newConfig.webhooks.push(hook);
this.isBusy = false;