update event types + schema

This commit is contained in:
dank074 2025-04-24 18:29:45 -05:00
parent 2d4f3b0c6a
commit bbcd690620
5 changed files with 14968 additions and 7 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,9 @@ import {
Region, Region,
Snowflake, Snowflake,
Stream, Stream,
StreamCreateEvent,
StreamCreateSchema, StreamCreateSchema,
StreamServerUpdateEvent,
StreamSession, StreamSession,
VoiceState, VoiceState,
VoiceStateUpdateEvent, VoiceStateUpdateEvent,
@ -100,7 +102,7 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
paused: false, paused: false,
}, },
user_id: this.user_id, user_id: this.user_id,
}); } as StreamCreateEvent);
await emitEvent({ await emitEvent({
event: "STREAM_SERVER_UPDATE", event: "STREAM_SERVER_UPDATE",
@ -111,7 +113,7 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
endpoint: stream.endpoint, endpoint: stream.endpoint,
}, },
user_id: this.user_id, user_id: this.user_id,
}); } as StreamServerUpdateEvent);
voiceState.self_stream = true; voiceState.self_stream = true;
await voiceState.save(); await voiceState.save();

View File

@ -2,6 +2,7 @@ import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
import { import {
emitEvent, emitEvent,
Stream, Stream,
StreamDeleteEvent,
StreamDeleteSchema, StreamDeleteSchema,
VoiceState, VoiceState,
VoiceStateUpdateEvent, VoiceStateUpdateEvent,
@ -27,15 +28,24 @@ export async function onStreamDelete(this: WebSocket, data: Payload) {
const { userId, channelId, guildId, type } = parsedKey; const { userId, channelId, guildId, type } = parsedKey;
// when a user selects to stop watching another user stream, this event gets triggered
// just disconnect user without actually deleting stream
if (this.user_id !== userId) { if (this.user_id !== userId) {
return this.close(4000, "Cannot delete stream for another user"); await emitEvent({
event: "STREAM_DELETE",
data: {
stream_key: body.stream_key,
},
user_id: this.user_id,
} as StreamDeleteEvent);
return;
} }
const stream = await Stream.findOne({ const stream = await Stream.findOne({
where: { channel_id: channelId, owner_id: userId }, where: { channel_id: channelId, owner_id: userId },
}); });
if (!stream) return this.close(4000, "Invalid stream key"); if (!stream) return;
await stream.remove(); await stream.remove();
@ -62,5 +72,5 @@ export async function onStreamDelete(this: WebSocket, data: Payload) {
}, },
guild_id: guildId, guild_id: guildId,
channel_id: channelId, channel_id: channelId,
}); } as StreamDeleteEvent);
} }

View File

@ -8,6 +8,8 @@ import {
Config, Config,
emitEvent, emitEvent,
Stream, Stream,
StreamCreateEvent,
StreamServerUpdateEvent,
StreamSession, StreamSession,
StreamWatchSchema, StreamWatchSchema,
} from "@spacebar/util"; } from "@spacebar/util";
@ -81,7 +83,7 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
}, },
channel_id: channelId, channel_id: channelId,
user_id: this.user_id, user_id: this.user_id,
}); } as StreamCreateEvent);
await emitEvent({ await emitEvent({
event: "STREAM_SERVER_UPDATE", event: "STREAM_SERVER_UPDATE",
@ -92,5 +94,5 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
endpoint: stream.endpoint, endpoint: stream.endpoint,
}, },
user_id: this.user_id, user_id: this.user_id,
}); } as StreamServerUpdateEvent);
} }

View File

@ -451,6 +451,9 @@ export interface StreamCreateEvent extends Event {
data: { data: {
stream_key: string; stream_key: string;
rtc_server_id: string; rtc_server_id: string;
viewer_ids: string[];
region: string;
paused: boolean;
}; };
} }
@ -460,6 +463,14 @@ export interface StreamServerUpdateEvent extends Event {
token: string; token: string;
stream_key: string; stream_key: string;
endpoint: string; endpoint: string;
guild_id: string | null;
};
}
export interface StreamDeleteEvent extends Event {
event: "STREAM_DELETE";
data: {
stream_key: string;
}; };
} }