summary refs log tree commit diff
path: root/api/src/routes/invites/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/invites/index.ts')
-rw-r--r--api/src/routes/invites/index.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts
new file mode 100644
index 00000000..8c04713c
--- /dev/null
+++ b/api/src/routes/invites/index.ts
@@ -0,0 +1,44 @@
+import { Router, Request, Response } from "express";
+import { getPermission, InviteModel, toObject } from "@fosscord/server-util";
+import { HTTPError } from "lambert-server";
+import { addMember } from "../../util/Member";
+const router: Router = Router();
+
+router.get("/:code", async (req: Request, res: Response) => {
+	const { code } = req.params;
+
+	const invite = await InviteModel.findOne({ code }).exec();
+	if (!invite) throw new HTTPError("Unknown Invite", 404);
+
+	res.status(200).send(toObject(invite));
+});
+
+router.post("/:code", async (req: Request, res: Response) => {
+	const { code } = req.params;
+
+	const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }).exec();
+	if (!invite) throw new HTTPError("Unknown Invite", 404);
+
+	await addMember(req.user_id, invite.guild_id);
+
+	res.status(200).send(toObject(invite));
+});
+
+router.delete("/:code", async (req: Request, res: Response) => {
+	const { code } = req.params;
+	const invite = await InviteModel.findOne({ code }).exec();
+
+	if (!invite) throw new HTTPError("Unknown Invite", 404);
+
+	const { guild_id, channel_id } = invite;
+	const perms = await getPermission(req.user_id, guild_id, channel_id);
+
+	if (!perms.has("MANAGE_GUILD") && !perms.has("MANAGE_CHANNELS"))
+		throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401);
+
+	await InviteModel.deleteOne({ code }).exec();
+
+	res.status(200).send({ invite: toObject(invite) });
+});
+
+export default router;