Merge remote-tracking branch 'origin/master' into travis/webhooks2

This commit is contained in:
Travis Ralston 2018-10-20 13:19:45 -06:00
commit 2416718065
38 changed files with 1219 additions and 8 deletions

View File

@ -3,8 +3,10 @@ FROM node:9.11.2-alpine
LABEL maintainer="Andreas Peters <support@aventer.biz>" LABEL maintainer="Andreas Peters <support@aventer.biz>"
#Upstream URL: https://git.aventer.biz/AVENTER/docker-matrix-dimension #Upstream URL: https://git.aventer.biz/AVENTER/docker-matrix-dimension
RUN apk add dos2unix --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ --allow-untrusted
RUN apk update && \ RUN apk update && \
apk add bash gcc python make g++ sqlite && \ apk add --no-cache bash gcc python make g++ sqlite && \
mkdir /home/node/.npm-global && \ mkdir /home/node/.npm-global && \
mkdir -p /home/node/app mkdir -p /home/node/app
@ -29,7 +31,8 @@ USER root
RUN apk del gcc make g++ && \ RUN apk del gcc make g++ && \
rm /home/node/matrix-dimension/Dockerfile && \ rm /home/node/matrix-dimension/Dockerfile && \
rm /home/node/matrix-dimension/docker-entrypoint.sh rm /home/node/matrix-dimension/docker-entrypoint.sh && \
dos2unix /docker-entrypoint.sh
USER node USER node

View File

@ -49,3 +49,4 @@ export const CACHE_WIDGET_TITLES = "widget-titles";
export const CACHE_FEDERATION = "federation"; export const CACHE_FEDERATION = "federation";
export const CACHE_IRC_BRIDGE = "irc-bridge"; export const CACHE_IRC_BRIDGE = "irc-bridge";
export const CACHE_STICKERS = "stickers"; export const CACHE_STICKERS = "stickers";
export const CACHE_TELEGRAM_BRIDGE = "telegram-bridge";

View File

@ -35,5 +35,7 @@ export class ApiError {
this.jsonResponse = json; this.jsonResponse = json;
this.statusCode = statusCode; this.statusCode = statusCode;
this.errorCode = errCode; this.errorCode = errCode;
this.jsonResponse["dim_errcode"] = this.errorCode;
} }
} }

View File

@ -0,0 +1,118 @@
import { GET, Path, PathParam, POST, QueryParam } from "typescript-rest";
import { AdminService } from "./AdminService";
import { Cache, CACHE_INTEGRATIONS, CACHE_TELEGRAM_BRIDGE } from "../../MemoryCache";
import { LogService } from "matrix-js-snippets";
import { ApiError } from "../ApiError";
import TelegramBridgeRecord from "../../db/models/TelegramBridgeRecord";
interface CreateWithUpstream {
upstreamId: number;
}
interface CreateSelfhosted {
provisionUrl: string;
sharedSecret: string;
allowTgPuppets: boolean;
allowMxPuppets: boolean;
}
interface BridgeResponse {
id: number;
upstreamId?: number;
provisionUrl?: string;
allowTgPuppets?: boolean;
allowMxPuppets?: boolean;
sharedSecret?: string;
isEnabled: boolean;
}
/**
* Administrative API for configuring Telegram bridge instances.
*/
@Path("/api/v1/dimension/admin/telegram")
export class AdminTelegramService {
@GET
@Path("all")
public async getBridges(@QueryParam("scalar_token") scalarToken: string): Promise<BridgeResponse[]> {
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bridges = await TelegramBridgeRecord.findAll();
return Promise.all(bridges.map(async b => {
return {
id: b.id,
upstreamId: b.upstreamId,
provisionUrl: b.provisionUrl,
allowTgPuppets: b.allowTgPuppets,
allowMxPuppets: b.allowMxPuppets,
sharedSecret: b.sharedSecret,
isEnabled: b.isEnabled,
};
}));
}
@GET
@Path(":bridgeId")
public async getBridge(@QueryParam("scalar_token") scalarToken: string, @PathParam("bridgeId") bridgeId: number): Promise<BridgeResponse> {
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const telegramBridge = await TelegramBridgeRecord.findByPrimary(bridgeId);
if (!telegramBridge) throw new ApiError(404, "Telegram Bridge not found");
return {
id: telegramBridge.id,
upstreamId: telegramBridge.upstreamId,
provisionUrl: telegramBridge.provisionUrl,
allowTgPuppets: telegramBridge.allowTgPuppets,
allowMxPuppets: telegramBridge.allowMxPuppets,
sharedSecret: telegramBridge.sharedSecret,
isEnabled: telegramBridge.isEnabled,
};
}
@POST
@Path(":bridgeId")
public async updateBridge(@QueryParam("scalar_token") scalarToken: string, @PathParam("bridgeId") bridgeId: number, request: CreateSelfhosted): Promise<BridgeResponse> {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bridge = await TelegramBridgeRecord.findByPrimary(bridgeId);
if (!bridge) throw new ApiError(404, "Bridge not found");
bridge.provisionUrl = request.provisionUrl;
bridge.sharedSecret = request.sharedSecret;
bridge.allowTgPuppets = request.allowTgPuppets;
bridge.allowMxPuppets = request.allowMxPuppets;
await bridge.save();
LogService.info("AdminTelegramService", userId + " updated Telegram Bridge " + bridge.id);
Cache.for(CACHE_TELEGRAM_BRIDGE).clear();
Cache.for(CACHE_INTEGRATIONS).clear();
return this.getBridge(scalarToken, bridge.id);
}
@POST
@Path("new/upstream")
public async newConfigForUpstream(@QueryParam("scalar_token") _scalarToken: string, _request: CreateWithUpstream): Promise<BridgeResponse> {
throw new ApiError(400, "Cannot create a telegram bridge from an upstream");
}
@POST
@Path("new/selfhosted")
public async newSelfhosted(@QueryParam("scalar_token") scalarToken: string, request: CreateSelfhosted): Promise<BridgeResponse> {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bridge = await TelegramBridgeRecord.create({
provisionUrl: request.provisionUrl,
sharedSecret: request.sharedSecret,
allowTgPuppets: request.allowTgPuppets,
allowMxPuppets: request.allowMxPuppets,
isEnabled: true,
});
LogService.info("AdminTelegramService", userId + " created a new Telegram Bridge with provisioning URL " + request.provisionUrl);
Cache.for(CACHE_TELEGRAM_BRIDGE).clear();
Cache.for(CACHE_INTEGRATIONS).clear();
return this.getBridge(scalarToken, bridge.id);
}
}

View File

@ -0,0 +1,90 @@
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;
}
interface BridgeRoomRequest {
unbridgeOtherPortals: boolean;
}
/**
* 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);
return {
bridged: conf ? conf.bridged : false,
canUnbridge: conf ? conf.canUnbridge : false,
chatId: conf ? conf.chatId : null,
roomId: conf ? conf.roomId : null,
chatName: conf ? conf.chatName : null,
};
} 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, request: BridgeRoomRequest): Promise<PortalInfoResponse> {
const userId = await ScalarService.getTokenOwner(scalarToken);
try {
const telegram = new TelegramBridge(userId);
const portal = await telegram.bridgeRoom(chatId, roomId, request.unbridgeOtherPortals);
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;
}
}
}

View File

@ -11,11 +11,8 @@ interface AppServiceTransaction {
/** /**
* API for handling appservice traffic from a homeserver * API for handling appservice traffic from a homeserver
*/ */
// Note: There's no actual defined prefix for this API. The following was chosen to be
// somewhat consistent with the other matrix APIs. In reality, the homeserver will just
// hit the URL given in the registration - be sure to define it to match this prefix.
// Eg: `url: "http://localhost:8184/_matrix/appservice/r0"`
@Path("/_matrix/appservice/r0") @Path("/_matrix/appservice/r0")
@Path("/_matrix/app/v1") // the matrix spec version
export class MatrixAppServiceApiService { export class MatrixAppServiceApiService {
@PUT @PUT

View File

@ -0,0 +1,231 @@
import TelegramBridgeRecord from "../db/models/TelegramBridgeRecord";
import { LogService } from "matrix-js-snippets";
import * as request from "request";
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 getBridgeInfo(): Promise<BridgeInfo> {
const bridge = await this.getDefaultBridge();
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 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 && info.mxid === inRoomId,
chatId: info ? info.chat_id : 0,
roomId: info.mxid,
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 && !!info.mxid,
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, unbridgeOtherPortals = false): Promise<PortalInfo> {
const bridge = await this.getDefaultBridge();
try {
const qs = {};
if (unbridgeOtherPortals) qs["force"] = "unbridge";
await this.doProvisionRequest(bridge, "POST", `/portal/${roomId}/connect/${chatId}`, qs);
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> {
const provisionUrl = bridge.provisionUrl;
const apiUrl = provisionUrl.endsWith("/") ? provisionUrl.substring(0, provisionUrl.length - 1) : provisionUrl;
const url = apiUrl + (endpoint.startsWith("/") ? endpoint : "/" + endpoint);
LogService.info("TelegramBridge", "Doing provision Telegram Bridge request: " + url);
if (!qs) qs = {};
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({
method: method,
url: url,
qs: qs,
json: body,
headers: {
"Authorization": `Bearer ${bridge.sharedSecret}`,
},
}, (err, res, _body) => {
if (err) {
LogService.error("TelegramBridge", "Error calling" + url);
LogService.error("TelegramBridge", err);
reject(err);
} 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 && 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);
reject({errBody: res.body, error: new Error("Request failed")});
} else {
if (typeof(res.body) === "string") res.body = JSON.parse(res.body);
resolve(res.body);
}
});
});
}
}

View File

@ -0,0 +1,32 @@
export interface PortalInformationResponse {
mxid: string;
chat_id: number;
peer_type: string;
megagroup: boolean;
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;
}

View File

@ -1,7 +1,9 @@
import { Bridge, TelegramBridgeConfiguration } from "../integrations/Bridge";
import { Bridge, WebhookBridgeConfiguration } from "../integrations/Bridge"; import { Bridge, WebhookBridgeConfiguration } from "../integrations/Bridge";
import BridgeRecord from "./models/BridgeRecord"; import BridgeRecord from "./models/BridgeRecord";
import { IrcBridge } from "../bridges/IrcBridge"; import { IrcBridge } from "../bridges/IrcBridge";
import { LogService } from "matrix-js-snippets"; import { LogService } from "matrix-js-snippets";
import { TelegramBridge } from "../bridges/TelegramBridge";
import { WebhooksBridge } from "../bridges/WebhooksBridge"; import { WebhooksBridge } from "../bridges/WebhooksBridge";
export class BridgeStore { export class BridgeStore {
@ -45,6 +47,8 @@ export class BridgeStore {
if (integrationType === "irc") { if (integrationType === "irc") {
throw new Error("IRC Bridges should be modified with the dedicated API"); throw new Error("IRC Bridges should be modified with the dedicated API");
} else if (integrationType === "telegram") {
throw new Error("Telegram bridges should be modified with the dedicated API");
} else if (integrationType === "webhooks") { } else if (integrationType === "webhooks") {
throw new Error("Webhooks should be modified with the dedicated API"); throw new Error("Webhooks should be modified with the dedicated API");
} else throw new Error("Unsupported bridge"); } else throw new Error("Unsupported bridge");
@ -54,6 +58,9 @@ export class BridgeStore {
if (record.type === "irc") { if (record.type === "irc") {
const irc = new IrcBridge(requestingUserId); const irc = new IrcBridge(requestingUserId);
return irc.hasNetworks(); return irc.hasNetworks();
} else if (record.type === "telegram") {
const telegram = new TelegramBridge(requestingUserId);
return telegram.isBridgingEnabled();
} else if (record.type === "webhooks") { } else if (record.type === "webhooks") {
const webhooks = new WebhooksBridge(requestingUserId); const webhooks = new WebhooksBridge(requestingUserId);
return webhooks.isBridgingEnabled(); return webhooks.isBridgingEnabled();
@ -65,6 +72,17 @@ export class BridgeStore {
if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs
const irc = new IrcBridge(requestingUserId); const irc = new IrcBridge(requestingUserId);
return irc.getRoomConfiguration(inRoomId); return irc.getRoomConfiguration(inRoomId);
} else if (record.type === "telegram") {
if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs
const telegram = new TelegramBridge(requestingUserId);
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 if (record.type === "webhooks") { } else if (record.type === "webhooks") {
if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs if (!inRoomId) return {}; // The bridge's admin config is handled by other APIs
const webhooks = new WebhooksBridge(requestingUserId); const webhooks = new WebhooksBridge(requestingUserId);

View File

@ -21,6 +21,7 @@ import IrcBridgeNetwork from "./models/IrcBridgeNetwork";
import StickerPack from "./models/StickerPack"; import StickerPack from "./models/StickerPack";
import Sticker from "./models/Sticker"; import Sticker from "./models/Sticker";
import UserStickerPack from "./models/UserStickerPack"; import UserStickerPack from "./models/UserStickerPack";
import TelegramBridgeRecord from "./models/TelegramBridgeRecord";
import WebhookBridgeRecord from "./models/WebhookBridgeRecord"; import WebhookBridgeRecord from "./models/WebhookBridgeRecord";
class _DimensionStore { class _DimensionStore {
@ -54,6 +55,7 @@ class _DimensionStore {
StickerPack, StickerPack,
Sticker, Sticker,
UserStickerPack, UserStickerPack,
TelegramBridgeRecord,
WebhookBridgeRecord, WebhookBridgeRecord,
]); ]);
} }

View File

@ -0,0 +1,24 @@
import { QueryInterface } from "sequelize";
import { DataType } from "sequelize-typescript";
export default {
up: (queryInterface: QueryInterface) => {
return Promise.resolve()
.then(() => queryInterface.createTable("dimension_telegram_bridges", {
"id": {type: DataType.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
"upstreamId": {
type: DataType.INTEGER, allowNull: true,
references: {model: "dimension_upstreams", key: "id"},
onUpdate: "cascade", onDelete: "cascade",
},
"provisionUrl": {type: DataType.STRING, allowNull: true},
"sharedSecret": {type: DataType.STRING, allowNull: true},
"allowPuppets": {type: DataType.BOOLEAN, allowNull: true},
"isEnabled": {type: DataType.BOOLEAN, allowNull: false},
}));
},
down: (queryInterface: QueryInterface) => {
return Promise.resolve()
.then(() => queryInterface.dropTable("dimension_telegram_bridges"));
}
}

View File

@ -0,0 +1,23 @@
import { QueryInterface } from "sequelize";
export default {
up: (queryInterface: QueryInterface) => {
return Promise.resolve()
.then(() => queryInterface.bulkInsert("dimension_bridges", [
{
type: "telegram",
name: "Telegram Bridge",
avatarUrl: "/img/avatars/telegram.png",
isEnabled: true,
isPublic: true,
description: "Bridges Telegram chats and channels to rooms on Matrix",
},
]));
},
down: (queryInterface: QueryInterface) => {
return Promise.resolve()
.then(() => queryInterface.bulkDelete("dimension_bridges", {
type: "telegram",
}));
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,38 @@
import { AllowNull, AutoIncrement, Column, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
import Upstream from "./Upstream";
@Table({
tableName: "dimension_telegram_bridges",
underscoredAll: false,
timestamps: false,
})
export default class TelegramBridgeRecord extends Model<TelegramBridgeRecord> {
@PrimaryKey
@AutoIncrement
@Column
id: number;
@AllowNull
@Column
@ForeignKey(() => Upstream)
upstreamId?: number;
@AllowNull
@Column
provisionUrl?: string;
@AllowNull
@Column
sharedSecret?: string;
@AllowNull
@Column
allowTgPuppets?: boolean;
@AllowNull
@Column
allowMxPuppets?: boolean;
@Column
isEnabled: boolean;
}

View File

@ -1,6 +1,7 @@
import { Integration } from "./Integration"; import { Integration } from "./Integration";
import BridgeRecord from "../db/models/BridgeRecord"; import BridgeRecord from "../db/models/BridgeRecord";
import { AvailableNetworks, LinkedChannels } from "../bridges/IrcBridge"; import { AvailableNetworks, LinkedChannels } from "../bridges/IrcBridge";
import { PortalInfo, PuppetInfo } from "../bridges/TelegramBridge";
import { WebhookConfiguration } from "../bridges/models/webhooks"; import { WebhookConfiguration } from "../bridges/models/webhooks";
export class Bridge extends Integration { export class Bridge extends Integration {
@ -23,6 +24,13 @@ export interface IrcBridgeConfiguration {
links: LinkedChannels; links: LinkedChannels;
} }
export interface TelegramBridgeConfiguration {
botUsername: string;
linked: number[];
portalInfo: PortalInfo;
puppet: PuppetInfo;
}
export interface WebhookBridgeConfiguration { export interface WebhookBridgeConfiguration {
webhooks: WebhookConfiguration[]; webhooks: WebhookConfiguration[];
} }

View File

@ -20,7 +20,7 @@
</thead> </thead>
<tbody> <tbody>
<tr *ngIf="!configurations || configurations.length === 0"> <tr *ngIf="!configurations || configurations.length === 0">
<td colspan="2"><i>No bridge configurations.</i></td> <td colspan="3"><i>No bridge configurations.</i></td>
</tr> </tr>
<tr *ngFor="let bridge of configurations trackById"> <tr *ngFor="let bridge of configurations trackById">
<td> <td>

View File

@ -0,0 +1,46 @@
<div class="dialog">
<div class="dialog-header">
<h4>{{ isAdding ? "Add a new" : "Edit" }} self-hosted Telegram bridge</h4>
</div>
<div class="dialog-content">
<p>Self-hosted Telegram bridges must have <code>provisioning</code> enabled in the configuration.</p>
<label class="label-block">
Provisioning URL
<span class="text-muted ">The provisioning URL for the bridge. This is the public address for the bridge followed by the provisioning prefix given in the configuration.</span>
<input type="text" class="form-control"
placeholder="http://localhost:9999/_matrix/provision/v1"
[(ngModel)]="provisionUrl" [disabled]="isSaving"/>
</label>
<label class="label-block">
Shared Secret
<span class="text-muted ">The shared secret defined in the configuration for provisioning.</span>
<input type="text" class="form-control"
placeholder="some_secret_value"
[(ngModel)]="sharedSecret" [disabled]="isSaving"/>
</label>
<label class="label-block">
Promote Telegram Puppeting
<span class="text-muted ">If enabled, Dimension will recommend that users log in to their Telegram accounts.</span>
<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">
<button type="button" (click)="add()" title="close" class="btn btn-primary btn-sm">
<i class="far fa-save"></i> Save
</button>
<button type="button" (click)="dialog.close()" title="close" class="btn btn-secondary btn-sm">
<i class="far fa-times-circle"></i> Cancel
</button>
</div>
</div>

View File

@ -0,0 +1,66 @@
import { Component } from "@angular/core";
import { ToasterService } from "angular2-toaster";
import { DialogRef, ModalComponent } from "ngx-modialog";
import { BSModalContext } from "ngx-modialog/plugins/bootstrap";
import { AdminTelegramApiService } from "../../../../shared/services/admin/admin-telegram-api.service";
export class ManageSelfhostedTelegramBridgeDialogContext extends BSModalContext {
public provisionUrl: string;
public sharedSecret: string;
public allowTgPuppets = false;
public allowMxPuppets = false;
public bridgeId: number;
}
@Component({
templateUrl: "./manage-selfhosted.component.html",
styleUrls: ["./manage-selfhosted.component.scss"],
})
export class AdminTelegramBridgeManageSelfhostedComponent implements ModalComponent<ManageSelfhostedTelegramBridgeDialogContext> {
public isSaving = false;
public provisionUrl: string;
public sharedSecret: string;
public allowTgPuppets = false;
public allowMxPuppets = false;
public bridgeId: number;
public isAdding = false;
constructor(public dialog: DialogRef<ManageSelfhostedTelegramBridgeDialogContext>,
private telegramApi: AdminTelegramApiService,
private toaster: ToasterService) {
this.provisionUrl = dialog.context.provisionUrl;
this.sharedSecret = dialog.context.sharedSecret;
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, options).then(() => {
this.toaster.pop("success", "Telegram bridge added");
this.dialog.close();
}).catch(err => {
console.error(err);
this.isSaving = false;
this.toaster.pop("error", "Failed to create Telegram bridge");
});
} else {
this.telegramApi.updateSelfhosted(this.bridgeId, this.provisionUrl, this.sharedSecret, options).then(() => {
this.toaster.pop("success", "Telegram bridge updated");
this.dialog.close();
}).catch(err => {
console.error(err);
this.isSaving = false;
this.toaster.pop("error", "Failed to update Telegram bridge");
});
}
}
}

View File

@ -0,0 +1,48 @@
<div *ngIf="isLoading">
<my-spinner></my-spinner>
</div>
<div *ngIf="!isLoading">
<my-ibox title="Telegram Bridge Configurations">
<div class="my-ibox-content">
<p>
<a href="https://github.com/tulir/mautrix-telegram" target="_blank">mautrix-telegram</a>
is a Telegram bridge that supports bridging channels/supergroups to Matrix. This can be
done through a "relay bot" (all Matrix users are represented through a single bot in Telegram)
or by making use of a user's real Telegram account (known as "puppeting"). Currently
only one Telegram bridge can be configured at a time.
</p>
<table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Enabled Features</th>
<th class="text-center" style="width: 120px;">Actions</th>
</tr>
</thead>
<tbody>
<tr *ngIf="!configurations || configurations.length === 0">
<td colspan="3"><i>No bridge configurations.</i></td>
</tr>
<tr *ngFor="let bridge of configurations trackById">
<td>
{{ bridge.upstreamId ? "matrix.org's bridge" : "Self-hosted bridge" }}
<span class="text-muted" style="display: inline-block;" *ngIf="!bridge.upstreamId">({{ bridge.provisionUrl }})</span>
</td>
<td>
{{ getEnabledFeaturesString(bridge) }}
</td>
<td class="text-center">
<span class="editButton" (click)="editBridge(bridge)">
<i class="fa fa-pencil-alt"></i>
</span>
</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-success btn-sm" (click)="addSelfHostedBridge()" [disabled]="configurations && configurations.length > 0">
<i class="fa fa-plus"></i> Add self-hosted bridge
</button>
</div>
</my-ibox>
</div>

View File

@ -0,0 +1,3 @@
.editButton {
cursor: pointer;
}

View File

@ -0,0 +1,79 @@
import { Component, OnInit } from "@angular/core";
import { ToasterService } from "angular2-toaster";
import { Modal, overlayConfigFactory } from "ngx-modialog";
import {
AdminTelegramBridgeManageSelfhostedComponent,
ManageSelfhostedTelegramBridgeDialogContext
} from "./manage-selfhosted/manage-selfhosted.component";
import { FE_TelegramBridge } from "../../../shared/models/telegram";
import { AdminTelegramApiService } from "../../../shared/services/admin/admin-telegram-api.service";
@Component({
templateUrl: "./telegram.component.html",
styleUrls: ["./telegram.component.scss"],
})
export class AdminTelegramBridgeComponent implements OnInit {
public isLoading = true;
public isUpdating = false;
public configurations: FE_TelegramBridge[] = [];
constructor(private telegramApi: AdminTelegramApiService,
private toaster: ToasterService,
private modal: Modal) {
}
public ngOnInit() {
this.reload().then(() => this.isLoading = false);
}
private async reload(): Promise<any> {
try {
this.configurations = await this.telegramApi.getBridges();
} catch (err) {
console.error(err);
this.toaster.pop("error", "Error loading bridges");
}
}
public addSelfHostedBridge() {
this.modal.open(AdminTelegramBridgeManageSelfhostedComponent, overlayConfigFactory({
isBlocking: true,
size: 'lg',
provisionUrl: '',
sharedSecret: '',
allowPuppets: false,
}, ManageSelfhostedTelegramBridgeDialogContext)).result.then(() => {
this.reload().catch(err => {
console.error(err);
this.toaster.pop("error", "Failed to get an update Telegram bridge list");
});
});
}
public getEnabledFeaturesString(bridge: FE_TelegramBridge): string {
if (!bridge.options) return "";
if (bridge.options.allowTgPuppets) return "Telegram Puppetting";
if (bridge.options.allowMxPuppets) return "Matrix Puppetting";
return "";
}
public editBridge(bridge: FE_TelegramBridge) {
this.modal.open(AdminTelegramBridgeManageSelfhostedComponent, overlayConfigFactory({
isBlocking: true,
size: 'lg',
provisionUrl: bridge.provisionUrl,
sharedSecret: bridge.sharedSecret,
allowTgPuppets: bridge.options ? bridge.options.allowTgPuppets : false,
allowMxPuppets: bridge.options ? bridge.options.allowMxPuppets : false,
bridgeId: bridge.id,
}, ManageSelfhostedTelegramBridgeDialogContext)).result.then(() => {
this.reload().catch(err => {
console.error(err);
this.toaster.pop("error", "Failed to get an update Telegram bridge list");
});
});
}
}

View File

@ -78,6 +78,13 @@ import { MediaService } from "./shared/services/media.service";
import { StickerApiService } from "./shared/services/integrations/sticker-api.service"; import { StickerApiService } from "./shared/services/integrations/sticker-api.service";
import { StickerpickerComponent } from "./configs/stickerpicker/stickerpicker.component"; import { StickerpickerComponent } from "./configs/stickerpicker/stickerpicker.component";
import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-picker/sticker-picker.component"; import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-picker/sticker-picker.component";
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";
import { TelegramAskUnbridgeComponent } from "./configs/bridge/telegram/ask-unbridge/ask-unbridge.component";
import { TelegramCannotUnbridgeComponent } from "./configs/bridge/telegram/cannot-unbridge/cannot-unbridge.component";
@NgModule({ @NgModule({
imports: [ imports: [
@ -145,6 +152,11 @@ import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-p
AdminStickerPackPreviewComponent, AdminStickerPackPreviewComponent,
StickerpickerComponent, StickerpickerComponent,
StickerPickerWidgetWrapperComponent, StickerPickerWidgetWrapperComponent,
AdminTelegramBridgeComponent,
AdminTelegramBridgeManageSelfhostedComponent,
TelegramBridgeConfigComponent,
TelegramAskUnbridgeComponent,
TelegramCannotUnbridgeComponent,
// Vendor // Vendor
], ],
@ -164,6 +176,8 @@ import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-p
AdminStickersApiService, AdminStickersApiService,
MediaService, MediaService,
StickerApiService, StickerApiService,
AdminTelegramApiService,
TelegramApiService,
{provide: Window, useValue: window}, {provide: Window, useValue: window},
// Vendor // Vendor
@ -181,6 +195,9 @@ import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-p
AdminIrcBridgeNetworksComponent, AdminIrcBridgeNetworksComponent,
AdminIrcBridgeAddSelfhostedComponent, AdminIrcBridgeAddSelfhostedComponent,
AdminStickerPackPreviewComponent, AdminStickerPackPreviewComponent,
AdminTelegramBridgeManageSelfhostedComponent,
TelegramAskUnbridgeComponent,
TelegramCannotUnbridgeComponent,
] ]
}) })
export class AppModule { export class AppModule {

View File

@ -27,6 +27,8 @@ import { IrcBridgeConfigComponent } from "./configs/bridge/irc/irc.bridge.compon
import { AdminStickerPacksComponent } from "./admin/sticker-packs/sticker-packs.component"; import { AdminStickerPacksComponent } from "./admin/sticker-packs/sticker-packs.component";
import { StickerpickerComponent } from "./configs/stickerpicker/stickerpicker.component"; import { StickerpickerComponent } from "./configs/stickerpicker/stickerpicker.component";
import { StickerPickerWidgetWrapperComponent } from "./widget-wrappers/sticker-picker/sticker-picker.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 = [ const routes: Routes = [
{path: "", component: HomeComponent}, {path: "", component: HomeComponent},
@ -87,6 +89,11 @@ const routes: Routes = [
component: AdminIrcBridgeComponent, component: AdminIrcBridgeComponent,
data: {breadcrumb: "IRC Bridge", name: "IRC Bridge"}, data: {breadcrumb: "IRC Bridge", name: "IRC Bridge"},
}, },
{
path: "telegram",
component: AdminTelegramBridgeComponent,
data: {breadcrumb: "Telegram Bridge", name: "Telegram Bridge"},
},
], ],
}, },
{ {
@ -164,6 +171,11 @@ const routes: Routes = [
component: IrcBridgeConfigComponent, component: IrcBridgeConfigComponent,
data: {breadcrumb: "IRC Bridge Configuration", name: "IRC Bridge Configuration"}, data: {breadcrumb: "IRC Bridge Configuration", name: "IRC Bridge Configuration"},
}, },
{
path: "telegram",
component: TelegramBridgeConfigComponent,
data: {breadcrumb: "Telegram Bridge Configuration", name: "Telegram Bridge Configuration"},
},
], ],
}, },
{ {

View File

@ -0,0 +1,17 @@
<div class="dialog">
<div class="dialog-header">
<h4>Telegram chat is already bridged</h4>
</div>
<div class="dialog-content">
You have the appropriate permissions to be able to unbridge the chat, however. Would you like to unbridge
the other room and instead bridge it here?
</div>
<div class="dialog-footer">
<button type="button" (click)="unbridgeAndContinue()" title="unbridge and continue" class="btn btn-danger btn-sm">
Unbridge and continue
</button>
<button type="button" (click)="cancel()" title="cancel" class="btn btn-primary btn-sm">
<i class="far fa-times-circle"></i> No, don't bridge
</button>
</div>
</div>

View File

@ -0,0 +1,24 @@
import { Component } from "@angular/core";
import { DialogRef, ModalComponent } from "ngx-modialog";
import { BSModalContext } from "ngx-modialog/plugins/bootstrap";
export class AskUnbridgeDialogContext extends BSModalContext {
}
@Component({
templateUrl: "./ask-unbridge.component.html",
styleUrls: ["./ask-unbridge.component.scss"],
})
export class TelegramAskUnbridgeComponent implements ModalComponent<AskUnbridgeDialogContext> {
constructor(public dialog: DialogRef<AskUnbridgeDialogContext>) {
}
public unbridgeAndContinue(): void {
this.dialog.close({unbridge: true});
}
public cancel(): void {
this.dialog.close({unbridge: false});
}
}

View File

@ -0,0 +1,14 @@
<div class="dialog">
<div class="dialog-header">
<h4>Telegram chat is already bridged</h4>
</div>
<div class="dialog-content">
That Telegram chat is bridged to another Matrix room and cannot be bridged here. Unfortunately, you do not
have the required permissions to be able to unbridge the other room.
</div>
<div class="dialog-footer">
<button type="button" (click)="dialog.close()" title="cancel" class="btn btn-primary btn-sm">
<i class="far fa-times-circle"></i> Close
</button>
</div>
</div>

View File

@ -0,0 +1,16 @@
import { Component } from "@angular/core";
import { DialogRef, ModalComponent } from "ngx-modialog";
import { BSModalContext } from "ngx-modialog/plugins/bootstrap";
export class CannotUnbridgeDialogContext extends BSModalContext {
}
@Component({
templateUrl: "./cannot-unbridge.component.html",
styleUrls: ["./cannot-unbridge.component.scss"],
})
export class TelegramCannotUnbridgeComponent implements ModalComponent<CannotUnbridgeDialogContext> {
constructor(public dialog: DialogRef<CannotUnbridgeDialogContext>) {
}
}

View File

@ -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>

View File

@ -0,0 +1,4 @@
.actions-col {
width: 120px;
text-align: center;
}

View File

@ -0,0 +1,143 @@
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";
import { Modal, overlayConfigFactory } from "ngx-modialog";
import { AskUnbridgeDialogContext, TelegramAskUnbridgeComponent } from "./ask-unbridge/ask-unbridge.component";
import {
CannotUnbridgeDialogContext,
TelegramCannotUnbridgeComponent
} from "./cannot-unbridge/cannot-unbridge.component";
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, private modal: Modal) {
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(async (chatInfo) => {
let forceUnbridge = false;
if (chatInfo.bridged && chatInfo.canUnbridge) {
const response = await this.modal.open(TelegramAskUnbridgeComponent, overlayConfigFactory({
isBlocking: true,
size: 'lg',
}, AskUnbridgeDialogContext)).result;
if (response.unbridge) {
forceUnbridge = true;
} else {
return {aborted: true};
}
} else if (chatInfo.bridged) {
this.modal.open(TelegramCannotUnbridgeComponent, overlayConfigFactory({
isBlocking: true,
size: 'lg',
}, CannotUnbridgeDialogContext));
return {aborted: true};
}
return this.telegram.bridgeRoom(this.roomId, this.bridge.config.portalInfo.chatId, forceUnbridge);
}).then((portalInfo: FE_PortalInfo) => {
if ((<any>portalInfo).aborted) return;
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);
});
}
}

View File

@ -0,0 +1,21 @@
export interface FE_TelegramBridge {
id: number;
upstreamId?: number;
provisionUrl?: string;
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;
}

View File

@ -17,6 +17,7 @@ export class IntegrationsRegistry {
}, },
"bridge": { "bridge": {
"irc": {}, "irc": {},
"telegram": {},
}, },
"widget": { "widget": {
"custom": { "custom": {

View File

@ -0,0 +1,40 @@
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, FE_TelegramBridgeOptions } from "../../models/telegram";
@Injectable()
export class AdminTelegramApiService extends AuthedApi {
constructor(http: Http) {
super(http);
}
public getBridges(): Promise<FE_TelegramBridge[]> {
return this.authedGet("/api/v1/dimension/admin/telegram/all").map(r => r.json()).toPromise();
}
public getBridge(bridgeId: number): Promise<FE_TelegramBridge> {
return this.authedGet("/api/v1/dimension/admin/telegram/" + bridgeId).map(r => r.json()).toPromise();
}
public newFromUpstream(upstream: FE_Upstream): Promise<FE_TelegramBridge> {
return this.authedPost("/api/v1/dimension/admin/telegram/new/upstream", {upstreamId: upstream.id}).map(r => r.json()).toPromise();
}
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,
options: options,
}).map(r => r.json()).toPromise();
}
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,
options: options,
}).map(r => r.json()).toPromise();
}
}

View 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 = 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();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB