summary refs log tree commit diff
path: root/api/src/routes/guilds
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-01 11:27:52 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-01 11:27:52 +0200
commit7ff5e1488719c78028f5db50988fe2da198ad51a (patch)
tree6da300a967a42756c3982c1862a76bea55b1addc /api/src/routes/guilds
parentfix #295 (diff)
downloadserver-7ff5e1488719c78028f5db50988fe2da198ad51a.tar.xz
:sparkles: use DiscordApiErrors and check limits
Diffstat (limited to 'api/src/routes/guilds')
-rw-r--r--api/src/routes/guilds/#guild_id/roles.ts45
-rw-r--r--api/src/routes/guilds/index.ts3
-rw-r--r--api/src/routes/guilds/templates/index.ts3
3 files changed, 32 insertions, 19 deletions
diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts

index f6ac8caa..c3dd92dc 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts
@@ -7,12 +7,14 @@ import { GuildRoleCreateEvent, GuildRoleUpdateEvent, GuildRoleDeleteEvent, - emitEvent + emitEvent, + Config } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; import { RoleModifySchema } from "../../../schema/Roles"; +import { DiscordApiErrors } from "../../../util/Constants"; const router: Router = Router(); @@ -33,24 +35,34 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = await new Role({ + const role_count = await Role.count({ guild_id }); + const { maxRoles } = Config.get().limits.guild; + + if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles); + + const role = { + position: 0, + hoist: false, + color: 0, // default value ...body, id: Snowflake.generate(), guild_id: guild_id, managed: false, - position: 0, - tags: null, - permissions: String(perms.bitfield & (body.permissions || 0n)) - }).save(); - - await emitEvent({ - event: "GUILD_ROLE_CREATE", - guild_id, - data: { + permissions: String(perms.bitfield & (body.permissions || 0n)), + tags: undefined + }; + + await Promise.all([ + Role.insert(role), + emitEvent({ + event: "GUILD_ROLE_CREATE", guild_id, - role: role - } - } as GuildRoleCreateEvent); + data: { + guild_id, + role: role + } + } as GuildRoleCreateEvent) + ]); res.json(role); }); @@ -84,14 +96,13 @@ router.delete("/:role_id", async (req: Request, res: Response) => { // TODO: check role hierarchy router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { role_id } = req.params; + const { role_id, guild_id } = req.params; const body = req.body as RoleModifySchema; const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = new Role({ ...body, id: role_id, guild_id, permissions: perms.bitfield & (body.permissions || 0n) }); + const role = new Role({ ...body, id: role_id, guild_id, permissions: String(perms.bitfield & (body.permissions || 0n)) }); await Promise.all([ role.save(), diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts
index e4157384..a54b83ba 100644 --- a/api/src/routes/guilds/index.ts +++ b/api/src/routes/guilds/index.ts
@@ -3,6 +3,7 @@ import { Role, Guild, Snowflake, Config, User, Member, Channel } from "@fosscord import { HTTPError } from "lambert-server"; import { check } from "./../../util/instanceOf"; import { GuildCreateSchema } from "../../schema/Guild"; +import { DiscordApiErrors } from "../../util/Constants"; const router: Router = Router(); @@ -14,7 +15,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = const { maxGuilds } = Config.get().limits.user; const guild_count = await Member.count({ id: req.user_id }); if (guild_count >= maxGuilds) { - throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); + throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); } const guild_id = Snowflake.generate(); diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts
index 7a8ac886..3a619278 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts
@@ -4,6 +4,7 @@ import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscor import { HTTPError } from "lambert-server"; import { GuildTemplateCreateSchema } from "../../../schema/Guild"; import { check } from "../../../util/instanceOf"; +import { DiscordApiErrors } from "../../../util/Constants"; router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; @@ -21,7 +22,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res const guild_count = await Member.count({ id: req.user_id }); if (guild_count >= maxGuilds) { - throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); + throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); } const template = await Template.findOneOrFail({ code: code });