summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/members/#member_id/index.ts
blob: 9a1676e622c8892d2963ccc90f6004d574a9896d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;