mirror of
https://github.com/turt2live/matrix-dimension.git
synced 2024-10-01 01:05:53 -04:00
Support the webhooks bridge in private rooms
This commit is contained in:
parent
3823788cc2
commit
b8a01cc848
@ -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> {
|
||||
|
@ -20,4 +20,8 @@ export interface WebhookOptions {
|
||||
|
||||
export interface SuccessResponse {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface WebhookBridgeInfo {
|
||||
botUserId: string;
|
||||
}
|
@ -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 {};
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user