summary refs log tree commit diff
path: root/src/gateway/opcodes/LazyRequest.ts
blob: d1806f9133b9aaa44574aa05261434254ea0a654 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Fosscord and Fosscord Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import {
	getDatabase,
	getPermission,
	listenEvent,
	Member,
	Role,
	Session,
	LazyRequestSchema,
	User,
	Presence,
} from "@fosscord/util";
import {
	WebSocket,
	Payload,
	handlePresenceUpdate,
	OPCODES,
	Send,
} from "@fosscord/gateway";
import { check } from "./instanceOf";

// TODO: only show roles/members that have access to this channel
// TODO: config: to list all members (even those who are offline) sorted by role, or just those who are online
// TODO: rewrite typeorm

const getMostRelevantSession = (sessions: Session[]) => {
	const statusMap = {
		online: 0,
		idle: 1,
		dnd: 2,
		invisible: 3,
		offline: 4,
	};
	// sort sessions by relevance
	sessions = sessions.sort((a, b) => {
		return (
			statusMap[a.status] -
			statusMap[b.status] +
			(a.activities.length - b.activities.length) * 2
		);
	});

	return sessions.first();
};

async function getMembers(guild_id: string, range: [number, number]) {
	if (!Array.isArray(range) || range.length !== 2) {
		throw new Error("range is not a valid array");
	}
	// TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620

	let members: Member[] = [];
	try {
		members =
			(await getDatabase()
				?.getRepository(Member)
				.createQueryBuilder("member")
				.where("member.guild_id = :guild_id", { guild_id })
				.leftJoinAndSelect("member.roles", "role")
				.leftJoinAndSelect("member.user", "user")
				.leftJoinAndSelect("user.sessions", "session")
				.addSelect("user.settings")
				.addSelect(
					"CASE WHEN session.status = 'offline' THEN 0 ELSE 1 END",
					"_status",
				)
				.orderBy("role.position", "DESC")
				.addOrderBy("_status", "DESC")
				.addOrderBy("user.username", "ASC")
				.offset(Number(range[0]) || 0)
				.limit(Number(range[1]) || 100)
				.getMany()) ?? [];
	} catch (e) {
		console.error(`LazyRequest`, e);
	}

	if (!members) {
		return {
			items: [],
			groups: [],
			range: [],
			members: [],
		};
	}

	const groups = [];
	const items = [];
	const member_roles = members
		.map((m) => m.roles)
		.flat()
		.unique((r: Role) => r.id);
	member_roles.push(
		member_roles.splice(
			member_roles.findIndex((x) => x.id === x.guild_id),
			1,
		)[0],
	);

	const offlineItems = [];

	for (const role of member_roles) {
		const [role_members, other_members] = partition(
			members,
			(m: Member) => !!m.roles.find((r) => r.id === role.id),
		);
		const group = {
			count: role_members.length,
			id: role.id === guild_id ? "online" : role.id,
		};

		items.push({ group });
		groups.push(group);

		for (const member of role_members) {
			const roles = member.roles
				.filter((x: Role) => x.id !== guild_id)
				.map((x: Role) => x.id);

			const session: Session | undefined = getMostRelevantSession(
				member.user.sessions,
			);

			// eslint-disable-next-line @typescript-eslint/ban-ts-comment
			// @ts-ignore
			if (session?.status == "unknown") {
				session.status = member?.user?.settings?.status || "online";
			}

			const item = {
				member: {
					...member,
					roles,
					user: member.user.toPublicUser(),
					presence: {
						...session,
						activities: session?.activities || [],
						user: { id: member.user.id },
					},
				},
			};

			if (
				!session ||
				session.status == "invisible" ||
				session.status == "offline"
			) {
				item.member.presence.status = "offline";
				offlineItems.push(item);
				group.count--;
				continue;
			}

			items.push(item);
		}
		members = other_members;
	}

	if (offlineItems.length) {
		const group = {
			count: offlineItems.length,
			id: "offline",
		};
		items.push({ group });
		groups.push(group);

		items.push(...offlineItems);
	}

	return {
		items,
		groups,
		range,
		members: items
			.map((x) =>
				"member" in x
					? { ...x.member, settings: undefined }
					: undefined,
			)
			.filter((x) => !!x),
	};
}

async function subscribeToMemberEvents(this: WebSocket, user_id: string) {
	if (this.events[user_id]) return false; // already subscribed as friend
	if (this.member_events[user_id]) return false; // already subscribed in member list
	this.member_events[user_id] = await listenEvent(
		user_id,
		handlePresenceUpdate.bind(this),
		this.listen_options,
	);
	return true;
}

export async function onLazyRequest(this: WebSocket, { d }: Payload) {
	// TODO: check data
	check.call(this, LazyRequestSchema, d);
	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	const { guild_id, typing, channels, activities, members } =
		d as LazyRequestSchema;

	if (members) {
		// Client has requested a PRESENCE_UPDATE for specific member

		await Promise.all([
			members.map(async (x) => {
				if (!x) return;
				const didSubscribe = await subscribeToMemberEvents.call(
					this,
					x,
				);
				if (!didSubscribe) return;

				// if we didn't subscribe just now, this is a new subscription
				// and we should send a PRESENCE_UPDATE immediately

				const sessions = await Session.find({ where: { user_id: x } });
				const session = getMostRelevantSession(sessions);
				// eslint-disable-next-line @typescript-eslint/ban-ts-comment
				// @ts-ignore
				if (session?.status == "unknown") session.status = "online";
				const user = (await User.getPublicUser(x)).toPublicUser(); // why is this needed?

				return Send(this, {
					op: OPCODES.Dispatch,
					s: this.sequence++,
					t: "PRESENCE_UPDATE",
					d: {
						user: user,
						activities: session?.activities || [],
						client_status: session?.client_info,
						status: session?.status || "offline",
					} as Presence,
				});
			}),
		]);

		if (!channels) return;
	}

	if (!channels) throw new Error("Must provide channel ranges");

	const channel_id = Object.keys(channels || {}).first();
	if (!channel_id) return;

	const permissions = await getPermission(this.user_id, guild_id, channel_id);
	permissions.hasThrow("VIEW_CHANNEL");

	const ranges = channels[channel_id];
	if (!Array.isArray(ranges)) throw new Error("Not a valid Array");

	const member_count = await Member.count({ where: { guild_id } });
	const ops = await Promise.all(ranges.map((x) => getMembers(guild_id, x)));

	// TODO: unsubscribe member_events that are not in op.members

	ops.forEach((op) => {
		op.members.forEach(async (member) => {
			if (!member?.user.id) return;
			return subscribeToMemberEvents.call(this, member.user.id);
		});
	});

	const groups = ops
		.map((x) => x.groups)
		.flat()
		.unique();

	return await Send(this, {
		op: OPCODES.Dispatch,
		s: this.sequence++,
		t: "GUILD_MEMBER_LIST_UPDATE",
		d: {
			ops: ops.map((x) => ({
				items: x.items,
				op: "SYNC",
				range: x.range,
			})),
			online_count:
				member_count -
				(groups.find((x) => x.id == "offline")?.count ?? 0),
			member_count,
			id: "everyone",
			guild_id,
			groups,
		},
	});
}

/* https://stackoverflow.com/a/50636286 */
function partition<T>(array: T[], filter: (elem: T) => boolean) {
	const pass: T[] = [],
		fail: T[] = [];
	array.forEach((e) => (filter(e) ? pass : fail).push(e));
	return [pass, fail];
}