more stuff
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
import { Config, emitEvent, Region, VoiceState } from "@spacebar/util";
|
||||
|
||||
interface StreamCreateSchema {
|
||||
type: "guild" | "call";
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
preferred_region?: string;
|
||||
}
|
||||
|
||||
export async function onStreamCreate(this: WebSocket, data: Payload) {
|
||||
const body = data.d as StreamCreateSchema;
|
||||
|
||||
// first check if we are in a voice channel already. cannot create a stream if there's no existing voice connection
|
||||
if (!this.voiceWs || !this.voiceWs.webrtcConnected) return;
|
||||
|
||||
// TODO: permissions check - if it's a guild, check if user is allowed to create stream in this guild
|
||||
|
||||
// TODO: create a new entry in db (StreamState?) containing the token for authenticating user in stream gateway IDENTIFY
|
||||
|
||||
// TODO: actually apply preferred_region from the event payload
|
||||
const regions = Config.get().regions;
|
||||
const guildRegion = regions.available.filter(
|
||||
(r) => r.id === regions.default,
|
||||
)[0];
|
||||
|
||||
const streamKey = `${body.type}${body.type === "guild" ? ":" + body.guild_id : ""}:${body.channel_id}:${this.user_id}`;
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_CREATE",
|
||||
data: {
|
||||
stream_key: streamKey,
|
||||
rtc_server_id: "lol", // for voice connections in guilds it is guild_id, for dm voice calls it seems to be DM channel id, for GoLive streams a generated number
|
||||
},
|
||||
guild_id: body.guild_id,
|
||||
//user_id: this.user_id,
|
||||
});
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_SERVER_UPDATE",
|
||||
data: {
|
||||
token: "TEST",
|
||||
stream_key: streamKey,
|
||||
endpoint: guildRegion.endpoint,
|
||||
},
|
||||
user_id: this.user_id,
|
||||
});
|
||||
}
|
||||
|
||||
//stream key:
|
||||
// guild:${guild_id}:${channel_id}:${user_id}
|
||||
// call:${channel_id}:${user_id}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
|
||||
interface StreamDeleteSchema {
|
||||
stream_key: string;
|
||||
}
|
||||
|
||||
export async function onStreamDelete(this: WebSocket, data: Payload) {
|
||||
const body = data.d as StreamDeleteSchema;
|
||||
|
||||
const splitStreamKey = body.stream_key.split(":");
|
||||
if (splitStreamKey.length < 3) {
|
||||
return this.close(4000, "Invalid stream key");
|
||||
}
|
||||
|
||||
const type = splitStreamKey.shift()!;
|
||||
let guild_id: string;
|
||||
|
||||
if (type === "guild") {
|
||||
guild_id = splitStreamKey.shift()!;
|
||||
}
|
||||
const channel_id = splitStreamKey.shift()!;
|
||||
const user_id = splitStreamKey.shift()!;
|
||||
|
||||
if (this.user_id !== user_id) {
|
||||
return this.close(4000, "Cannot delete stream for another user");
|
||||
}
|
||||
|
||||
// TODO: actually delete stream
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
|
||||
interface StreamWatchSchema {
|
||||
stream_key: string;
|
||||
}
|
||||
|
||||
export async function onStreamWatch(this: WebSocket, data: Payload) {
|
||||
const body = data.d as StreamWatchSchema;
|
||||
|
||||
const splitStreamKey = body.stream_key.split(":");
|
||||
if (splitStreamKey.length < 3) {
|
||||
return this.close(4000, "Invalid stream key");
|
||||
}
|
||||
|
||||
const type = splitStreamKey.shift()!;
|
||||
let guild_id: string;
|
||||
|
||||
if (type === "guild") {
|
||||
guild_id = splitStreamKey.shift()!;
|
||||
}
|
||||
const channel_id = splitStreamKey.shift()!;
|
||||
const user_id = splitStreamKey.shift()!;
|
||||
}
|
||||
@@ -25,6 +25,9 @@ import { onRequestGuildMembers } from "./RequestGuildMembers";
|
||||
import { onResume } from "./Resume";
|
||||
import { onVoiceStateUpdate } from "./VoiceStateUpdate";
|
||||
import { onGuildSubscriptionsBulk } from "./GuildSubscriptionsBulk";
|
||||
import { onStreamCreate } from "./StreamCreate";
|
||||
import { onStreamDelete } from "./StreamDelete";
|
||||
import { onStreamWatch } from "./StreamWatch";
|
||||
|
||||
export type OPCodeHandler = (this: WebSocket, data: Payload) => unknown;
|
||||
|
||||
@@ -41,5 +44,8 @@ export default {
|
||||
// 10: Hello
|
||||
// 13: Dm_update
|
||||
14: onLazyRequest,
|
||||
18: onStreamCreate,
|
||||
19: onStreamDelete,
|
||||
20: onStreamWatch,
|
||||
37: onGuildSubscriptionsBulk,
|
||||
} as { [key: number]: OPCodeHandler };
|
||||
|
||||
@@ -42,6 +42,7 @@ export interface WebSocket extends WS {
|
||||
member_events: Record<string, () => unknown>;
|
||||
listen_options: ListenEventOpts;
|
||||
capabilities?: Capabilities;
|
||||
client?: WebRtcClient<WebSocket>;
|
||||
voiceWs?: WebRtcClient<WebSocket>;
|
||||
streamWs?: WebRtcClient<WebSocket>;
|
||||
large_threshold: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user