2018-10-17 23:09:19 -04:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { BridgeComponent } from "../bridge.component";
|
|
|
|
import { TelegramApiService } from "../../../shared/services/integrations/telegram-api.service";
|
|
|
|
import { FE_PortalInfo } from "../../../shared/models/telegram";
|
2021-08-16 18:00:59 -04:00
|
|
|
import { TelegramAskUnbridgeComponent } from "./ask-unbridge/ask-unbridge.component";
|
2021-11-30 21:27:37 -05:00
|
|
|
import { TelegramCannotUnbridgeComponent } from "./cannot-unbridge/cannot-unbridge.component";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2021-08-16 18:00:59 -04:00
|
|
|
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
|
2018-10-17 23:09:19 -04:00
|
|
|
|
|
|
|
interface TelegramConfig {
|
|
|
|
puppet: {
|
|
|
|
advertise: boolean;
|
|
|
|
linked: boolean;
|
|
|
|
telegram: {
|
|
|
|
username: string;
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
|
|
|
phone: number;
|
|
|
|
isBot: boolean;
|
|
|
|
permissions: string[];
|
|
|
|
availableChats: {
|
|
|
|
id: number;
|
|
|
|
title: string;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
portalInfo: FE_PortalInfo;
|
|
|
|
botUsername: string;
|
|
|
|
linked: number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "telegram.bridge.component.html",
|
|
|
|
styleUrls: ["telegram.bridge.component.scss"],
|
|
|
|
})
|
|
|
|
export class TelegramBridgeConfigComponent extends BridgeComponent<TelegramConfig> {
|
|
|
|
|
|
|
|
public isUpdating: boolean;
|
|
|
|
|
2021-08-16 18:00:59 -04:00
|
|
|
constructor(private telegram: TelegramApiService, private modal: NgbModal, public translate: TranslateService) {
|
2020-10-23 07:30:20 -04:00
|
|
|
super("telegram", translate);
|
|
|
|
this.translate = translate;
|
2018-10-17 23:09:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public get isBridged(): boolean {
|
|
|
|
return this.bridge.config.linked.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get canUnbridge(): boolean {
|
|
|
|
return this.bridge.config.portalInfo ? this.bridge.config.portalInfo.canUnbridge : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get botUsername(): string {
|
|
|
|
return this.bridge.config.botUsername;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get chatName(): string {
|
|
|
|
return this.bridge.config.portalInfo ? this.bridge.config.portalInfo.chatName : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get chatId(): number {
|
|
|
|
return this.bridge.config.portalInfo ? this.bridge.config.portalInfo.chatId : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set chatId(n: number) {
|
|
|
|
if (!this.bridge.config.portalInfo) this.bridge.config.portalInfo = {
|
|
|
|
chatId: n,
|
|
|
|
chatName: null,
|
|
|
|
canUnbridge: false,
|
|
|
|
bridged: false,
|
|
|
|
roomId: this.roomId,
|
|
|
|
};
|
|
|
|
else this.bridge.config.portalInfo.chatId = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bridgeRoom(): void {
|
2018-10-17 23:37:29 -04:00
|
|
|
this.telegram.getPortalInfo(this.bridge.config.portalInfo.chatId, this.roomId).then(async (chatInfo) => {
|
|
|
|
let forceUnbridge = false;
|
2018-10-17 23:09:19 -04:00
|
|
|
if (chatInfo.bridged && chatInfo.canUnbridge) {
|
2021-08-16 18:00:59 -04:00
|
|
|
const askUnbridgeRef = this.modal.open(TelegramAskUnbridgeComponent, {
|
|
|
|
backdrop: 'static',
|
2018-10-17 23:37:29 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
|
|
|
askUnbridgeRef.result.then((response) => {
|
|
|
|
if (response.unbridge) {
|
|
|
|
forceUnbridge = true;
|
|
|
|
} else {
|
|
|
|
return {aborted: true};
|
|
|
|
}
|
|
|
|
});
|
2018-10-17 23:09:19 -04:00
|
|
|
} else if (chatInfo.bridged) {
|
2021-08-16 18:00:59 -04:00
|
|
|
this.modal.open(TelegramCannotUnbridgeComponent, {
|
|
|
|
backdrop: 'static',
|
2018-10-17 23:37:29 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
2018-10-17 23:37:29 -04:00
|
|
|
return {aborted: true};
|
2018-10-17 23:09:19 -04:00
|
|
|
}
|
2018-10-17 23:37:29 -04:00
|
|
|
|
|
|
|
return this.telegram.bridgeRoom(this.roomId, this.bridge.config.portalInfo.chatId, forceUnbridge);
|
2021-08-11 17:41:29 -04:00
|
|
|
}).then((portalInfo) => {
|
2018-10-17 23:37:29 -04:00
|
|
|
if ((<any>portalInfo).aborted) return;
|
2021-08-11 17:41:29 -04:00
|
|
|
const loadedPortalInfo = portalInfo as FE_PortalInfo
|
2018-10-17 23:37:29 -04:00
|
|
|
|
2021-08-11 17:41:29 -04:00
|
|
|
this.bridge.config.portalInfo = loadedPortalInfo;
|
|
|
|
this.bridge.config.linked = [loadedPortalInfo.chatId];
|
2018-10-17 23:09:19 -04:00
|
|
|
this.isUpdating = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Bridge updated').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("success", res);
|
|
|
|
});
|
2018-10-17 23:09:19 -04:00
|
|
|
}).catch(error => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
const body = error.json ? error.json() : null;
|
2020-10-23 07:30:20 -04:00
|
|
|
let message = 'Error bridging room';
|
2018-10-17 23:09:19 -04:00
|
|
|
if (body) {
|
2020-10-23 07:30:20 -04:00
|
|
|
if (body["dim_errcode"] === "CHAT_ALREADY_BRIDGED") message = 'That Telegram chat is already bridged to another room';
|
|
|
|
if (body["dim_errcode"] === "ROOM_ALREADY_BRIDGED") message = 'This room is already bridged to a Telegram chat';
|
|
|
|
if (body["dim_errcode"] === "BOT_NOT_IN_CHAT") message = 'The Telegram bot has not been invited to the chat';
|
|
|
|
if (body["dim_errcode"] === "NOT_ENOUGH_PERMISSIONS") message = 'You do not have permission to bridge that chat';
|
2021-09-01 19:01:01 -04:00
|
|
|
}
|
|
|
|
this.translate.get(message).subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-10-17 23:09:19 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public unbridgeRoom(): void {
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.telegram.unbridgeRoom(this.roomId).then(portalInfo => {
|
|
|
|
this.bridge.config.portalInfo = portalInfo;
|
|
|
|
this.bridge.config.linked = [];
|
|
|
|
this.isUpdating = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Bridge removed').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("success", res);
|
|
|
|
});
|
2018-10-17 23:09:19 -04:00
|
|
|
}).catch(error => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
console.error(error);
|
|
|
|
const body = error.json ? error.json() : null;
|
2020-10-23 07:30:20 -04:00
|
|
|
let message = 'Error removing bridge';
|
2018-10-17 23:09:19 -04:00
|
|
|
if (body) {
|
2020-10-23 07:30:20 -04:00
|
|
|
if (body["dim_errcode"] === "BOT_NOT_IN_CHAT") message = 'The Telegram bot has not been invited to the chat';
|
|
|
|
if (body["dim_errcode"] === "NOT_ENOUGH_PERMISSIONS") message = 'You do not have permission to unbridge that chat';
|
2018-10-17 23:09:19 -04:00
|
|
|
}
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get(message).subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-10-17 23:09:19 -04:00
|
|
|
});
|
|
|
|
}
|
2020-10-23 07:30:20 -04:00
|
|
|
}
|