2019-06-22 18:52:24 +03:00
|
|
|
import express from "express";
|
|
|
|
import passport from "passport";
|
|
|
|
import { AllowedGuilds } from "../data/AllowedGuilds";
|
2019-06-23 19:18:41 +03:00
|
|
|
import { ApiPermissions } from "../data/ApiPermissions";
|
2019-07-11 12:23:57 +03:00
|
|
|
import { clientError, error, ok, serverError, unauthorized } from "./responses";
|
2019-06-23 03:40:53 +03:00
|
|
|
import { Configs } from "../data/Configs";
|
2019-06-23 19:18:41 +03:00
|
|
|
import { ApiRoles } from "../data/ApiRoles";
|
2019-07-11 12:23:57 +03:00
|
|
|
import { validateGuildConfig } from "../configValidator";
|
|
|
|
import yaml, { YAMLException } from "js-yaml";
|
2019-07-22 00:49:05 +03:00
|
|
|
import { apiTokenAuthHandlers } from "./auth";
|
2019-06-22 18:52:24 +03:00
|
|
|
|
|
|
|
export function initGuildsAPI(app: express.Express) {
|
|
|
|
const allowedGuilds = new AllowedGuilds();
|
2019-06-23 19:18:41 +03:00
|
|
|
const apiPermissions = new ApiPermissions();
|
2019-06-23 03:40:53 +03:00
|
|
|
const configs = new Configs();
|
2019-06-22 18:52:24 +03:00
|
|
|
|
2019-07-22 00:49:05 +03:00
|
|
|
app.get("/guilds/available", ...apiTokenAuthHandlers(), async (req, res) => {
|
2019-06-23 19:18:41 +03:00
|
|
|
const guilds = await allowedGuilds.getForApiUser(req.user.userId);
|
2019-06-23 03:40:53 +03:00
|
|
|
res.json(guilds);
|
2019-06-22 18:52:24 +03:00
|
|
|
});
|
2019-06-23 03:40:53 +03:00
|
|
|
|
2019-07-22 00:49:05 +03:00
|
|
|
app.get("/guilds/:guildId/config", ...apiTokenAuthHandlers(), async (req, res) => {
|
2019-06-23 19:18:41 +03:00
|
|
|
const permissions = await apiPermissions.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
|
|
|
if (!permissions) return unauthorized(res);
|
2019-06-23 03:40:53 +03:00
|
|
|
|
|
|
|
const config = await configs.getActiveByKey(`guild-${req.params.guildId}`);
|
|
|
|
res.json({ config: config ? config.config : "" });
|
|
|
|
});
|
|
|
|
|
2019-07-22 00:49:05 +03:00
|
|
|
app.post("/guilds/:guildId/config", ...apiTokenAuthHandlers(), async (req, res) => {
|
2019-06-23 19:18:41 +03:00
|
|
|
const permissions = await apiPermissions.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
|
|
|
if (!permissions || ApiRoles[permissions.role] < ApiRoles.Editor) return unauthorized(res);
|
2019-06-23 03:40:53 +03:00
|
|
|
|
2019-07-22 00:14:24 +03:00
|
|
|
let config = req.body.config;
|
2019-06-23 03:40:53 +03:00
|
|
|
if (config == null) return clientError(res, "No config supplied");
|
|
|
|
|
2019-07-22 00:14:24 +03:00
|
|
|
config = config.trim() + "\n"; // Normalize start/end whitespace in the config
|
|
|
|
|
|
|
|
const currentConfig = await configs.getActiveByKey(`guild-${req.params.guildId}`);
|
|
|
|
if (config === currentConfig.config) {
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:23:57 +03:00
|
|
|
// Validate config
|
|
|
|
let parsedConfig;
|
|
|
|
try {
|
|
|
|
parsedConfig = yaml.safeLoad(config);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof YAMLException) {
|
|
|
|
return error(res, e.message, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error("Error when loading YAML: " + e.message);
|
|
|
|
return serverError(res, "Server error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedConfig == null) {
|
|
|
|
parsedConfig = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const errors = validateGuildConfig(parsedConfig);
|
|
|
|
if (errors) {
|
|
|
|
return res.status(422).json({ errors });
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:40:53 +03:00
|
|
|
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
|
|
|
ok(res);
|
|
|
|
});
|
2019-06-22 18:52:24 +03:00
|
|
|
}
|