summary refs log tree commit diff
path: root/src/opcodes/LazyRequest.ts
blob: ba7278f0c1c0c67dd295a2eea0d4ac2fe5a3e83e (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
// @ts-nocheck WIP
import { db, getPermission, MemberModel, MongooseCache, PublicUserProjection, RoleModel } from "@fosscord/server-util";
import { LazyRequest } from "../schema/LazyRequest";
import { OPCODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket";
import { check } from "./instanceOf";

// TODO: config: if want to list all members (even those who are offline) sorted by role, or just those who are online

export async function onLazyRequest(this: WebSocket, { d }: Payload) {
	return; // WIP
	// TODO: check data
	check.call(this, LazyRequest, d);
	const { guild_id, typing, channels, activities } = d as LazyRequest;

	const permissions = await getPermission(this.user_id, guild_id);

	// MongoDB query to retrieve all hoisted roles and join them with the members and users collection
	const roles = await db
		.collection("roles")
		.aggregate([
			{ $match: { guild_id, hoist: true } },
			{ $sort: { position: 1 } },
			{
				$lookup: {
					from: "members",
					let: { id: "$id" },
					pipeline: [
						{ $match: { $expr: { $in: ["$$id", "$roles"] } } },
						{ $limit: 1 },
						{
							$lookup: {
								from: "users",
								let: { user_id: "$id" },
								pipeline: [
									{ $match: { $expr: { $eq: ["$id", "$$user_id"] } } },
									{ $project: PublicUserProjection },
								],
								as: "user",
							},
						},
					],
					as: "members",
				},
			},
		])
		.toArray();

	Send(this, {
		op: OPCODES.Dispatch,
		s: this.sequence++,
		t: "GUILD_MEMBER_LIST_UPDATE",
		d: {
			ops: [
				{
					range: [0, 99],
					op: "SYNC",
					items: [{ group: { id: "online", count: 0 } }],
				},
			],
			online_count: 1,
			member_count: 1,
			id: "everyone",
			guild_id,
			groups: [{ id: "online", count: 1 }],
		},
	});
}