summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-01-30 20:19:24 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-01-30 20:23:35 +1100
commitd4d1ab181e5f9bf89fa8758b98c7d331fde45044 (patch)
tree8dad4ca42c3bdb08a47fa8860d5b543bf9bc40cf /src
parentquestion mark. (diff)
downloadserver-d4d1ab181e5f9bf89fa8758b98c7d331fde45044.tar.xz
subscribe clients to member events when they lazy request members
Diffstat (limited to 'src')
-rw-r--r--src/gateway/opcodes/LazyRequest.ts31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts
index ce0bb915..b7fff4d1 100644
--- a/src/gateway/opcodes/LazyRequest.ts
+++ b/src/gateway/opcodes/LazyRequest.ts
@@ -197,6 +197,17 @@ async function getMembers(guild_id: string, range: [number, number]) {
 	};
 }
 
+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);
@@ -209,6 +220,16 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 
 		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
@@ -251,14 +272,8 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 
 	ops.forEach((op) => {
 		op.members.forEach(async (member) => {
-			if (!member) return;
-			if (this.events[member.user.id]) return; // already subscribed as friend
-			if (this.member_events[member.user.id]) return; // already subscribed in member list
-			this.member_events[member.user.id] = await listenEvent(
-				member.user.id,
-				handlePresenceUpdate.bind(this),
-				this.listen_options,
-			);
+			if (!member?.user.id) return;
+			return subscribeToMemberEvents.call(this, member.user.id);
 		});
 	});