From 6ed97ea2d1f011ced52cc7e3c2f642aaa3c82f1d Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 12 Mar 2026 21:32:34 +0100 Subject: Factor out getMostRelevantSession --- src/gateway/opcodes/GuildSync.ts | 18 +----------------- src/gateway/opcodes/LazyRequest.ts | 18 +----------------- src/gateway/util/SessionUtils.ts | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/gateway/opcodes/GuildSync.ts b/src/gateway/opcodes/GuildSync.ts index 770e62c81..34bfdb902 100644 --- a/src/gateway/opcodes/GuildSync.ts +++ b/src/gateway/opcodes/GuildSync.ts @@ -32,7 +32,7 @@ import { Stopwatch, Guild, } from "@spacebar/util"; -import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send } from "@spacebar/gateway"; +import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send, getMostRelevantSession } from "@spacebar/gateway"; import murmur from "murmurhash-js/murmurhash3_gc"; import { check } from "./instanceOf"; import { LazyRequestSchema, PublicMember } from "@spacebar/schemas"; @@ -42,22 +42,6 @@ import { In } from "typeorm"; // 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 ?? 0) - (b.activities?.length ?? 0)) * 2; - }); - - return sessions[0]; -}; - export async function onGuildSync(this: WebSocket, { d }: Payload) { const sw = Stopwatch.startNew(); if (!Array.isArray(d)) throw new Error("Invalid payload for GUILD_SYNC"); diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts index ab58368dd..0e63a7d2d 100644 --- a/src/gateway/opcodes/LazyRequest.ts +++ b/src/gateway/opcodes/LazyRequest.ts @@ -17,7 +17,7 @@ */ import { getDatabase, getPermission, listenEvent, Member, Role, Session, User, Presence, Channel, Permissions, arrayPartition } from "@spacebar/util"; -import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send } from "@spacebar/gateway"; +import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send, getMostRelevantSession } from "@spacebar/gateway"; import murmur from "murmurhash-js/murmurhash3_gc"; import { check } from "./instanceOf"; import { LazyRequestSchema } from "@spacebar/schemas"; @@ -26,22 +26,6 @@ import { LazyRequestSchema } from "@spacebar/schemas"; // TODO: config: to list all members (even those who are offline) sorted by role, or just those who are online // TODO: rewrite typeorm -function 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 ?? 0) - (b.activities?.length ?? 0)) * 2; - }); - - return sessions[0]; -} - 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"); diff --git a/src/gateway/util/SessionUtils.ts b/src/gateway/util/SessionUtils.ts index 843341cc5..f84304c61 100644 --- a/src/gateway/util/SessionUtils.ts +++ b/src/gateway/util/SessionUtils.ts @@ -16,6 +16,8 @@ along with this program. If not, see . */ +import { getDatabase, Member, Session } from "@spacebar/util"; + export function genSessionId() { return genRanHex(32); } @@ -27,3 +29,19 @@ export function genVoiceToken() { function genRanHex(size: number) { return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(""); } + +export function 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 ?? 0) - (b.activities?.length ?? 0)) * 2; + }); + + return sessions[0]; +} -- cgit 1.5.1