summary refs log tree commit diff
diff options
context:
space:
mode:
authorIntevel ツ <59223342+Intevel@users.noreply.github.com>2021-05-06 11:10:16 +0200
committerIntevel ツ <59223342+Intevel@users.noreply.github.com>2021-05-06 11:10:16 +0200
commitaeafd30b0132654784e2b25717e7feec748b768b (patch)
tree893b301e581f9c352946104b5cc76a5654db181b
parentUpdate Guild.ts (diff)
downloadserver-aeafd30b0132654784e2b25717e7feec748b768b.tar.xz
[Route] POST /guilds/template/:code
-rw-r--r--src/routes/guilds/templates/index.ts53
1 files changed, 52 insertions, 1 deletions
diff --git a/src/routes/guilds/templates/index.ts b/src/routes/guilds/templates/index.ts

index f1556e80..2584af73 100644 --- a/src/routes/guilds/templates/index.ts +++ b/src/routes/guilds/templates/index.ts
@@ -1,7 +1,12 @@ import { Request, Response, Router } from "express"; const router: Router = Router(); -import { TemplateModel, GuildModel, toObject, UserModel } from "@fosscord/server-util"; +import { TemplateModel, GuildModel, toObject, UserModel, RoleModel, Snowflake, Guild } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; +import { GuildTemplateCreateSchema } from "../../../schema/Guild"; +import { getPublicUser } from "../../../util/User"; +import { check } from "../../../util/instanceOf"; +import Config from "../../../util/Config"; +import { addMember } from "../../../util/Member"; router.get("/:template_id", async (req: Request, res: Response) => { @@ -18,4 +23,50 @@ router.get("/:template_id", async (req: Request, res: Response) => { res.json(toObject(template)).send(); }); +router.post("/:template_id", check(GuildTemplateCreateSchema), async (req: Request, res: Response) => { + const { template_id } = req.params; + const body = req.body as GuildTemplateCreateSchema; + + const { maxGuilds } = Config.get().limits.user; + const user = await getPublicUser(req.user_id, { guilds: true }); + + if (user.guilds.length >= maxGuilds) { + throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); + } + + if (!template_id) throw new HTTPError("Unknown template_id", 404); + + const template = await TemplateModel.findById({ _id: template_id }).exec(); + if (!template) throw new HTTPError("template not found", 404); + + const guild_id = Snowflake.generate(); + + const guild: Guild = { + ...body, + ...template.serialized_source_guild, + id: guild_id, + owner_id: req.user_id + }; + + await Promise.all([ + new GuildModel(guild).save(), + new RoleModel({ + id: guild_id, + guild_id: guild_id, + color: 0, + hoist: false, + managed: true, + mentionable: true, + name: "@everyone", + permissions: 2251804225n, + position: 0, + tags: null, + }).save(), + ]); + await addMember(req.user_id, guild_id, { guild }); + + res.status(201).json({ id: guild.id }); +}); + + export default router;