Files
server/src/gateway/util/Utils.ts
T
Rory& 0c384cf4e9
Build docker images / build (api) (push) Failing after 1m9s
Build docker images / build (cdn) (push) Failing after 1m20s
Build docker images / build (admin-api) (push) Failing after 4m7s
Build docker images / build (cdn-cs) (push) Failing after 1m50s
Build docker images / build (gateway) (push) Failing after 1m28s
Build / build (24.x) (push) Successful in 2m42s
Nix build / build-nix (push) Failing after 5m17s
Build docker images / build (gateway-offload) (push) Failing after 1m20s
Build docker images / build (default) (push) Failing after 5m30s
Style / build (24.x) (push) Failing after 2m18s
Gateway: dont block on clearing voice states
2026-02-16 21:40:49 +01:00

61 lines
2.1 KiB
TypeScript

import { VoiceState } from "@spacebar/util";
export function parseStreamKey(streamKey: string): {
type: "guild" | "call";
channelId: string;
guildId?: string;
userId: string;
} {
const streamKeyArray = streamKey.split(":");
const type = streamKeyArray.shift();
if (type !== "guild" && type !== "call") {
throw new Error(`Invalid stream key type: ${type}`);
}
if ((type === "guild" && streamKeyArray.length < 3) || (type === "call" && streamKeyArray.length < 2)) throw new Error(`Invalid stream key: ${streamKey}`); // invalid stream key
let guildId: string | undefined;
if (type === "guild") {
guildId = streamKeyArray.shift();
}
const channelId = streamKeyArray.shift();
const userId = streamKeyArray.shift();
if (!channelId || !userId) {
throw new Error(`Invalid stream key: ${streamKey}`);
}
return { type, channelId, guildId, userId };
}
export function generateStreamKey(type: "guild" | "call", guildId: string | undefined, channelId: string, userId: string): string {
const streamKey = `${type}${type === "guild" ? `:${guildId}` : ""}:${channelId}:${userId}`;
return streamKey;
}
// Temporary cleanup function until shutdown cleanup function is fixed.
// Currently when server is shut down the voice states are not cleared
// TODO: remove this when Server.stop() is fixed so that it waits for all websocket connections to run their
// respective Close event listener function for session cleanup
export async function cleanupOnStartup(): Promise<void> {
// TODO: how is this different from clearing the table?
//await VoiceState.update(
// {},
// {
// // @ts-expect-error channel_id is nullable
// channel_id: null,
// // @ts-expect-error guild_id is nullable
// guild_id: null,
// self_stream: false,
// self_video: false,
// },
//);
console.log("[Gateway] Starting async voice state wipe...");
VoiceState.clear()
.then((e) => console.log("[Gateway] Successfully cleaned voice states"))
.catch((e) => console.error("[Gateway] Error cleaning voice states on startup:", e));
}