2018-10-20 20:33:01 -04:00
|
|
|
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";
|
2018-10-20 20:57:54 -04:00
|
|
|
import { ScalarClientApiService } from "../../../shared/services/scalar/scalar-client-api.service";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2018-10-20 20:33:01 -04:00
|
|
|
|
|
|
|
interface WebhooksConfig {
|
|
|
|
webhooks: FE_Webhook[];
|
2018-10-20 20:57:54 -04:00
|
|
|
botUserId: string;
|
2018-10-20 20:33:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "webhooks.bridge.component.html",
|
|
|
|
styleUrls: ["webhooks.bridge.component.scss"],
|
|
|
|
})
|
|
|
|
export class WebhooksBridgeConfigComponent extends BridgeComponent<WebhooksConfig> {
|
|
|
|
|
|
|
|
public webhookName: string;
|
|
|
|
public isBusy = false;
|
|
|
|
|
2020-10-23 07:30:20 -04:00
|
|
|
constructor(private webhooks: WebhooksApiService, private scalar: ScalarClientApiService, public translate: TranslateService) {
|
|
|
|
super("webhooks", translate);
|
2018-10-20 20:33:01 -04:00
|
|
|
}
|
|
|
|
|
2018-10-20 20:57:54 -04:00
|
|
|
public async newHook() {
|
2018-10-20 20:33:01 -04:00
|
|
|
this.isBusy = true;
|
2018-10-20 20:57:54 -04:00
|
|
|
|
|
|
|
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;
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Error inviting bridge').subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-10-20 20:57:54 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 20:33:01 -04:00
|
|
|
this.webhooks.createWebhook(this.roomId, {label: this.webhookName}).then(hook => {
|
|
|
|
this.newConfig.webhooks.push(hook);
|
|
|
|
this.isBusy = false;
|
|
|
|
this.webhookName = "";
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Webhook created').subscribe((res: string) => {this.toaster.pop("success", res); });
|
2018-10-20 20:33:01 -04:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isBusy = false;
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Error creating webhook').subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-10-20 20:33:01 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeHook(hook: FE_Webhook) {
|
|
|
|
this.isBusy = true;
|
|
|
|
this.webhooks.deleteWebhook(this.roomId, hook.id).then(() => {
|
|
|
|
const idx = this.newConfig.webhooks.indexOf(hook);
|
|
|
|
if (idx !== -1) this.newConfig.webhooks.splice(idx, 1);
|
|
|
|
this.isBusy = false;
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Webhook deleted').subscribe((res: string) => {this.toaster.pop("success", res); });
|
2018-10-20 20:33:01 -04:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isBusy = false;
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Error deleting webhook').subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-10-20 20:33:01 -04:00
|
|
|
});
|
|
|
|
}
|
2020-10-23 07:30:20 -04:00
|
|
|
}
|