summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/vanity-url.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-19 21:28:57 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-19 21:28:57 +0200
commitec83db3d4e11552eff7c0de83a9c2c404c0bb72c (patch)
treebc5315e18ec6147a76a5bf8b90f1342378205213 /api/src/routes/guilds/#guild_id/vanity-url.ts
parentadded all unittests for attachments (diff)
downloadserver-ec83db3d4e11552eff7c0de83a9c2c404c0bb72c.tar.xz
:bug: fix vanity url
Diffstat (limited to 'api/src/routes/guilds/#guild_id/vanity-url.ts')
-rw-r--r--api/src/routes/guilds/#guild_id/vanity-url.ts30
1 files changed, 23 insertions, 7 deletions
diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts
index 86adfeb0..1e659d8d 100644
--- a/api/src/routes/guilds/#guild_id/vanity-url.ts
+++ b/api/src/routes/guilds/#guild_id/vanity-url.ts
@@ -1,4 +1,4 @@
-import { getPermission, GuildModel, InviteModel, trimSpecial } from "@fosscord/util";
+import { ChannelModel, ChannelType, getPermission, GuildModel, InviteModel, trimSpecial } from "@fosscord/util";
 import { Router, Request, Response } from "express";
 import { HTTPError } from "lambert-server";
 import { check, Length } from "../../../util/instanceOf";
@@ -11,11 +11,14 @@ const InviteRegex = /\W/g;
 router.get("/", async (req: Request, res: Response) => {
 	const { guild_id } = req.params;
 
-	await isMember(req.user_id, guild_id);
+	const permission = await getPermission(req.user_id, guild_id);
+	permission.hasThrow("MANAGE_GUILD");
+
 	const guild = await GuildModel.findOne({ id: guild_id }).exec();
-	if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204);
+	if (!guild.vanity_url_code) return res.json({ code: null });
+	const { uses } = await InviteModel.findOne({ code: guild.vanity_url_code }).exec();
 
-	return res.json({ code: guild.vanity_url.code });
+	return res.json({ code: guild.vanity_url_code, uses });
 });
 
 // TODO: check if guild is elgible for vanity url
@@ -24,11 +27,12 @@ router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Reques
 	var code = req.body.code.replace(InviteRegex);
 	if (!code) code = null;
 
-	const permission = await getPermission(req.user_id, guild_id);
+	const guild = await GuildModel.findOne({ id: guild_id }).exec();
+	const permission = await getPermission(req.user_id, guild_id, undefined, { guild });
 	permission.hasThrow("MANAGE_GUILD");
 
 	const alreadyExists = await Promise.all([
-		GuildModel.findOne({ "vanity_url.code": code })
+		GuildModel.findOne({ vanity_url_code: code })
 			.exec()
 			.catch(() => null),
 		InviteModel.findOne({ code: code })
@@ -37,7 +41,19 @@ router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Reques
 	]);
 	if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400);
 
-	await GuildModel.updateOne({ id: guild_id }, { "vanity_url.code": code }).exec();
+	await GuildModel.updateOne({ id: guild_id }, { vanity_url_code: code }).exec();
+	const { id } = await ChannelModel.findOne({ guild_id, type: ChannelType.GUILD_TEXT }).exec();
+	await InviteModel.updateOne(
+		{ code: guild.vanity_url_code },
+		{
+			code: code,
+			uses: 0,
+			created_at: new Date(),
+			guild_id,
+			channel_id: id
+		},
+		{ upsert: true }
+	).exec();
 
 	return res.json({ code: code });
 });