From 008f2615e6eb5be09d6908a658c7973015f0f6be Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 4 Mar 2021 22:00:58 +0100 Subject: [PATCH] :sparkles: [Guild] channels --- src/routes/api/v8/guilds/#id/channels.ts | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/routes/api/v8/guilds/#id/channels.ts diff --git a/src/routes/api/v8/guilds/#id/channels.ts b/src/routes/api/v8/guilds/#id/channels.ts new file mode 100644 index 00000000..1316a2ca --- /dev/null +++ b/src/routes/api/v8/guilds/#id/channels.ts @@ -0,0 +1,51 @@ +import { Router } from "express"; +import { ChannelModel, ChannelType, GuildModel, Snowflake } from "fosscord-server-util"; +import { HTTPError } from "lambert-server"; +import { ChannelModifySchema } from "../../../../../schema/Channel"; +import { check } from "../../../../../util/instanceOf"; +const router = Router(); + +router.get("/", async (req, res) => { + const guild_id = BigInt(req.params.id); + const channels = await ChannelModel.find({ guild_id }).exec(); + + res.json(channels); +}); + +router.post("/", check(ChannelModifySchema), async (req, res) => { + const guild_id = BigInt(req.params.id); + const body = req.body as ChannelModifySchema; + if (!body.permission_overwrites) body.permission_overwrites = []; + if (!body.topic) body.topic = ""; + if (!body.rate_limit_per_user) body.rate_limit_per_user = 0; + switch (body.type) { + case ChannelType.DM: + case ChannelType.GROUP_DM: + throw new HTTPError("You can't create a dm channel in a guild"); + // TODO: + case ChannelType.GUILD_STORE: + throw new HTTPError("Not yet supported"); + case ChannelType.GUILD_NEWS: + // TODO: check if guild is community server + } + + if (body.parent_id) { + const exists = ChannelModel.findOne({ channel_id: body.parent_id }).exec(); + if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); + } + + const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); + if (!guild) throw new HTTPError("Guild not found", 4040); + + const channel = { + ...body, + id: Snowflake.generate(), + created_at: new Date(), + guild_id, + }; + await new ChannelModel(channel).save(); + + res.json(channel); +}); + +export default router;