summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/vanity-url.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/guilds/#guild_id/vanity-url.ts')
-rw-r--r--api/src/routes/guilds/#guild_id/vanity-url.ts38
1 files changed, 16 insertions, 22 deletions
diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts
index 335cea27..58940b42 100644
--- a/api/src/routes/guilds/#guild_id/vanity-url.ts
+++ b/api/src/routes/guilds/#guild_id/vanity-url.ts
@@ -1,8 +1,7 @@
-import { Channel, ChannelType, getPermission, Guild, InviteModel, trimSpecial } from "@fosscord/util";
+import { Channel, ChannelType, getPermission, Guild, Invite, trimSpecial } from "@fosscord/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();
 
@@ -14,42 +13,37 @@ router.get("/", async (req: Request, res: Response) => {
 	const permission = await getPermission(req.user_id, guild_id);
 	permission.hasThrow("MANAGE_GUILD");
 
-	const guild = await Guild.findOneOrFail({ id: guild_id });
-	if (!guild.vanity_url_code) return res.json({ code: null });
-	const { uses } = await Invite.findOneOrFail({ code: guild.vanity_url_code });
+	const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] });
+	if (!guild.vanity_url) return res.json({ code: null });
 
-	return res.json({ code: guild.vanity_url_code, uses });
+	return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses });
 });
 
 // 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 code = req.body.code.replace(InviteRegex);
+
+	await Invite.findOneOrFail({ code });
 
 	const guild = await Guild.findOneOrFail({ id: guild_id });
-	const permission = await getPermission(req.user_id, guild_id, undefined, { guild });
+	const permission = await getPermission(req.user_id, guild_id);
 	permission.hasThrow("MANAGE_GUILD");
 
-	const alreadyExists = await Promise.all([
-		Guild.findOneOrFail({ vanity_url_code: code }).catch(() => null),
-		Invite.findOneOrFail({ code: code }).catch(() => null)
-	]);
-	if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400);
-
-	await Guild.update({ id: guild_id }, { vanity_url_code: code });
 	const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT });
-	await Invite.update(
-		{ code: guild.vanity_url_code },
-		{
+	guild.vanity_url_code = code;
+
+	Promise.all([
+		guild.save(),
+		Invite.delete({ code: guild.vanity_url_code }),
+		new Invite({
 			code: code,
 			uses: 0,
 			created_at: new Date(),
 			guild_id,
 			channel_id: id
-		},
-		{ upsert: true }
-	);
+		}).save()
+	]);
 
 	return res.json({ code: code });
 });