summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-03-12 21:32:34 +0100
committerRory& <root@rory.gay>2026-03-12 21:34:27 +0100
commit6ed97ea2d1f011ced52cc7e3c2f642aaa3c82f1d (patch)
tree9145144faf64bc13ef77e79a05ec78fafb4034ac
parentfix op43 (diff)
downloadserver-ts-6ed97ea2d1f011ced52cc7e3c2f642aaa3c82f1d.tar.xz
Factor out getMostRelevantSession
-rw-r--r--.idea/workspace.xml25
-rw-r--r--src/gateway/opcodes/GuildSync.ts18
-rw-r--r--src/gateway/opcodes/LazyRequest.ts18
-rw-r--r--src/gateway/util/SessionUtils.ts18
4 files changed, 24 insertions, 55 deletions
diff --git a/.idea/workspace.xml b/.idea/workspace.xml

index 51f40ff2..11f4ea46 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml
@@ -12,18 +12,6 @@ <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="LAST_RESOLUTION" value="IGNORE" /> </component> - <component name="CopilotPersistence"> - <persistenceIdMap> - <entry key="_/home/Rory/git/spacebar/server-master" value="2oJ9u2nkEFq1qQW1NFF69ECjiYu" /> - </persistenceIdMap> - </component> - <component name="CopilotUserSelectedModel"> - <selectedModels> - <entry key="edit-panel" value="GPT-5 mini" /> - <entry key="chat-panel" value="Gemini 3 Pro (Preview)" /> - <entry key="agent-panel" value="Gemini 3 Pro (Preview)" /> - </selectedModels> - </component> <component name="FileTemplateManagerImpl"> <option name="RECENT_TEMPLATES"> <list> @@ -202,7 +190,10 @@ <workItem from="1771558270982" duration="82802000" /> <workItem from="1771818832744" duration="44139000" /> <workItem from="1771996450006" duration="81615000" /> - <workItem from="1772272118101" duration="20496000" /> + <workItem from="1772272118101" duration="30395000" /> + <workItem from="1772714447261" duration="12025000" /> + <workItem from="1772830175841" duration="4576000" /> + <workItem from="1772918744394" duration="70646000" /> </task> <servers /> </component> @@ -241,12 +232,4 @@ </breakpoints> </breakpoint-manager> </component> - <component name="github-copilot-workspace"> - <instructionFileLocations> - <option value=".github/instructions" /> - </instructionFileLocations> - <promptFileLocations> - <option value=".github/prompts" /> - </promptFileLocations> - </component> </project> \ No newline at end of file diff --git a/src/gateway/opcodes/GuildSync.ts b/src/gateway/opcodes/GuildSync.ts
index 770e62c8..34bfdb90 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 ab58368d..0e63a7d2 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 843341cc..f84304c6 100644 --- a/src/gateway/util/SessionUtils.ts +++ b/src/gateway/util/SessionUtils.ts
@@ -16,6 +16,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +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]; +}