add /join endpoint to api backend

This commit is contained in:
jesopo 2022-09-07 13:54:07 +00:00 committed by gnuxie
parent 039f64adb4
commit ad6e787486
3 changed files with 55 additions and 3 deletions

View File

@ -40,6 +40,7 @@ export class Api {
this.httpdConfig.get("/get", this.pathGet.bind(this)); this.httpdConfig.get("/get", this.pathGet.bind(this));
this.httpdConfig.get("/list", this.pathList.bind(this)); this.httpdConfig.get("/list", this.pathList.bind(this));
this.httpdConfig.post("/create", this.pathCreate.bind(this)); this.httpdConfig.post("/create", this.pathCreate.bind(this));
this.httpdConfig.post("/join", this.pathJoin.bind(this));
this.httpdConfig.listen(port); this.httpdConfig.listen(port);
} }
@ -92,9 +93,15 @@ export class Api {
return; return;
} }
const roomId = request.body["roomId"];
if (roomId === undefined) {
response.status(400).send("invalid request");
return;
}
const userId = await this.resolveAccessToken(accessToken); const userId = await this.resolveAccessToken(accessToken);
if (userId === null) { if (userId === null) {
response.status(4401).send("unauthorised"); response.status(401).send("unauthorised");
return; return;
} }
@ -106,4 +113,41 @@ export class Api {
response.status(200).json({ mxid: mjolnirId, roomId: managementRoom }); 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({});
}
} }

View File

@ -27,7 +27,7 @@ import { Api } from "./Api";
export class MjolnirAppService { export class MjolnirAppService {
public readonly bridge: Bridge; public readonly bridge: Bridge;
private readonly mjolnirManager: MjolnirManager = new MjolnirManager(); public readonly mjolnirManager: MjolnirManager = new MjolnirManager();
public constructor() { public constructor() {
new Api("http://localhost:8081", this).start(9001); new Api("http://localhost:8081", this).start(9001);

View File

@ -5,11 +5,12 @@ import { SHORTCODE_EVENT_TYPE } from "../models/PolicyList";
import { Permalinks, MatrixClient } from "matrix-bot-sdk"; import { Permalinks, MatrixClient } from "matrix-bot-sdk";
export class MjolnirManager { export class MjolnirManager {
private readonly mjolnirs: Map</*the user id of the mjolnir*/string, ManagedMjolnir> = new Map(); public readonly mjolnirs: Map</*the user id of the mjolnir*/string, ManagedMjolnir> = new Map();
public getDefaultMjolnirConfig(managementRoom: string): IConfig { public getDefaultMjolnirConfig(managementRoom: string): IConfig {
let config = configRead(); let config = configRead();
config.managementRoom = managementRoom; config.managementRoom = managementRoom;
config.protectedRooms = [];
return config; 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) { public async moveMeSomewhereCommonAndStopImplementingFunctionalityOnACommandFirstBasis(mjolnirOwnerId: string, shortcode: string) {
const powerLevels: { [key: string]: any } = { const powerLevels: { [key: string]: any } = {
"ban": 50, "ban": 50,