mirror of
https://github.com/turt2live/matrix-dimension.git
synced 2024-08-31 06:02:09 +00:00
Frontend interface for basic bridging and unbridging of chats
Requires https://github.com/tulir/mautrix-telegram/pull/235 Puppeting is scheduled to be handled in https://github.com/turt2live/matrix-dimension/issues/214 The TODO comments about asking for unbridge/permission denied will be handled in a later commit.
This commit is contained in:
parent
242ad3bf3a
commit
c0936745c0
@ -35,5 +35,7 @@ export class ApiError {
|
||||
this.jsonResponse = json;
|
||||
this.statusCode = statusCode;
|
||||
this.errorCode = errCode;
|
||||
|
||||
this.jsonResponse["dim_errcode"] = this.errorCode;
|
||||
}
|
||||
}
|
@ -12,14 +12,16 @@ interface CreateWithUpstream {
|
||||
interface CreateSelfhosted {
|
||||
provisionUrl: string;
|
||||
sharedSecret: string;
|
||||
allowPuppets: boolean;
|
||||
allowTgPuppets: boolean;
|
||||
allowMxPuppets: boolean;
|
||||
}
|
||||
|
||||
interface BridgeResponse {
|
||||
id: number;
|
||||
upstreamId?: number;
|
||||
provisionUrl?: string;
|
||||
allowPuppets?: boolean;
|
||||
allowTgPuppets?: boolean;
|
||||
allowMxPuppets?: boolean;
|
||||
sharedSecret?: string;
|
||||
isEnabled: boolean;
|
||||
}
|
||||
@ -41,7 +43,8 @@ export class AdminTelegramService {
|
||||
id: b.id,
|
||||
upstreamId: b.upstreamId,
|
||||
provisionUrl: b.provisionUrl,
|
||||
allowPuppets: b.allowPuppets,
|
||||
allowTgPuppets: b.allowTgPuppets,
|
||||
allowMxPuppets: b.allowMxPuppets,
|
||||
sharedSecret: b.sharedSecret,
|
||||
isEnabled: b.isEnabled,
|
||||
};
|
||||
@ -60,7 +63,8 @@ export class AdminTelegramService {
|
||||
id: telegramBridge.id,
|
||||
upstreamId: telegramBridge.upstreamId,
|
||||
provisionUrl: telegramBridge.provisionUrl,
|
||||
allowPuppets: telegramBridge.allowPuppets,
|
||||
allowTgPuppets: telegramBridge.allowTgPuppets,
|
||||
allowMxPuppets: telegramBridge.allowMxPuppets,
|
||||
sharedSecret: telegramBridge.sharedSecret,
|
||||
isEnabled: telegramBridge.isEnabled,
|
||||
};
|
||||
@ -76,7 +80,8 @@ export class AdminTelegramService {
|
||||
|
||||
bridge.provisionUrl = request.provisionUrl;
|
||||
bridge.sharedSecret = request.sharedSecret;
|
||||
bridge.allowPuppets = request.allowPuppets;
|
||||
bridge.allowTgPuppets = request.allowTgPuppets;
|
||||
bridge.allowMxPuppets = request.allowMxPuppets;
|
||||
await bridge.save();
|
||||
|
||||
LogService.info("AdminTelegramService", userId + " updated Telegram Bridge " + bridge.id);
|
||||
@ -100,7 +105,8 @@ export class AdminTelegramService {
|
||||
const bridge = await TelegramBridgeRecord.create({
|
||||
provisionUrl: request.provisionUrl,
|
||||
sharedSecret: request.sharedSecret,
|
||||
allowPuppets: request.allowPuppets,
|
||||
allowTgPuppets: request.allowTgPuppets,
|
||||
allowMxPuppets: request.allowMxPuppets,
|
||||
isEnabled: true,
|
||||
});
|
||||
LogService.info("AdminTelegramService", userId + " created a new Telegram Bridge with provisioning URL " + request.provisionUrl);
|
||||
|
93
src/api/dimension/DimensionTelegramService.ts
Normal file
93
src/api/dimension/DimensionTelegramService.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import { DELETE, GET, Path, PathParam, POST, QueryParam } from "typescript-rest";
|
||||
import { ScalarService } from "../scalar/ScalarService";
|
||||
import { TelegramBridge } from "../../bridges/TelegramBridge";
|
||||
import { ApiError } from "../ApiError";
|
||||
|
||||
interface PortalInfoResponse {
|
||||
bridged: boolean;
|
||||
chatId: number;
|
||||
roomId: string;
|
||||
canUnbridge: boolean;
|
||||
chatName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* API for interacting with the Telegram bridge
|
||||
*/
|
||||
@Path("/api/v1/dimension/telegram")
|
||||
export class DimensionTelegramService {
|
||||
|
||||
@GET
|
||||
@Path("chat/:chatId")
|
||||
public async getPortalInfo(@QueryParam("scalar_token") scalarToken: string, @PathParam("chatId") chatId: number, @QueryParam("roomId") roomId: string): Promise<PortalInfoResponse> {
|
||||
const userId = await ScalarService.getTokenOwner(scalarToken);
|
||||
|
||||
try {
|
||||
const telegram = new TelegramBridge(userId);
|
||||
const conf = await telegram.getChatConfiguration(chatId, roomId);
|
||||
if (!conf) return {
|
||||
bridged: false,
|
||||
canUnbridge: true,
|
||||
chatId: chatId,
|
||||
roomId: null,
|
||||
chatName: null,
|
||||
};
|
||||
|
||||
return {
|
||||
bridged: true,
|
||||
canUnbridge: conf.canUnbridge,
|
||||
chatId: chatId,
|
||||
roomId: conf.roomId,
|
||||
chatName: conf.chatName,
|
||||
};
|
||||
} catch (e) {
|
||||
if (e.errcode) {
|
||||
throw new ApiError(400, "Error bridging room", e.errcode.toUpperCase());
|
||||
} else throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("chat/:chatId/room/:roomId")
|
||||
public async bridgeRoom(@QueryParam("scalar_token") scalarToken: string, @PathParam("chatId") chatId: number, @PathParam("roomId") roomId: string): Promise<PortalInfoResponse> {
|
||||
const userId = await ScalarService.getTokenOwner(scalarToken);
|
||||
|
||||
try {
|
||||
const telegram = new TelegramBridge(userId);
|
||||
const portal = await telegram.bridgeRoom(chatId, roomId);
|
||||
return {
|
||||
bridged: true,
|
||||
canUnbridge: portal.canUnbridge,
|
||||
chatId: portal.chatId,
|
||||
roomId: portal.roomId,
|
||||
chatName: portal.chatName,
|
||||
};
|
||||
} catch (e) {
|
||||
if (e.errcode) {
|
||||
throw new ApiError(400, "Error bridging room", e.errcode.toUpperCase());
|
||||
} else throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("room/:roomId")
|
||||
public async unbridgeRoom(@QueryParam("scalar_token") scalarToken: string, @PathParam("roomId") roomId: string): Promise<PortalInfoResponse> {
|
||||
const userId = await ScalarService.getTokenOwner(scalarToken);
|
||||
|
||||
try {
|
||||
const telegram = new TelegramBridge(userId);
|
||||
const portal = await telegram.unbridgeRoom(roomId);
|
||||
return {
|
||||
bridged: false,
|
||||
canUnbridge: portal.canUnbridge,
|
||||
chatId: portal.chatId,
|
||||
roomId: portal.roomId,
|
||||
chatName: portal.chatName,
|
||||
};
|
||||
} catch (e) {
|
||||
if (e.errcode) {
|
||||
throw new ApiError(400, "Error bridging room", e.errcode.toUpperCase());
|
||||
} else throw e;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +1,189 @@
|
||||
import { TelegramBridgeConfiguration } from "../integrations/Bridge";
|
||||
import TelegramBridgeRecord from "../db/models/TelegramBridgeRecord";
|
||||
import { LogService } from "matrix-js-snippets";
|
||||
import * as request from "request";
|
||||
import { PortalInformationResponse } from "./models/telegram";
|
||||
import {
|
||||
BridgeInfoResponse,
|
||||
PortalInformationResponse,
|
||||
UserChatResponse,
|
||||
UserInformationResponse
|
||||
} from "./models/telegram";
|
||||
|
||||
export interface PortalInfo {
|
||||
bridged: boolean;
|
||||
chatId: number;
|
||||
roomId: string;
|
||||
canUnbridge: boolean;
|
||||
chatName: string;
|
||||
}
|
||||
|
||||
export interface PuppetInfo {
|
||||
advertiseTgPuppets: boolean;
|
||||
advertiseMxPuppets: boolean;
|
||||
linked: boolean;
|
||||
telegram: {
|
||||
username: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
phone: number;
|
||||
isBot: boolean;
|
||||
permissions: string[];
|
||||
availableChats: {
|
||||
id: number;
|
||||
title: string;
|
||||
}[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface BridgeInfo {
|
||||
botUsername: string;
|
||||
}
|
||||
|
||||
export class TelegramBridge {
|
||||
constructor(private requestingUserId: string) {
|
||||
}
|
||||
|
||||
private async getDefaultBridge(): Promise<TelegramBridgeRecord> {
|
||||
const bridges = await TelegramBridgeRecord.findAll({where: {isEnabled: true}});
|
||||
if (!bridges || bridges.length !== 1) {
|
||||
throw new Error("No bridges or too many bridges found");
|
||||
}
|
||||
|
||||
return bridges[0];
|
||||
}
|
||||
|
||||
public async isBridgingEnabled(): Promise<boolean> {
|
||||
const bridges = await TelegramBridgeRecord.findAll({where: {isEnabled: true}});
|
||||
return !!bridges;
|
||||
}
|
||||
|
||||
public async getRoomConfiguration(inRoomId: string): Promise<TelegramBridgeConfiguration> {
|
||||
const bridges = await TelegramBridgeRecord.findAll({where: {isEnabled: true}});
|
||||
public async getBridgeInfo(): Promise<BridgeInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
const linkedChats: number[] = [];
|
||||
for (const bridge of bridges) {
|
||||
try {
|
||||
const chatInfo = await this.doProvisionRequest<PortalInformationResponse>(bridge, "GET", `/portal/${inRoomId}`);
|
||||
linkedChats.push(chatInfo.chat_id);
|
||||
} catch (e) {
|
||||
if (!e.errBody || e.errBody["errcode"] !== "portal_not_found") {
|
||||
throw e.error || e;
|
||||
}
|
||||
}
|
||||
const info = await this.doProvisionRequest<BridgeInfoResponse>(bridge, "GET", `/bridge`);
|
||||
return {
|
||||
botUsername: info.relaybot_username,
|
||||
};
|
||||
}
|
||||
|
||||
public async getPuppetInfo(): Promise<PuppetInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
const info = await this.doProvisionRequest<UserInformationResponse>(bridge, "GET", `/user/${this.requestingUserId}`);
|
||||
const puppet: PuppetInfo = {
|
||||
advertiseTgPuppets: bridge.allowTgPuppets,
|
||||
advertiseMxPuppets: bridge.allowMxPuppets,
|
||||
linked: !!info.telegram,
|
||||
telegram: {
|
||||
permissions: [info.permissions],
|
||||
username: info.telegram ? info.telegram.username : null,
|
||||
firstName: info.telegram ? info.telegram.first_name : null,
|
||||
lastName: info.telegram ? info.telegram.last_name : null,
|
||||
phone: info.telegram ? info.telegram.phone : null,
|
||||
isBot: info.telegram ? info.telegram.is_bot : null,
|
||||
availableChats: [], // populated next
|
||||
},
|
||||
};
|
||||
|
||||
if (puppet.linked) {
|
||||
puppet.telegram.availableChats = await this.doProvisionRequest<UserChatResponse[]>(bridge, "GET", `/user/${this.requestingUserId}/chats`);
|
||||
}
|
||||
|
||||
return {linkedChatIds: linkedChats};
|
||||
return puppet;
|
||||
}
|
||||
|
||||
public async getRoomConfiguration(inRoomId: string): Promise<PortalInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
try {
|
||||
const info = await this.doProvisionRequest<PortalInformationResponse>(bridge, "GET", `/portal/${inRoomId}`);
|
||||
return {
|
||||
bridged: !!info,
|
||||
chatId: info ? info.chat_id : 0,
|
||||
roomId: inRoomId,
|
||||
chatName: info ? info.title || info.username : null,
|
||||
canUnbridge: info ? info.can_unbridge : false,
|
||||
};
|
||||
} catch (e) {
|
||||
if (!e.errBody || e.errBody["errcode"] !== "portal_not_found") {
|
||||
throw e.error || e;
|
||||
}
|
||||
|
||||
return {
|
||||
bridged: false,
|
||||
chatId: 0,
|
||||
roomId: "",
|
||||
chatName: null,
|
||||
canUnbridge: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async getChatConfiguration(chatId: number, roomId: string): Promise<PortalInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
try {
|
||||
const info = await this.doProvisionRequest<PortalInformationResponse>(bridge, "GET", `/portal/${chatId}`, {room_id: roomId});
|
||||
return {
|
||||
bridged: !!info,
|
||||
chatId: chatId,
|
||||
roomId: info ? info.mxid : null,
|
||||
chatName: info ? info.title || info.username : null,
|
||||
canUnbridge: info ? info.can_unbridge : false,
|
||||
};
|
||||
} catch (e) {
|
||||
if (!e.errBody || e.errBody["errcode"] !== "portal_not_found") {
|
||||
throw e.error || e;
|
||||
}
|
||||
|
||||
const rethrowCodes = ["bot_not_in_chat"];
|
||||
if (rethrowCodes.indexOf(e.errBody["errcode"]) !== -1) {
|
||||
throw {errcode: e.errBody["errcode"]};
|
||||
}
|
||||
|
||||
return {
|
||||
bridged: false,
|
||||
chatId: 0,
|
||||
roomId: null,
|
||||
chatName: null,
|
||||
canUnbridge: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async bridgeRoom(chatId: number, roomId: string): Promise<PortalInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
try {
|
||||
await this.doProvisionRequest(bridge, "POST", `/portal/${roomId}/connect/${chatId}`);
|
||||
return this.getChatConfiguration(chatId, roomId);
|
||||
} catch (e) {
|
||||
if (!e.errBody) throw e.error || e;
|
||||
|
||||
const rethrowCodes = ["not_enough_permissions", "room_already_bridged", "chat_already_bridged", "bot_not_in_chat"];
|
||||
if (rethrowCodes.indexOf(e.errBody["errcode"]) !== -1) {
|
||||
throw {errcode: e.errBody["errcode"]};
|
||||
}
|
||||
|
||||
throw e.error || e;
|
||||
}
|
||||
}
|
||||
|
||||
public async unbridgeRoom(roomId: string): Promise<PortalInfo> {
|
||||
const bridge = await this.getDefaultBridge();
|
||||
|
||||
try {
|
||||
await this.doProvisionRequest(bridge, "POST", `/portal/${roomId}/disconnect`);
|
||||
return this.getRoomConfiguration(roomId);
|
||||
} catch (e) {
|
||||
if (!e.errBody) throw e.error || e;
|
||||
|
||||
const rethrowCodes = ["not_enough_permissions", "bot_not_in_chat"];
|
||||
if (rethrowCodes.indexOf(e.errBody["errcode"]) !== -1) {
|
||||
throw {errcode: e.errBody["errcode"]};
|
||||
}
|
||||
|
||||
throw e.error || e;
|
||||
}
|
||||
}
|
||||
|
||||
private async doProvisionRequest<T>(bridge: TelegramBridgeRecord, method: string, endpoint: string, qs?: any, body?: any): Promise<T> {
|
||||
@ -38,7 +193,9 @@ export class TelegramBridge {
|
||||
LogService.info("TelegramBridge", "Doing provision Telegram Bridge request: " + url);
|
||||
|
||||
if (!qs) qs = {};
|
||||
if (!qs["user_id"]) qs["user_id"] = this.requestingUserId;
|
||||
|
||||
if (qs["user_id"] === false) delete qs["user_id"];
|
||||
else if (!qs["user_id"]) qs["user_id"] = this.requestingUserId;
|
||||
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
request({
|
||||
@ -46,6 +203,9 @@ export class TelegramBridge {
|
||||
url: url,
|
||||
qs: qs,
|
||||
json: body,
|
||||
headers: {
|
||||
"Authorization": `Bearer ${bridge.sharedSecret}`,
|
||||
},
|
||||
}, (err, res, _body) => {
|
||||
if (err) {
|
||||
LogService.error("TelegramBridge", "Error calling" + url);
|
||||
@ -54,7 +214,7 @@ export class TelegramBridge {
|
||||
} else if (!res) {
|
||||
LogService.error("TelegramBridge", "There is no response for " + url);
|
||||
reject(new Error("No response provided - is the service online?"));
|
||||
} else if (res.statusCode !== 200) {
|
||||
} else if (res.statusCode !== 200 && res.statusCode !== 202) {
|
||||
LogService.error("TelegramBridge", "Got status code " + res.statusCode + " when calling " + url);
|
||||
LogService.error("TelegramBridge", res.body);
|
||||
if (typeof(res.body) === "string") res.body = JSON.parse(res.body);
|
||||
|
@ -6,4 +6,27 @@ export interface PortalInformationResponse {
|
||||
username: string;
|
||||
title: string;
|
||||
about: string;
|
||||
can_unbridge: boolean;
|
||||
}
|
||||
|
||||
export interface UserInformationResponse {
|
||||
mxid: string;
|
||||
permissions: string;
|
||||
telegram?: {
|
||||
id: number;
|
||||
username: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
phone: number;
|
||||
is_bot: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface UserChatResponse {
|
||||
id: number;
|
||||
title: string;
|
||||
}
|
||||
|
||||
export interface BridgeInfoResponse {
|
||||
relaybot_username: string;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { Bridge } from "../integrations/Bridge";
|
||||
import { Bridge, TelegramBridgeConfiguration } from "../integrations/Bridge";
|
||||
import BridgeRecord from "./models/BridgeRecord";
|
||||
import { IrcBridge } from "../bridges/IrcBridge";
|
||||
import { LogService } from "matrix-js-snippets";
|
||||
@ -68,7 +68,14 @@ export class BridgeStore {
|
||||
} else if (record.type === "telegram") {
|
||||
if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs
|
||||
const telegram = new TelegramBridge(requestingUserId);
|
||||
return telegram.getRoomConfiguration(inRoomId);
|
||||
const roomConf = await telegram.getRoomConfiguration(inRoomId);
|
||||
const bridgeInfo = await telegram.getBridgeInfo();
|
||||
return <TelegramBridgeConfiguration>{
|
||||
botUsername: bridgeInfo.botUsername,
|
||||
linked: roomConf.bridged ? [roomConf.chatId] : [],
|
||||
portalInfo: roomConf,
|
||||
puppet: await telegram.getPuppetInfo(),
|
||||
};
|
||||
} else return {};
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
import { QueryInterface } from "sequelize";
|
||||
import { DataType } from "sequelize-typescript";
|
||||
|
||||
export default {
|
||||
up: (queryInterface: QueryInterface) => {
|
||||
return Promise.resolve()
|
||||
.then(() => queryInterface.removeColumn("dimension_telegram_bridges", "allowPuppets"))
|
||||
.then(() => queryInterface.addColumn("dimension_telegram_bridges", "allowTgPuppets", DataType.BOOLEAN))
|
||||
.then(() => queryInterface.addColumn("dimension_telegram_bridges", "allowMxPuppets", DataType.BOOLEAN));
|
||||
},
|
||||
down: (queryInterface: QueryInterface) => {
|
||||
return Promise.resolve()
|
||||
.then(() => queryInterface.removeColumn("dimension_telegram_bridges", "allowTgPuppets"))
|
||||
.then(() => queryInterface.removeColumn("dimension_telegram_bridges", "allowMxPuppets"))
|
||||
.then(() => queryInterface.addColumn("dimension_telegram_bridges", "allowPuppets", DataType.BOOLEAN));
|
||||
}
|
||||
}
|
@ -27,7 +27,11 @@ export default class TelegramBridgeRecord extends Model<TelegramBridgeRecord> {
|
||||
|
||||
@AllowNull
|
||||
@Column
|
||||
allowPuppets?: boolean;
|
||||
allowTgPuppets?: boolean;
|
||||
|
||||
@AllowNull
|
||||
@Column
|
||||
allowMxPuppets?: boolean;
|
||||
|
||||
@Column
|
||||
isEnabled: boolean;
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Integration } from "./Integration";
|
||||
import BridgeRecord from "../db/models/BridgeRecord";
|
||||
import { AvailableNetworks, LinkedChannels } from "../bridges/IrcBridge";
|
||||
import { PortalInfo, PuppetInfo } from "../bridges/TelegramBridge";
|
||||
|
||||
export class Bridge extends Integration {
|
||||
constructor(bridge: BridgeRecord, public config: any) {
|
||||
@ -23,5 +24,8 @@ export interface IrcBridgeConfiguration {
|
||||
}
|
||||
|
||||
export interface TelegramBridgeConfiguration {
|
||||
linkedChatIds: number[];
|
||||
botUsername: string;
|
||||
linked: number[];
|
||||
portalInfo: PortalInfo;
|
||||
puppet: PuppetInfo;
|
||||
}
|
@ -22,10 +22,17 @@
|
||||
</label>
|
||||
|
||||
<label class="label-block">
|
||||
Promote Puppeting
|
||||
Promote Telegram Puppeting
|
||||
<span class="text-muted ">If enabled, Dimension will recommend that users log in to their Telegram accounts.</span>
|
||||
<ui-switch [checked]="allowPuppets" size="small" [disabled]="isSaving"
|
||||
(change)="allowPuppets = !allowPuppets"></ui-switch>
|
||||
<ui-switch [checked]="allowTgPuppets" size="small" [disabled]="isSaving"
|
||||
(change)="allowTgPuppets = !allowTgPuppets"></ui-switch>
|
||||
</label>
|
||||
|
||||
<label class="label-block">
|
||||
Promote Matrix Puppeting
|
||||
<span class="text-muted ">If enabled, Dimension will recommend that users log in to their Matrix accounts.</span>
|
||||
<ui-switch [checked]="allowMxPuppets" size="small" [disabled]="isSaving"
|
||||
(change)="allowMxPuppets = !allowMxPuppets"></ui-switch>
|
||||
</label>
|
||||
</div>
|
||||
<div class="dialog-footer">
|
||||
|
@ -7,7 +7,8 @@ import { AdminTelegramApiService } from "../../../../shared/services/admin/admin
|
||||
export class ManageSelfhostedTelegramBridgeDialogContext extends BSModalContext {
|
||||
public provisionUrl: string;
|
||||
public sharedSecret: string;
|
||||
public allowPuppets = false;
|
||||
public allowTgPuppets = false;
|
||||
public allowMxPuppets = false;
|
||||
public bridgeId: number;
|
||||
}
|
||||
|
||||
@ -20,7 +21,8 @@ export class AdminTelegramBridgeManageSelfhostedComponent implements ModalCompon
|
||||
public isSaving = false;
|
||||
public provisionUrl: string;
|
||||
public sharedSecret: string;
|
||||
public allowPuppets = false;
|
||||
public allowTgPuppets = false;
|
||||
public allowMxPuppets = false;
|
||||
public bridgeId: number;
|
||||
public isAdding = false;
|
||||
|
||||
@ -29,15 +31,20 @@ export class AdminTelegramBridgeManageSelfhostedComponent implements ModalCompon
|
||||
private toaster: ToasterService) {
|
||||
this.provisionUrl = dialog.context.provisionUrl;
|
||||
this.sharedSecret = dialog.context.sharedSecret;
|
||||
this.allowPuppets = dialog.context.allowPuppets;
|
||||
this.allowTgPuppets = dialog.context.allowTgPuppets;
|
||||
this.allowMxPuppets = dialog.context.allowMxPuppets;
|
||||
this.bridgeId = dialog.context.bridgeId;
|
||||
this.isAdding = !this.bridgeId;
|
||||
}
|
||||
|
||||
public add() {
|
||||
this.isSaving = true;
|
||||
const options = {
|
||||
allowTgPuppets: this.allowTgPuppets,
|
||||
allowMxPuppets: this.allowMxPuppets,
|
||||
};
|
||||
if (this.isAdding) {
|
||||
this.telegramApi.newSelfhosted(this.provisionUrl, this.sharedSecret, this.allowPuppets).then(() => {
|
||||
this.telegramApi.newSelfhosted(this.provisionUrl, this.sharedSecret, options).then(() => {
|
||||
this.toaster.pop("success", "Telegram bridge added");
|
||||
this.dialog.close();
|
||||
}).catch(err => {
|
||||
@ -46,7 +53,7 @@ export class AdminTelegramBridgeManageSelfhostedComponent implements ModalCompon
|
||||
this.toaster.pop("error", "Failed to create Telegram bridge");
|
||||
});
|
||||
} else {
|
||||
this.telegramApi.updateSelfhosted(this.bridgeId, this.provisionUrl, this.sharedSecret, this.allowPuppets).then(() => {
|
||||
this.telegramApi.updateSelfhosted(this.bridgeId, this.provisionUrl, this.sharedSecret, options).then(() => {
|
||||
this.toaster.pop("success", "Telegram bridge updated");
|
||||
this.dialog.close();
|
||||
}).catch(err => {
|
||||
|
@ -53,7 +53,9 @@ export class AdminTelegramBridgeComponent implements OnInit {
|
||||
}
|
||||
|
||||
public getEnabledFeaturesString(bridge: FE_TelegramBridge): string {
|
||||
if (bridge.allowPuppets) return "Puppeting";
|
||||
if (!bridge.options) return "";
|
||||
if (bridge.options.allowTgPuppets) return "Telegram Puppetting";
|
||||
if (bridge.options.allowMxPuppets) return "Matrix Puppetting";
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -64,7 +66,8 @@ export class AdminTelegramBridgeComponent implements OnInit {
|
||||
|
||||
provisionUrl: bridge.provisionUrl,
|
||||
sharedSecret: bridge.sharedSecret,
|
||||
allowPuppets: bridge.allowPuppets,
|
||||
allowTgPuppets: bridge.options ? bridge.options.allowTgPuppets : false,
|
||||
allowMxPuppets: bridge.options ? bridge.options.allowMxPuppets : false,
|
||||
bridgeId: bridge.id,
|
||||
}, ManageSelfhostedTelegramBridgeDialogContext)).result.then(() => {
|
||||
this.reload().catch(err => {
|
||||
|
@ -81,6 +81,8 @@ import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-p
|
||||
import { AdminTelegramApiService } from "./shared/services/admin/admin-telegram-api.service";
|
||||
import { AdminTelegramBridgeComponent } from "./admin/bridges/telegram/telegram.component";
|
||||
import { AdminTelegramBridgeManageSelfhostedComponent } from "./admin/bridges/telegram/manage-selfhosted/manage-selfhosted.component";
|
||||
import { TelegramApiService } from "./shared/services/integrations/telegram-api.service";
|
||||
import { TelegramBridgeConfigComponent } from "./configs/bridge/telegram/telegram.bridge.component";
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
@ -150,6 +152,7 @@ import { AdminTelegramBridgeManageSelfhostedComponent } from "./admin/bridges/te
|
||||
StickerPickerWidgetWrapperComponent,
|
||||
AdminTelegramBridgeComponent,
|
||||
AdminTelegramBridgeManageSelfhostedComponent,
|
||||
TelegramBridgeConfigComponent,
|
||||
|
||||
// Vendor
|
||||
],
|
||||
@ -170,6 +173,7 @@ import { AdminTelegramBridgeManageSelfhostedComponent } from "./admin/bridges/te
|
||||
MediaService,
|
||||
StickerApiService,
|
||||
AdminTelegramApiService,
|
||||
TelegramApiService,
|
||||
{provide: Window, useValue: window},
|
||||
|
||||
// Vendor
|
||||
|
@ -28,6 +28,7 @@ import { AdminStickerPacksComponent } from "./admin/sticker-packs/sticker-packs.
|
||||
import { StickerpickerComponent } from "./configs/stickerpicker/stickerpicker.component";
|
||||
import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-picker/sticker-picker.component";
|
||||
import { AdminTelegramBridgeComponent } from "./admin/bridges/telegram/telegram.component";
|
||||
import { TelegramBridgeConfigComponent } from "./configs/bridge/telegram/telegram.bridge.component";
|
||||
|
||||
const routes: Routes = [
|
||||
{path: "", component: HomeComponent},
|
||||
@ -170,6 +171,11 @@ const routes: Routes = [
|
||||
component: IrcBridgeConfigComponent,
|
||||
data: {breadcrumb: "IRC Bridge Configuration", name: "IRC Bridge Configuration"},
|
||||
},
|
||||
{
|
||||
path: "telegram",
|
||||
component: TelegramBridgeConfigComponent,
|
||||
data: {breadcrumb: "Telegram Bridge Configuration", name: "Telegram Bridge Configuration"},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -0,0 +1,32 @@
|
||||
<my-bridge-config [bridgeComponent]="this">
|
||||
<ng-template #bridgeParamsTemplate>
|
||||
<my-ibox [isCollapsible]="false">
|
||||
<h5 class="my-ibox-title">
|
||||
Bridge to Telegram
|
||||
</h5>
|
||||
<div class="my-ibox-content">
|
||||
<div *ngIf="isBridged">
|
||||
This room is bridged to "{{ chatName }}" (<code>{{ chatId }}</code>) on Telegram.
|
||||
<div *ngIf="canUnbridge">
|
||||
<button type="button" class="btn btn-sm btn-danger" [disabled]="isUpdating" (click)="unbridgeRoom()">
|
||||
Unbridge
|
||||
</button>
|
||||
</div>
|
||||
<span *ngIf="!canUnbridge">
|
||||
You do not have the necessary permissions in this room to unbridge the channel.
|
||||
</span>
|
||||
</div>
|
||||
<div *ngIf="!isBridged">
|
||||
<label class="label-block">
|
||||
Chat ID
|
||||
<span class="text-muted">After inviting <a href="https://t.me/{{ botUsername }}" target="_blank">@{{ botUsername }}</a> to your Telegram chat, run the command <code>/id</code> in the Telegram room to get the chat ID.</span>
|
||||
<input title="chat ID" type="text" class="form-control form-control-sm col-md-3" [(ngModel)]="chatId" [disabled]="isUpdating" />
|
||||
</label>
|
||||
<button type="button" class="btn btn-sm btn-primary" [disabled]="isUpdating" (click)="bridgeRoom()">
|
||||
Bridge
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</my-ibox>
|
||||
</ng-template>
|
||||
</my-bridge-config>
|
@ -0,0 +1,4 @@
|
||||
.actions-col {
|
||||
width: 120px;
|
||||
text-align: center;
|
||||
}
|
122
web/app/configs/bridge/telegram/telegram.bridge.component.ts
Normal file
122
web/app/configs/bridge/telegram/telegram.bridge.component.ts
Normal file
@ -0,0 +1,122 @@
|
||||
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";
|
||||
|
||||
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;
|
||||
|
||||
constructor(private telegram: TelegramApiService) {
|
||||
super("telegram");
|
||||
}
|
||||
|
||||
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 {
|
||||
this.telegram.getPortalInfo(this.bridge.config.portalInfo.chatId, this.roomId).then(chatInfo => {
|
||||
if (chatInfo.bridged && chatInfo.canUnbridge) {
|
||||
// TODO: Ask if the user would like to unbridge
|
||||
console.log("Ask for unbridge");
|
||||
} else if (chatInfo.bridged) {
|
||||
// TODO: Dialog saying 'sorry'
|
||||
console.log("Cannot bridge");
|
||||
}
|
||||
return this.telegram.bridgeRoom(this.roomId, this.bridge.config.portalInfo.chatId);
|
||||
}).then(portalInfo => {
|
||||
this.bridge.config.portalInfo = portalInfo;
|
||||
this.bridge.config.linked = [portalInfo.chatId];
|
||||
this.isUpdating = false;
|
||||
this.toaster.pop("success", "Bridge updated");
|
||||
}).catch(error => {
|
||||
this.isUpdating = false;
|
||||
console.error(error);
|
||||
|
||||
const body = error.json ? error.json() : null;
|
||||
let message = "Error bridging room";
|
||||
if (body) {
|
||||
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";
|
||||
}
|
||||
this.toaster.pop("error", message);
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
this.toaster.pop("success", "Bridge removed");
|
||||
}).catch(error => {
|
||||
this.isUpdating = false;
|
||||
console.error(error);
|
||||
|
||||
const body = error.json ? error.json() : null;
|
||||
let message = "Error removing bridge";
|
||||
if (body) {
|
||||
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";
|
||||
}
|
||||
this.toaster.pop("error", message);
|
||||
});
|
||||
}
|
||||
}
|
@ -2,7 +2,20 @@ export interface FE_TelegramBridge {
|
||||
id: number;
|
||||
upstreamId?: number;
|
||||
provisionUrl?: string;
|
||||
allowPuppets?: boolean;
|
||||
sharedSecret?: string;
|
||||
isEnabled: boolean;
|
||||
options?: FE_TelegramBridgeOptions;
|
||||
}
|
||||
|
||||
export interface FE_PortalInfo {
|
||||
bridged: boolean;
|
||||
chatId: number;
|
||||
roomId: string;
|
||||
canUnbridge: boolean;
|
||||
chatName: string;
|
||||
}
|
||||
|
||||
export interface FE_TelegramBridgeOptions {
|
||||
allowTgPuppets: boolean;
|
||||
allowMxPuppets: boolean;
|
||||
}
|
@ -17,6 +17,7 @@ export class IntegrationsRegistry {
|
||||
},
|
||||
"bridge": {
|
||||
"irc": {},
|
||||
"telegram": {},
|
||||
},
|
||||
"widget": {
|
||||
"custom": {
|
||||
|
@ -2,7 +2,7 @@ import { Injectable } from "@angular/core";
|
||||
import { Http } from "@angular/http";
|
||||
import { AuthedApi } from "../authed-api";
|
||||
import { FE_Upstream } from "../../models/admin-responses";
|
||||
import { FE_TelegramBridge } from "../../models/telegram";
|
||||
import { FE_TelegramBridge, FE_TelegramBridgeOptions } from "../../models/telegram";
|
||||
|
||||
@Injectable()
|
||||
export class AdminTelegramApiService extends AuthedApi {
|
||||
@ -22,19 +22,19 @@ export class AdminTelegramApiService extends AuthedApi {
|
||||
return this.authedPost("/api/v1/dimension/admin/telegram/new/upstream", {upstreamId: upstream.id}).map(r => r.json()).toPromise();
|
||||
}
|
||||
|
||||
public newSelfhosted(provisionUrl: string, sharedSecret: string, allowPuppets: boolean): Promise<FE_TelegramBridge> {
|
||||
public newSelfhosted(provisionUrl: string, sharedSecret: string, options: FE_TelegramBridgeOptions): Promise<FE_TelegramBridge> {
|
||||
return this.authedPost("/api/v1/dimension/admin/telegram/new/selfhosted", {
|
||||
provisionUrl: provisionUrl,
|
||||
sharedSecret: sharedSecret,
|
||||
allowPuppets: allowPuppets,
|
||||
options: options,
|
||||
}).map(r => r.json()).toPromise();
|
||||
}
|
||||
|
||||
public updateSelfhosted(bridgeId: number, provisionUrl: string, sharedSecret: string, allowPuppets: boolean): Promise<FE_TelegramBridge> {
|
||||
public updateSelfhosted(bridgeId: number, provisionUrl: string, sharedSecret: string, options: FE_TelegramBridgeOptions): Promise<FE_TelegramBridge> {
|
||||
return this.authedPost("/api/v1/dimension/admin/telegram/" + bridgeId, {
|
||||
provisionUrl: provisionUrl,
|
||||
sharedSecret: sharedSecret,
|
||||
allowPuppets: allowPuppets,
|
||||
options: options,
|
||||
}).map(r => r.json()).toPromise();
|
||||
}
|
||||
}
|
||||
|
24
web/app/shared/services/integrations/telegram-api.service.ts
Normal file
24
web/app/shared/services/integrations/telegram-api.service.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { Http } from "@angular/http";
|
||||
import { AuthedApi } from "../authed-api";
|
||||
import { FE_PortalInfo } from "../../models/telegram";
|
||||
|
||||
@Injectable()
|
||||
export class TelegramApiService extends AuthedApi {
|
||||
constructor(http: Http) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public getPortalInfo(chatId: number, roomId: string): Promise<FE_PortalInfo> {
|
||||
return this.authedGet("/api/v1/dimension/telegram/chat/" + chatId, {roomId: roomId}).map(r => r.json()).toPromise();
|
||||
}
|
||||
|
||||
public bridgeRoom(roomId: string, chatId: number, unbridgeOtherPortals: boolean = false): Promise<FE_PortalInfo> {
|
||||
return this.authedPost("/api/v1/dimension/telegram/chat/" + chatId + "/room/" + roomId, {unbridgeOtherPortals})
|
||||
.map(r => r.json()).toPromise();
|
||||
}
|
||||
|
||||
public unbridgeRoom(roomId: string): Promise<FE_PortalInfo> {
|
||||
return this.authedDelete("/api/v1/dimension/telegram/room/" + roomId).map(r => r.json()).toPromise();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user