2018-10-18 03:09:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:37:29 +00:00
|
|
|
interface BridgeRoomRequest {
|
|
|
|
unbridgeOtherPortals: boolean;
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:09:19 +00:00
|
|
|
/**
|
|
|
|
* 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")
|
2018-10-18 03:37:29 +00:00
|
|
|
public async bridgeRoom(@QueryParam("scalar_token") scalarToken: string, @PathParam("chatId") chatId: number, @PathParam("roomId") roomId: string, request: BridgeRoomRequest): Promise<PortalInfoResponse> {
|
2018-10-18 03:09:19 +00:00
|
|
|
const userId = await ScalarService.getTokenOwner(scalarToken);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const telegram = new TelegramBridge(userId);
|
2018-10-18 03:37:29 +00:00
|
|
|
const portal = await telegram.bridgeRoom(chatId, roomId, request.unbridgeOtherPortals);
|
2018-10-18 03:09:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|