summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--api/src/routes/guilds/#guild_id/vanity-url.ts30
-rw-r--r--api/src/routes/guilds/index.ts2
-rw-r--r--api/src/routes/invites/index.ts16
-rw-r--r--util/src/models/Guild.ts36
4 files changed, 51 insertions, 33 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 });
 });
diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts
index e0cb3325..92feed4e 100644
--- a/api/src/routes/guilds/index.ts
+++ b/api/src/routes/guilds/index.ts
@@ -53,7 +53,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 		system_channel_flags: 0,
 		system_channel_id: undefined,
 		unavailable: false,
-		vanity_url: undefined,
+		vanity_url_code: undefined,
 		verification_level: 0,
 		welcome_screen: {
 			enabled: false,
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) });
 });
 
diff --git a/util/src/models/Guild.ts b/util/src/models/Guild.ts
index 13a7d078..a5dcd8e3 100644
--- a/util/src/models/Guild.ts
+++ b/util/src/models/Guild.ts
@@ -32,7 +32,7 @@ export interface Guild {
 	// channels: GuildChannel[]; // * Channels are stored in a seperate collection
 	// emojis: Emoji[];  // * Emojis are stored in a seperate collection
 	// voice_states: []; // * voice_states are stored in a seperate collection
-    //TODO:
+	//TODO:
 	presences?: object[];
 	mfa_level?: number;
 	name: string;
@@ -47,19 +47,17 @@ export interface Guild {
 	system_channel_flags?: number;
 	system_channel_id?: string;
 	unavailable?: boolean;
-	vanity_url?: {
-		code: string;
-		uses: number;
-	};
+	vanity_url_code?: string;
 	verification_level?: number;
 	welcome_screen: {
 		enabled: boolean;
 		description: string;
-		welcome_channels: { 
-		description: string;
-	    emoji_id?: string;
-	    emoji_name: string;
-	    channel_id: string }[];
+		welcome_channels: {
+			description: string;
+			emoji_id?: string;
+			emoji_name: string;
+			channel_id: string;
+		}[];
 	};
 	widget_channel_id?: string;
 	widget_enabled?: boolean;
@@ -97,20 +95,20 @@ export const GuildSchema = new Schema({
 	system_channel_flags: Number,
 	system_channel_id: String,
 	unavailable: Boolean,
-	vanity_url: {
-		code: String,
-		uses: Number
-	},
+	vanity_url_code: String,
 	verification_level: Number,
 	voice_states: { type: [Object], default: [] },
 	welcome_screen: {
 		enabled: Boolean,
 		description: String,
-		welcome_channels: [{ 
-		description: String,
-	    emoji_id: String,
-	    emoji_name: String,
-	    channel_id: String }],
+		welcome_channels: [
+			{
+				description: String,
+				emoji_id: String,
+				emoji_name: String,
+				channel_id: String,
+			},
+		],
 	},
 	widget_channel_id: String,
 	widget_enabled: Boolean,