summary refs log tree commit diff
path: root/src/api/routes/guilds/templates
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-12-11 01:12:54 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-12-11 01:12:54 +0100
commit0a8ceb9e6349284e75545a01ffad608b020f78e2 (patch)
tree17a9163f963eddabf9168b0b630096b2f7535b64 /src/api/routes/guilds/templates
parentPrettier: use editorconfig (diff)
downloadserver-ts-dev/emma-refactors.tar.xz
Actually run prettier dev/emma-refactors
Diffstat (limited to 'src/api/routes/guilds/templates')
-rw-r--r--src/api/routes/guilds/templates/index.ts126
1 files changed, 59 insertions, 67 deletions
diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts

index 8f718a21..693c8dcb 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts
@@ -47,13 +47,11 @@ router.get( }, }), async (req: Request, res: Response) => { - const { allowDiscordTemplates, allowRaws, enabled } = - Config.get().templates; + const { allowDiscordTemplates, allowRaws, enabled } = Config.get().templates; if (!enabled) res.json({ code: 403, - message: - "Template creation & usage is disabled on this instance.", + message: "Template creation & usage is disabled on this instance.", }).sendStatus(403); const { code } = req.params; @@ -63,8 +61,7 @@ router.get( return res .json({ code: 403, - message: - "Discord templates cannot be used on this instance.", + message: "Discord templates cannot be used on this instance.", }) .sendStatus(403); const discordTemplateID = code.split("discord:", 2)[1]; @@ -74,7 +71,7 @@ router.get( { method: "get", headers: { "Content-Type": "application/json" }, - }, + } ); return res.json(await discordTemplateData.json()); } @@ -95,75 +92,70 @@ router.get( where: { code: code }, }); res.json(template); - }, + } ); -router.post( - "/:code", - route({ requestBody: "GuildTemplateCreateSchema" }), - async (req: Request, res: Response) => { - const { - enabled, - allowTemplateCreation, - // allowDiscordTemplates, - // allowRaws, - } = Config.get().templates; - if (!enabled) - return res - .json({ - code: 403, - message: - "Template creation & usage is disabled on this instance.", - }) - .sendStatus(403); - if (!allowTemplateCreation) - return res - .json({ - code: 403, - message: "Template creation is disabled on this instance.", - }) - .sendStatus(403); +router.post("/:code", route({ requestBody: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => { + const { + enabled, + allowTemplateCreation, + // allowDiscordTemplates, + // allowRaws, + } = Config.get().templates; + if (!enabled) + return res + .json({ + code: 403, + message: "Template creation & usage is disabled on this instance.", + }) + .sendStatus(403); + if (!allowTemplateCreation) + return res + .json({ + code: 403, + message: "Template creation is disabled on this instance.", + }) + .sendStatus(403); - const { code } = req.params; - const body = req.body as GuildTemplateCreateSchema; + const { code } = req.params; + const body = req.body as GuildTemplateCreateSchema; - const { maxGuilds } = Config.get().limits.user; + const { maxGuilds } = Config.get().limits.user; - const guild_count = await Member.count({ where: { id: req.user_id } }); - if (guild_count >= maxGuilds) { - throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); - } + const guild_count = await Member.count({ where: { id: req.user_id } }); + if (guild_count >= maxGuilds) { + throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); + } - const template = await Template.findOneOrFail({ - where: { code: code }, - }); + const template = await Template.findOneOrFail({ + where: { code: code }, + }); - const guild_id = Snowflake.generate(); + const guild_id = Snowflake.generate(); - const [guild] = await Promise.all([ - Guild.create({ - ...body, - ...template.serialized_source_guild, - id: guild_id, - owner_id: req.user_id, - }).save(), - Role.create({ - id: guild_id, - guild_id: guild_id, - color: 0, - hoist: false, - managed: true, - mentionable: true, - name: "@everyone", - permissions: BigInt("2251804225").toString(), // TODO: where did this come from? - position: 0, - }).save(), - ]); + const [guild] = await Promise.all([ + Guild.create({ + ...body, + ...template.serialized_source_guild, + id: guild_id, + owner_id: req.user_id, + }).save(), + Role.create({ + id: guild_id, + guild_id: guild_id, + color: 0, + hoist: false, + managed: true, + mentionable: true, + name: "@everyone", + permissions: BigInt("2251804225").toString(), // TODO: where did this come from? + position: 0, + }).save(), + ]); - await Member.addToGuild(req.user_id, guild_id); + await Member.addToGuild(req.user_id, guild_id); - res.status(201).json({ id: guild.id }); - }, -); + res.status(201).json({ id: guild.id }); +}); export default router;