summary refs log tree commit diff
path: root/src/routes/api/v8/guilds/#id/members/index.ts
blob: 9446e2da43f6c424c4ac3a2773155766d4b71b36 (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
import { Request, Response, Router } from "express";
import { GuildModel, MemberModel } from "fosscord-server-util";
import { HTTPError } from "lambert-server";
import { instanceOf, Length } from "../../../../../../util/instanceOf";
import { PublicMemberProjection } from "../../../../../../util/Member";
import { PublicUserProjection } from "../../../../../../util/User";

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 = BigInt(req.params.id);
	const guild = await GuildModel.findOne({ id: guild_id }).exec();
	if (!guild) throw new HTTPError("Guild not found", 404);

	try {
		instanceOf({ $limit: new Length(Number, 1, 1000), $after: BigInt }, req.query, {
			path: "query",
			req,
			ref: { obj: null, key: "" },
		});
	} catch (error) {
		return res.status(400).json({ code: 50035, message: "Invalid Form Body", success: false, errors: error });
	}

	// @ts-ignore
	if (!req.query.limit) req.query.limit = 1;
	const { limit, after } = (<unknown>req.query) as { limit: number; after: bigint };
	const query = after ? { id: { $gt: after } } : {};

	var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection)
		.limit(limit)
		.populate({ path: "user", select: PublicUserProjection })
		.exec();

	return res.json(members);
});

router.get("/:member", async (req: Request, res: Response) => {
	const guild_id = BigInt(req.params.id);
	const user_id = BigInt(req.params.member);

	const member = await MemberModel.findOne({ id: user_id, guild_id })
		.populate({ path: "user", select: PublicUserProjection })
		.exec();
	if (!member) throw new HTTPError("Member not found", 404);

	return res.json(member);
});

export default router;