From ad6e787486759340c817ac8630c9df343326614d Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 7 Sep 2022 13:54:07 +0000 Subject: [PATCH] add /join endpoint to api backend --- src/appservice/Api.ts | 46 +++++++++++++++++++++++++++++++- src/appservice/AppService.ts | 2 +- src/appservice/MjolnirManager.ts | 10 ++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/appservice/Api.ts b/src/appservice/Api.ts index 003e5ae..420cdb1 100644 --- a/src/appservice/Api.ts +++ b/src/appservice/Api.ts @@ -40,6 +40,7 @@ export class Api { this.httpdConfig.get("/get", this.pathGet.bind(this)); this.httpdConfig.get("/list", this.pathList.bind(this)); this.httpdConfig.post("/create", this.pathCreate.bind(this)); + this.httpdConfig.post("/join", this.pathJoin.bind(this)); this.httpdConfig.listen(port); } @@ -92,9 +93,15 @@ export class Api { return; } + const roomId = request.body["roomId"]; + if (roomId === undefined) { + response.status(400).send("invalid request"); + return; + } + const userId = await this.resolveAccessToken(accessToken); if (userId === null) { - response.status(4401).send("unauthorised"); + response.status(401).send("unauthorised"); return; } @@ -106,4 +113,41 @@ export class Api { response.status(200).json({ mxid: mjolnirId, roomId: managementRoom }); } + + private async pathJoin(request: express.Request, response: express.Response) { + const accessToken = request.body["openId"]; + if (accessToken === undefined) { + response.status(401).send("unauthorised"); + return; + } + + const userId = await this.resolveAccessToken(accessToken); + if (userId === null) { + response.status(401).send("unauthorised"); + return; + } + + const mjolnirId = request.body["mxid"]; + if (mjolnirId === undefined) { + response.status(400).send("invalid request"); + return; + } + + const roomId = request.body["roomId"]; + if (roomId === undefined) { + response.status(400).send("invalid request"); + return; + } + + const mjolnir = this.appService.mjolnirManager.mjolnirs.get(mjolnirId); + if (mjolnir === undefined) { + response.status(400).send("unknown mjolnir mxid"); + return; + } + + await mjolnir.joinRoom(roomId); + await mjolnir.addProtectedRoom(roomId); + + response.status(200).json({}); + } } diff --git a/src/appservice/AppService.ts b/src/appservice/AppService.ts index 6f94745..66d5eaa 100644 --- a/src/appservice/AppService.ts +++ b/src/appservice/AppService.ts @@ -27,7 +27,7 @@ import { Api } from "./Api"; export class MjolnirAppService { public readonly bridge: Bridge; - private readonly mjolnirManager: MjolnirManager = new MjolnirManager(); + public readonly mjolnirManager: MjolnirManager = new MjolnirManager(); public constructor() { new Api("http://localhost:8081", this).start(9001); diff --git a/src/appservice/MjolnirManager.ts b/src/appservice/MjolnirManager.ts index 459a381..9d58925 100644 --- a/src/appservice/MjolnirManager.ts +++ b/src/appservice/MjolnirManager.ts @@ -5,11 +5,12 @@ import { SHORTCODE_EVENT_TYPE } from "../models/PolicyList"; import { Permalinks, MatrixClient } from "matrix-bot-sdk"; export class MjolnirManager { - private readonly mjolnirs: Map = new Map(); + public readonly mjolnirs: Map = new Map(); public getDefaultMjolnirConfig(managementRoom: string): IConfig { let config = configRead(); config.managementRoom = managementRoom; + config.protectedRooms = []; return config; } @@ -56,6 +57,13 @@ export class ManagedMjolnir { } } + public async joinRoom(roomId: string) { + await this.mjolnir.client.joinRoom(roomId); + } + public async addProtectedRoom(roomId: string) { + await this.mjolnir.addProtectedRoom(roomId); + } + public async moveMeSomewhereCommonAndStopImplementingFunctionalityOnACommandFirstBasis(mjolnirOwnerId: string, shortcode: string) { const powerLevels: { [key: string]: any } = { "ban": 50,