mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-18 13:26:04 +00:00
Factor out getMostRelevantSession
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user