summary refs log tree commit diff
path: root/src/routes
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-11 01:25:09 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-11 01:25:09 +0200
commit99cb4c048b627f9faa49c17d07f46c92b948e2af (patch)
tree10897f18b9c141e023b2c392cc8dde2428407519 /src/routes
parent:bug: fix bans (diff)
downloadserver-99cb4c048b627f9faa49c17d07f46c92b948e2af.tar.xz
:bug: fix guild channel position update
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/guilds/#guild_id/channels.ts41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/routes/guilds/#guild_id/channels.ts b/src/routes/guilds/#guild_id/channels.ts
index 15cc7394..f9a48999 100644
--- a/src/routes/guilds/#guild_id/channels.ts
+++ b/src/routes/guilds/#guild_id/channels.ts
@@ -7,7 +7,8 @@ import {
 	Snowflake,
 	toObject,
 	ChannelUpdateEvent,
-	AnyChannel
+	AnyChannel,
+	getPermission
 } from "@fosscord/server-util";
 import { HTTPError } from "lambert-server";
 import { ChannelModifySchema } from "../../../schema/Channel";
@@ -25,7 +26,9 @@ router.get("/", async (req: Request, res: Response) => {
 
 // TODO: check if channel type is permitted
 // TODO: check if parent_id exists
+
 router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
+	// creates a new guild channel https://discord.com/developers/docs/resources/guild#create-guild-channel
 	const { guild_id } = req.params;
 	const body = req.body as ChannelModifySchema;
 
@@ -35,16 +38,36 @@ router.post("/", check(ChannelModifySchema), async (req: Request, res: Response)
 });
 
 // TODO: check if parent_id exists
-router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
-	const { guild_id } = req.params;
-	const body = req.body as ChannelModifySchema;
+router.patch(
+	"/",
+	check({ id: String, $position: Number, $lock_permissions: Boolean, $parent_id: String }),
+	async (req: Request, res: Response) => {
+		// changes guild channel position
+		const { guild_id } = req.params;
+		const body = req.body as { id: string; position?: number; lock_permissions?: boolean; parent_id?: string };
+		body.position = Math.floor(body.position || 0);
+		if (!body.position && !body.parent_id) throw new HTTPError(`You need to at least specify position or parent_id`, 400);
 
-	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
-	const channel = await ChannelModel.findOneAndUpdate({ guild_id }, body).exec();
+		const permission = await getPermission(req.user_id, guild_id);
+		permission.hasThrow("MANAGE_CHANNELS");
 
-	await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent);
+		const opts: any = {};
+		if (body.position) opts.position = body.position;
 
-	res.json(toObject(channel));
-});
+		if (body.parent_id) {
+			opts.parent_id = body.parent_id;
+			const parent_channel = await ChannelModel.findOne({ id: body.parent_id, guild_id }, { permission_overwrites: true }).exec();
+			if (body.lock_permissions) {
+				opts.permission_overwrites = parent_channel.permission_overwrites;
+			}
+		}
+
+		const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts).exec();
+
+		await emitEvent({ event: "CHANNEL_UPDATE", data: channel, channel_id: body.id } as ChannelUpdateEvent);
+
+		res.json(toObject(channel));
+	}
+);
 
 export default router;