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.ts16
1 files changed, 10 insertions, 6 deletions
diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts
index b4066bd8..e7543dbb 100644
--- a/api/src/routes/invites/index.ts
+++ b/api/src/routes/invites/index.ts
@@ -1,5 +1,5 @@
 import { Router, Request, Response } from "express";
-import { getPermission, InviteModel, toObject } from "@fosscord/util";
+import { getPermission, GuildModel, InviteModel, toObject } from "@fosscord/util";
 import { HTTPError } from "lambert-server";
 import { addMember } from "../../util/Member";
 const router: Router = Router();
@@ -18,6 +18,7 @@ router.post("/:code", async (req: Request, res: Response) => {
 
 	const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec();
 	if (!invite) throw new HTTPError("Unknown Invite", 404);
+	if (invite.uses >= invite.max_uses) await InviteModel.deleteOne({ code });
 
 	await addMember(req.user_id, invite.guild_id);
 
@@ -27,17 +28,20 @@ router.post("/:code", async (req: Request, res: Response) => {
 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"))
+	const guild = await GuildModel.findOne({ id: guild_id }).exec();
+	const permission = await getPermission(req.user_id, guild_id, channel_id, { guild });
+
+	if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS"))
 		throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401);
 
 	await InviteModel.deleteOne({ code }).exec();
 
+	await GuildModel.updateOne({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } })
+		.exec()
+		.catch((e) => {});
+
 	res.status(200).send({ invite: toObject(invite) });
 });