summary refs log tree commit diff
path: root/api
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
commite2ea701920f528b09c410842fcfe57ec3e3ba875 (patch)
tree429a4a3426730aa2649356b4f91cb720561e0709 /api
parent:see_no_evil: add database.db to .gitignore (diff)
downloadserver-e2ea701920f528b09c410842fcfe57ec3e3ba875.tar.xz
:sparkles: guild position update
Diffstat (limited to 'api')
-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
-rw-r--r--api/src/schema/Roles.ts12
3 files changed, 49 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;
diff --git a/api/src/schema/Roles.ts b/api/src/schema/Roles.ts
index 1e5f560f..e1a34ae8 100644
--- a/api/src/schema/Roles.ts
+++ b/api/src/schema/Roles.ts
@@ -15,3 +15,15 @@ export interface RoleModifySchema {
 	mentionable?: boolean; // whether the role should be mentionable
 	position?: number;
 }
+
+export const RolePositionUpdateSchema = [
+	{
+		id: String,
+		position: Number
+	}
+];
+
+export type RolePositionUpdateSchema = {
+	id: string;
+	position: number;
+}[];