2022-02-02 11:46:42 +02:00
|
|
|
import express from "express";
|
2022-02-15 14:24:46 +02:00
|
|
|
import { guildMap, logger } from "../common.js";
|
2022-02-02 11:46:42 +02:00
|
|
|
import { dangerousAdminMode } from "../config.js";
|
|
|
|
import { checkAuth, createToken } from "../tokens.js";
|
|
|
|
|
2022-02-15 14:24:46 +02:00
|
|
|
const error = logger("error", "API");
|
|
|
|
|
2022-02-01 03:49:12 +02:00
|
|
|
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" });
|
|
|
|
|
2022-02-10 01:15:39 +02:00
|
|
|
const { username, avatarURL, discordID, guildAccess, isSuperToken=false } = req.body;
|
2022-02-01 03:49:12 +02:00
|
|
|
if (!username || !discordID || !guildAccess)
|
|
|
|
return res.status(400).send({ error: true, message: "ERROR_BAD_REQUEST" });
|
|
|
|
|
|
|
|
try {
|
2022-02-10 01:15:39 +02:00
|
|
|
const token = await createToken({ username, avatarURL, discordID, guildAccess, isSuperToken });
|
2022-02-01 03:49:12 +02:00
|
|
|
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,
|
2022-02-10 01:15:39 +02:00
|
|
|
guildAccess: req.user.guildAccess,
|
2022-02-11 15:20:59 +02:00
|
|
|
isSuperToken: req.user.isSuperToken
|
2022-02-02 20:24:14 +02:00
|
|
|
}});
|
|
|
|
}));
|
|
|
|
|
2022-02-01 03:49:12 +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" });
|
|
|
|
|
|
|
|
|
2022-02-10 01:15:39 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-02-01 03:49:12 +02:00
|
|
|
|
|
|
|
if (guildAccess.indexOf(guildId) === -1)
|
|
|
|
return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" });
|
|
|
|
|
|
|
|
const guild = guildMap.get(guildId);
|
2022-02-11 15:20:59 +02:00
|
|
|
|
|
|
|
if (!guild)
|
|
|
|
return res.status(404).send({ error: true, message: "ERROR_GUILD_NOT_FOUND" });
|
|
|
|
|
2022-02-01 03:49:12 +02:00
|
|
|
try {
|
|
|
|
await guild.discordSendMessage(messageContent, channelId, username, avatarURL);
|
2022-02-02 20:24:14 +02:00
|
|
|
res.status(201).send({ error: false });
|
2022-02-01 03:49:12 +02:00
|
|
|
} catch(e) {
|
2022-02-15 14:24:46 +02:00
|
|
|
error("[message create] [error]", e);
|
2022-02-01 03:49:12 +02:00
|
|
|
res.status(500).send({ error: true, message: "ERROR_MESSAGE_SEND_FAILURE" });
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
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);
|
2022-02-02 11:46:42 +02:00
|
|
|
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;
|
|
|
|
|
2022-02-07 16:14:43 +02:00
|
|
|
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
|
|
|
}));
|
|
|
|
|
2022-02-02 11:46:42 +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 }));
|
2022-02-02 11:46:42 +02:00
|
|
|
} catch(e) {
|
2022-02-15 14:24:46 +02:00
|
|
|
error("[guild poll events]", e);
|
2022-02-02 11:46:42 +02:00
|
|
|
res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" });
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-02-02 23:48:18 +02:00
|
|
|
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);
|
2022-02-02 23:48:18 +02:00
|
|
|
res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" });
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
export default router;
|