Factor out getMostRelevantSession

This commit is contained in:
Rory&
2026-03-12 21:32:34 +01:00
parent ba78ed8e6a
commit 6ed97ea2d1
4 changed files with 24 additions and 55 deletions

25
.idea/workspace.xml generated
View File

@@ -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>

View File

@@ -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");

View File

@@ -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");

View File

@@ -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];
}