2022-02-02 11:46:42 +02:00
|
|
|
import express from "express";
|
|
|
|
import { guildMap, wait } from "../common.js";
|
|
|
|
import { dangerousAdminMode } from "../config.js";
|
|
|
|
import { checkAuth, createToken } from "../tokens.js";
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
}});
|
|
|
|
}));
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2022-02-02 20:24:14 +02:00
|
|
|
res.status(201).send({ error: false });
|
2022-02-01 03:49:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error("server main: api: message create: error: ", e);
|
|
|
|
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) {
|
|
|
|
console.error("server main: api: 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) {
|
|
|
|
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) });
|
|
|
|
}));
|
|
|
|
|
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) {
|
|
|
|
console.error("server main: api: guild poll events: error: ", e);
|
|
|
|
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) {
|
|
|
|
console.error("server main: api: guild poll events: error: ", e);
|
|
|
|
res.status(500).send({ error: true, message: "ERROR_POLL_FAILURE" });
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
export default router;
|