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-03 05:03:11 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-03 05:03:11 +0200
commit5b39b11e5f18e53cb234b4af3aa882fc99f8c407 (patch)
tree21f871ea25468ad3f56aa43c13a637bd16070317 /api/src/routes/guilds
parent:see_no_evil: add database.db to .gitignore (diff)
downloadserver-5b39b11e5f18e53cb234b4af3aa882fc99f8c407.tar.xz
:sparkles: guild position update
Diffstat (limited to 'api/src/routes/guilds')
-rw-r--r--api/src/routes/guilds/#guild_id/members/#member_id/index.ts4
-rw-r--r--api/src/routes/guilds/#guild_id/roles.ts41
2 files changed, 37 insertions, 8 deletions
diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts
index 8b74a524..aef1c6dc 100644
--- a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts
+++ b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -33,10 +33,10 @@ router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response)
 	const permission = await getPermission(req.user_id, guild_id);
 
 	if (body.roles) {
+		permission.hasThrow("MANAGE_ROLES");
+
 		const roles = await Role.find({ id: In(body.roles) });
 		if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404);
-
-		permission.hasThrow("MANAGE_ROLES");
 	}
 
 	const member = await Member.findOneOrFail({ id: member_id, guild_id });
diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts
index 20ab0779..bdfc370e 100644
--- a/api/src/routes/guilds/#guild_id/roles.ts
+++ b/api/src/routes/guilds/#guild_id/roles.ts
@@ -13,8 +13,9 @@ import {
 import { HTTPError } from "lambert-server";
 
 import { check } from "../../../util/instanceOf";
-import { RoleModifySchema } from "../../../schema/Roles";
+import { RoleModifySchema, RolePositionUpdateSchema } from "../../../schema/Roles";
 import { DiscordApiErrors } from "../../../util/Constants";
+import { In } from "typeorm";
 
 const router: Router = Router();
 
@@ -40,20 +41,21 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) =>
 
 	if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles);
 
-	const role = {
+	const role = new Role({
+		// values before ...body are default and can be overriden
 		position: 0,
 		hoist: false,
-		color: 0, // default value
+		color: 0,
+		mentionable: false,
 		...body,
-		id: Snowflake.generate(),
 		guild_id: guild_id,
 		managed: false,
 		permissions: String(perms.bitfield & (body.permissions || 0n)),
 		tags: undefined
-	};
+	});
 
 	await Promise.all([
-		new Role(role).save(),
+		role.save(),
 		emitEvent({
 			event: "GUILD_ROLE_CREATE",
 			guild_id,
@@ -119,4 +121,31 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
 	res.json(role);
 });
 
+router.patch("/", check(RolePositionUpdateSchema), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+	const body = req.body as RolePositionUpdateSchema;
+
+	const perms = await getPermission(req.user_id, guild_id);
+	perms.hasThrow("MANAGE_ROLES");
+
+	await Promise.all(body.map(async (x) => Role.update({ guild_id, id: x.id }, { position: x.position })));
+
+	const roles = await Role.find({ guild_id, id: In(body.map((x) => x.id)) });
+
+	await Promise.all(
+		roles.map((x) =>
+			emitEvent({
+				event: "GUILD_ROLE_UPDATE",
+				guild_id,
+				data: {
+					guild_id,
+					role: x
+				}
+			} as GuildRoleUpdateEvent)
+		)
+	);
+
+	res.json(roles);
+});
+
 export default router;