bridgecord/routes/api.js

168 lines
5.7 KiB
JavaScript
Raw Normal View History

import express from "express";
2022-02-15 14:24:46 +02:00
import { guildMap, logger } from "../common.js";
import { dangerousAdminMode } from "../config.js";
import { checkAuth, createToken } from "../tokens.js";
2022-02-15 14:24:46 +02:00
const error = logger("error", "API");
const router = express();
router.get("/", (req, res) => {
res.status(200).send({ error: false, message: "SUCCESS_API_OK" });
});
router.post("/tokens/create", async (req, res) => {
if (!dangerousAdminMode)
return res.status(403).send({ error: true, message: "ERROR_FEATURE_DISABLED" });
const { username, avatarURL, discordID, guildAccess, isSuperToken=false } = req.body;
if (!username || !discordID || !guildAccess)
return res.status(400).send({ error: true, message: "ERROR_BAD_REQUEST" });
try {
const token = await createToken({ username, avatarURL, discordID, guildAccess, isSuperToken });
res.status(200).send({ error: false, message: "SUCCESS_TOKEN_CREATED", token });
} catch(e) {
res.status(500).send({ error: true, message: "ERROR_TOKEN_CREATE_FAILURE" });
}
});
2022-02-02 20:24:14 +02:00
router.get("/users/@self", checkAuth(async (req, res) => {
res.status(200).send({ error: false, user: {
username: req.user.username,
avatarURL: req.user.avatarURL,
discordID: req.user.discordID,
guildAccess: req.user.guildAccess,
isSuperToken: req.user.isSuperToken
2022-02-02 20:24:14 +02:00
}});
}));
router.post("/guilds/:guildId/channels/:channelId/messages/create", checkAuth(async (req, res) => {
const messageContent = req.body.content;
if (!messageContent)
return res.status(400).send({ error: true, message: "ERROR_NO_MESSAGE_CONTENT" });
const guildId = req.params.guildId;
if (!guildId)
return res.status(400).send({ error: true, message: "ERROR_NO_GUILD_ID" });
const channelId = req.params.channelId;
if (!channelId)
return res.status(400).send({ error: true, message: "ERROR_NO_CHANNEL_ID" });
let { username, avatarURL, guildAccess, isSuperToken } = req.user;
if (isSuperToken) {
if (req.body.username)
username = req.body.username;
if (req.body.avatarURL)
avatarURL = req.body.avatarURL;
}
if (guildAccess.indexOf(guildId) === -1)
return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" });
const guild = guildMap.get(guildId);
if (!guild)
return res.status(404).send({ error: true, message: "ERROR_GUILD_NOT_FOUND" });
try {
await guild.discordSendMessage(messageContent, channelId, username, avatarURL);
2022-02-02 20:24:14 +02:00
res.status(201).send({ error: false });
} catch(e) {
2022-02-15 14:24:46 +02:00
error("[message create] [error]", e);
res.status(500).send({ error: true, message: "ERROR_MESSAGE_SEND_FAILURE" });
}
}));
router.get("/guilds/:guildId/channels", checkAuth(async (req, res) => {
const guildId = req.params.guildId;
if (!guildId)
return res.status(400).send({ error: true, message: "ERROR_NO_GUILD_ID" });
const { guildAccess } = req.user;
if (guildAccess.indexOf(guildId) === -1)
return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" });
const guild = guildMap.get(guildId);
try {
res.status(200).send({ error: false, channels: guild.userFacingChannelList() });
} catch(e) {
2022-02-15 14:24:46 +02:00
error("[guild get channels] [error]", e);
res.status(500).send({ error: true, message: "ERROR_CHANNELS_FETCH_FAILURE" });
}
}));
2022-02-02 20:24:14 +02:00
router.get("/guilds/:guildId", checkAuth(async (req, res) => {
const guildId = req.params.guildId;
if (!guildId)
return res.status(400).send({ error: true, message: "ERROR_NO_GUILD_ID" });
const { guildAccess } = req.user;
if (guildAccess.indexOf(guildId) === -1)
return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" });
const guild = guildMap.get(guildId);
try {
res.status(200).send({ error: false, guild: guild.guildObject });
} catch(e) {
2022-02-15 14:24:46 +02:00
error("[guild get info]", e);
2022-02-02 20:24:14 +02:00
res.status(500).send({ error: true, message: "ERROR_GUILD_INFO_FETCH_FAILURE" });
}
}));
router.get("/users/@self/guilds", checkAuth(async (req, res) => {
const { guildAccess } = req.user;
res.status(200).send({ error: false, guilds: guildAccess.map(e => guildMap.get(e)?.guildObject).filter(e => !!e) });
2022-02-02 20:24:14 +02:00
}));
router.get("/guilds/:guildId/events/poll", checkAuth(async (req, res) => {
const guildId = req.params.guildId;
if (!guildId)
return res.status(400).send({ error: true, message: "ERROR_NO_GUILD_ID" });
const { guildAccess } = req.user;
if (guildAccess.indexOf(guildId) === -1)
return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" });
const guild = guildMap.get(guildId);
try {
2022-02-02 20:25:09 +02:00
guild.holdForEvent(15000)
2022-02-02 20:24:14 +02:00
.then(result => res.status(200).send({ error: false, event: result }));
} catch(e) {
2022-02-15 14:24:46 +02:00
error("[guild poll events]", e);
res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" });
}
}));
router.get("/events/poll", checkAuth(async (req, res) => {
const { guildAccess } = req.user;
try {
// note: probably not very optimal
const promises = [];
guildAccess.forEach((guildId) => {
const guild = guildMap.get(guildId);
if (guild) {
promises.push(guild.holdForEvent(15000));
}
});
// note: if 2 guild events happen at the same time, one of them may be dropped
Promise.race(promises)
.then(e => {
res.status(200).send({ error: false, event: e })
});
} catch(e) {
2022-02-15 14:24:46 +02:00
error("[guilds poll events]", e);
res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" });
}
}));
export default router;