summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/members
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-12 20:09:35 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-12 20:09:35 +0200
commit08e837bf5559e9680fc8cb99bd05b93f8eb2cac5 (patch)
tree1eadc038773b025275d7b751265f741b09ca92ab /api/src/routes/guilds/#guild_id/members
parentnpm i @fosscord/server-util@1.3.52 (diff)
downloadserver-08e837bf5559e9680fc8cb99bd05b93f8eb2cac5.tar.xz
:sparkles: api
Diffstat (limited to 'api/src/routes/guilds/#guild_id/members')
-rw-r--r--api/src/routes/guilds/#guild_id/members/#member_id/index.ts69
-rw-r--r--api/src/routes/guilds/#guild_id/members/#member_id/nick.ts24
-rw-r--r--api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts27
-rw-r--r--api/src/routes/guilds/#guild_id/members/index.ts38
4 files changed, 158 insertions, 0 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
new file mode 100644
index 00000000..9a1676e6
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -0,0 +1,69 @@
+import { Request, Response, Router } from "express";
+import {
+	GuildModel,
+	MemberModel,
+	UserModel,
+	toObject,
+	GuildMemberAddEvent,
+	getPermission,
+	PermissionResolvable,
+	RoleModel,
+	GuildMemberUpdateEvent
+} from "@fosscord/server-util";
+import { HTTPError } from "lambert-server";
+import { addMember, isMember, removeMember } from "../../../../../util/Member";
+import { check } from "../../../../../util/instanceOf";
+import { MemberChangeSchema } from "../../../../../schema/Member";
+import { emitEvent } from "../../../../../util/Event";
+
+const router = Router();
+
+router.get("/", async (req: Request, res: Response) => {
+	const { guild_id, member_id } = req.params;
+	await isMember(req.user_id, guild_id);
+
+	const member = await MemberModel.findOne({ id: member_id, guild_id }).exec();
+
+	return res.json(toObject(member));
+});
+
+router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response) => {
+	const { guild_id, member_id } = req.params;
+	const body = req.body as MemberChangeSchema;
+	if (body.roles) {
+		const roles = await RoleModel.find({ id: { $in: body.roles } }).exec();
+		if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404);
+		// TODO: check if user has permission to add role
+	}
+
+	const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body).exec();
+
+	await emitEvent({
+		event: "GUILD_MEMBER_UPDATE",
+		guild_id,
+		data: toObject(member)
+	} as GuildMemberUpdateEvent);
+
+	res.json(toObject(member));
+});
+
+router.put("/", async (req: Request, res: Response) => {
+	const { guild_id, member_id } = req.params;
+
+	throw new HTTPError("Maintenance: Currently you can't add a member", 403);
+	// TODO: only for oauth2 applications
+	await addMember(member_id, guild_id);
+	res.sendStatus(204);
+});
+
+router.delete("/", async (req: Request, res: Response) => {
+	const { guild_id, member_id } = req.params;
+
+	const perms = await getPermission(req.user_id, guild_id);
+	perms.hasThrow("KICK_MEMBERS");
+
+	await removeMember(member_id, guild_id);
+	res.sendStatus(204);
+});
+
+export default router;
diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts b/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts
new file mode 100644
index 00000000..9078409d
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts
@@ -0,0 +1,24 @@
+import { getPermission, PermissionResolvable } from "@fosscord/server-util";
+import { Request, Response, Router } from "express";
+import { check } from "lambert-server";
+import { MemberNickChangeSchema } from "../../../../../schema/Member";
+import { changeNickname } from "../../../../../util/Member";
+
+const router = Router();
+
+router.patch("/", check(MemberNickChangeSchema), async (req: Request, res: Response) => {
+	var { guild_id, member_id } = req.params;
+	var permissionString: PermissionResolvable = "MANAGE_NICKNAMES";
+	if (member_id === "@me") {
+		member_id = req.user_id;
+		permissionString = "CHANGE_NICKNAME";
+	}
+
+	const perms = await getPermission(req.user_id, guild_id);
+	perms.hasThrow(permissionString);
+
+	await changeNickname(member_id, guild_id, req.body.nickname);
+	res.status(204);
+});
+
+export default router;
diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts b/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts
new file mode 100644
index 00000000..b7a43c74
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts
@@ -0,0 +1,27 @@
+import { getPermission } from "@fosscord/server-util";
+import { Request, Response, Router } from "express";
+import { addRole, removeRole } from "../../../../../../../util/Member";
+
+const router = Router();
+
+router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
+	const { guild_id, role_id, member_id } = req.params;
+
+	const perms = await getPermission(req.user_id, guild_id);
+	perms.hasThrow("MANAGE_ROLES");
+
+	await removeRole(member_id, guild_id, role_id);
+	res.sendStatus(204);
+});
+
+router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
+	const { guild_id, role_id, member_id } = req.params;
+
+	const perms = await getPermission(req.user_id, guild_id);
+	perms.hasThrow("MANAGE_ROLES");
+
+	await addRole(member_id, guild_id, role_id);
+	res.sendStatus(204);
+});
+
+export default router;
diff --git a/api/src/routes/guilds/#guild_id/members/index.ts b/api/src/routes/guilds/#guild_id/members/index.ts
new file mode 100644
index 00000000..a157d8f5
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/members/index.ts
@@ -0,0 +1,38 @@
+import { Request, Response, Router } from "express";
+import { GuildModel, MemberModel, toObject } from "@fosscord/server-util";
+import { HTTPError } from "lambert-server";
+import { instanceOf, Length } from "../../../../util/instanceOf";
+import { PublicMemberProjection, isMember } from "../../../../util/Member";
+
+const router = Router();
+
+// TODO: not allowed for user -> only allowed for bots with privileged intents
+// TODO: send over websocket
+router.get("/", async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+	const guild = await GuildModel.findOne({ id: guild_id }).exec();
+	await isMember(req.user_id, guild_id);
+
+	try {
+		instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, {
+			path: "query",
+			req,
+			ref: { obj: null, key: "" }
+		});
+	} catch (error) {
+		return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error });
+	}
+
+	// @ts-ignore
+	if (!req.query.limit) req.query.limit = 1;
+	const { limit, after } = (<unknown>req.query) as { limit: number; after: string };
+	const query = after ? { id: { $gt: after } } : {};
+
+	var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection)
+		.limit(limit)
+		.exec();
+
+	return res.json(toObject(members));
+});
+
+export default router;