2021-06-24 10:09:10 +02:00
|
|
|
import { BaseController } from "./BaseController";
|
|
|
|
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
|
|
|
|
import { ADMIN_API_TOKEN } from "../Enum/EnvironmentVariable";
|
|
|
|
import { apiClientRepository } from "../Services/ApiClientRepository";
|
|
|
|
import {
|
|
|
|
AdminRoomMessage,
|
|
|
|
WorldFullWarningToRoomMessage,
|
|
|
|
RefreshRoomPromptMessage,
|
|
|
|
} from "../Messages/generated/messages_pb";
|
|
|
|
|
|
|
|
export class AdminController extends BaseController {
|
|
|
|
constructor(private App: TemplatedApp) {
|
2021-03-01 17:47:00 +01:00
|
|
|
super();
|
|
|
|
this.App = App;
|
|
|
|
this.receiveGlobalMessagePrompt();
|
2021-04-01 16:43:12 +02:00
|
|
|
this.receiveRoomEditionPrompt();
|
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
|
2021-04-01 16:43:12 +02:00
|
|
|
receiveRoomEditionPrompt() {
|
|
|
|
this.App.options("/room/refresh", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
this.App.post("/room/refresh", async (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
res.onAborted(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("/message request was aborted");
|
|
|
|
});
|
2021-04-01 16:43:12 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
const token = req.getHeader("admin-token");
|
2021-04-01 16:43:12 +02:00
|
|
|
const body = await res.json();
|
|
|
|
|
|
|
|
if (token !== ADMIN_API_TOKEN) {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.error("Admin access refused for token: " + token);
|
|
|
|
res.writeStatus("401 Unauthorized").end("Incorrect token");
|
2021-04-01 16:43:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof body.roomId !== "string") {
|
|
|
|
throw "Incorrect roomId parameter";
|
2021-04-01 16:43:12 +02:00
|
|
|
}
|
|
|
|
const roomId: string = body.roomId;
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
await apiClientRepository.getClient(roomId).then((roomClient) => {
|
2021-04-01 16:43:12 +02:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const roomMessage = new RefreshRoomPromptMessage();
|
|
|
|
roomMessage.setRoomid(roomId);
|
|
|
|
|
|
|
|
roomClient.sendRefreshRoomPrompt(roomMessage, (err) => {
|
|
|
|
err ? rej(err) : res();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
this.errorToResponse(err, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeStatus("200");
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end("ok");
|
2021-04-01 16:43:12 +02:00
|
|
|
});
|
2021-03-01 17:47:00 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
|
2021-03-01 17:47:00 +01:00
|
|
|
receiveGlobalMessagePrompt() {
|
|
|
|
this.App.options("/message", (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
this.App.post("/message", async (res: HttpResponse, req: HttpRequest) => {
|
|
|
|
res.onAborted(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("/message request was aborted");
|
|
|
|
});
|
2021-03-01 17:47:00 +01:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
const token = req.getHeader("admin-token");
|
2021-03-01 17:47:00 +01:00
|
|
|
const body = await res.json();
|
2021-06-24 10:09:10 +02:00
|
|
|
|
2021-03-01 17:47:00 +01:00
|
|
|
if (token !== ADMIN_API_TOKEN) {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.error("Admin access refused for token: " + token);
|
|
|
|
res.writeStatus("401 Unauthorized").end("Incorrect token");
|
2021-03-01 17:47:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof body.text !== "string") {
|
|
|
|
throw "Incorrect text parameter";
|
2021-03-01 17:47:00 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (body.type !== "capacity" && body.type !== "message") {
|
|
|
|
throw "Incorrect type parameter";
|
2021-03-11 16:14:34 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (!body.targets || typeof body.targets !== "object") {
|
|
|
|
throw "Incorrect targets parameter";
|
2021-03-01 17:47:00 +01:00
|
|
|
}
|
|
|
|
const text: string = body.text;
|
2021-03-11 16:14:34 +01:00
|
|
|
const type: string = body.type;
|
2021-03-01 17:47:00 +01:00
|
|
|
const targets: string[] = body.targets;
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
await Promise.all(
|
|
|
|
targets.map((roomId) => {
|
|
|
|
return apiClientRepository.getClient(roomId).then((roomClient) => {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
if (type === "message") {
|
|
|
|
const roomMessage = new AdminRoomMessage();
|
|
|
|
roomMessage.setMessage(text);
|
|
|
|
roomMessage.setRoomid(roomId);
|
|
|
|
|
|
|
|
roomClient.sendAdminMessageToRoom(roomMessage, (err) => {
|
|
|
|
err ? rej(err) : res();
|
|
|
|
});
|
|
|
|
} else if (type === "capacity") {
|
|
|
|
const roomMessage = new WorldFullWarningToRoomMessage();
|
|
|
|
roomMessage.setRoomid(roomId);
|
|
|
|
|
|
|
|
roomClient.sendWorldFullWarningToRoom(roomMessage, (err) => {
|
|
|
|
err ? rej(err) : res();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-03-01 17:47:00 +01:00
|
|
|
});
|
2021-06-24 10:09:10 +02:00
|
|
|
})
|
|
|
|
);
|
2021-03-01 17:47:00 +01:00
|
|
|
} catch (err) {
|
|
|
|
this.errorToResponse(err, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeStatus("200");
|
|
|
|
this.addCorsHeaders(res);
|
2021-06-24 10:09:10 +02:00
|
|
|
res.end("ok");
|
2021-03-01 17:47:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|