summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-07 23:04:36 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-07 23:04:36 +0200
commit8daa6ddc23b0b354e1eba13313523b1bfb84e3fa (patch)
treea50917f8e833831b41df134ad84f82ab72e62340
parent:bug: fix post/patch channel (diff)
downloadserver-8daa6ddc23b0b354e1eba13313523b1bfb84e3fa.tar.xz
:bug: fix vanity url
-rw-r--r--src/routes/guilds/#guild_id/vanity-url.ts33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/routes/guilds/#guild_id/vanity-url.ts b/src/routes/guilds/#guild_id/vanity-url.ts
index 06311a28..323b2647 100644
--- a/src/routes/guilds/#guild_id/vanity-url.ts
+++ b/src/routes/guilds/#guild_id/vanity-url.ts
@@ -1,16 +1,45 @@
-import { GuildModel } from "@fosscord/server-util";
+import { getPermission, GuildModel, InviteModel, trimSpecial } from "@fosscord/server-util";
 import { Router, Request, Response } from "express";
 import { HTTPError } from "lambert-server";
+import { check, Length } from "../../../util/instanceOf";
+import { isMember } from "../../../util/Member";
 
 const router = Router();
 
+const InviteRegex = /\W/g;
+
 router.get("/", async (req: Request, res: Response) => {
 	const { guild_id } = req.params;
 
+	await isMember(req.user_id, guild_id);
 	const guild = await GuildModel.findOne({ id: guild_id }).exec();
 	if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204);
 
-	return res.json({ vanity_url: guild.vanity_url.code });
+	return res.json({ code: guild.vanity_url.code });
+});
+
+// TODO: check if guild is elgible for vanity url
+router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+	var code = req.body.code.replace(InviteRegex);
+	if (!code) code = null;
+
+	const permission = await getPermission(req.user_id, guild_id);
+	permission.hasThrow("MANAGE_GUILD");
+
+	const alreadyExists = await Promise.all([
+		GuildModel.findOne({ "vanity_url.code": code })
+			.exec()
+			.catch(() => null),
+		InviteModel.findOne({ code: code })
+			.exec()
+			.catch(() => null)
+	]);
+	if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400);
+
+	await GuildModel.updateOne({ id: guild_id }, { "vanity_url.code": code }).exec();
+
+	return res.json({ code: code });
 });
 
 export default router;