import express from "express"; import { guildMap, wait } from "../common.js"; import { dangerousAdminMode } from "../config.js"; import { checkAuth, createToken } from "../tokens.js"; 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 } = 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 }); res.status(200).send({ error: false, message: "SUCCESS_TOKEN_CREATED", token }); } catch(e) { res.status(500).send({ error: true, message: "ERROR_TOKEN_CREATE_FAILURE" }); } }); 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 }}); })); 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" }); const { username, avatarURL, 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 { await guild.discordSendMessage(messageContent, channelId, username, avatarURL); res.status(201).send({ error: false }); } catch(e) { console.error("server main: api: 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) { console.error("server main: api: guild get channels: error: ", e); res.status(500).send({ error: true, message: "ERROR_CHANNELS_FETCH_FAILURE" }); } })); 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) { console.error("server main: api: guild get info: error: ", e); 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) }); })); 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 { guild.holdForEvent(15000) .then(result => res.status(200).send({ error: false, event: result })); } catch(e) { console.error("server main: api: guild poll events: error: ", 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) { console.error("server main: api: guild poll events: error: ", e); res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" }); } })); export default router;