add schemas for stream gateway messages
This commit is contained in:
parent
dd079f935f
commit
54f6685082
@ -11,17 +11,13 @@ import {
|
|||||||
Region,
|
Region,
|
||||||
Snowflake,
|
Snowflake,
|
||||||
Stream,
|
Stream,
|
||||||
|
StreamCreateSchema,
|
||||||
StreamSession,
|
StreamSession,
|
||||||
} from "@spacebar/util";
|
} from "@spacebar/util";
|
||||||
|
import { check } from "./instanceOf";
|
||||||
interface StreamCreateSchema {
|
|
||||||
type: "guild" | "call";
|
|
||||||
channel_id: string;
|
|
||||||
guild_id?: string;
|
|
||||||
preferred_region?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function onStreamCreate(this: WebSocket, data: Payload) {
|
export async function onStreamCreate(this: WebSocket, data: Payload) {
|
||||||
|
check.call(this, StreamCreateSchema, data.d);
|
||||||
const body = data.d as StreamCreateSchema;
|
const body = data.d as StreamCreateSchema;
|
||||||
|
|
||||||
// TODO: first check if we are in a voice channel already. cannot create a stream if there's no existing voice connection
|
// TODO: first check if we are in a voice channel already. cannot create a stream if there's no existing voice connection
|
||||||
|
@ -1,16 +1,25 @@
|
|||||||
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
|
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
|
||||||
import { emitEvent, Stream } from "@spacebar/util";
|
import { emitEvent, Stream, StreamDeleteSchema } from "@spacebar/util";
|
||||||
|
import { check } from "./instanceOf";
|
||||||
interface StreamDeleteSchema {
|
|
||||||
stream_key: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function onStreamDelete(this: WebSocket, data: Payload) {
|
export async function onStreamDelete(this: WebSocket, data: Payload) {
|
||||||
|
check.call(this, StreamDeleteSchema, data.d);
|
||||||
const body = data.d as StreamDeleteSchema;
|
const body = data.d as StreamDeleteSchema;
|
||||||
|
|
||||||
const { userId, channelId, guildId, type } = parseStreamKey(
|
let parsedKey: {
|
||||||
body.stream_key,
|
type: "guild" | "call";
|
||||||
);
|
channelId: string;
|
||||||
|
guildId?: string;
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
parsedKey = parseStreamKey(body.stream_key);
|
||||||
|
} catch (e) {
|
||||||
|
return this.close(4000, "Invalid stream key");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { userId, channelId, guildId, type } = parsedKey;
|
||||||
|
|
||||||
if (this.user_id !== userId) {
|
if (this.user_id !== userId) {
|
||||||
return this.close(4000, "Cannot delete stream for another user");
|
return this.close(4000, "Cannot delete stream for another user");
|
||||||
|
@ -4,13 +4,18 @@ import {
|
|||||||
Payload,
|
Payload,
|
||||||
WebSocket,
|
WebSocket,
|
||||||
} from "@spacebar/gateway";
|
} from "@spacebar/gateway";
|
||||||
import { Config, emitEvent, Stream, StreamSession } from "@spacebar/util";
|
import {
|
||||||
|
Config,
|
||||||
interface StreamWatchSchema {
|
emitEvent,
|
||||||
stream_key: string;
|
Stream,
|
||||||
}
|
StreamSession,
|
||||||
|
StreamWatchSchema,
|
||||||
|
} from "@spacebar/util";
|
||||||
|
import { check } from "./instanceOf";
|
||||||
|
import { Not } from "typeorm";
|
||||||
|
|
||||||
export async function onStreamWatch(this: WebSocket, data: Payload) {
|
export async function onStreamWatch(this: WebSocket, data: Payload) {
|
||||||
|
check.call(this, StreamWatchSchema, data.d);
|
||||||
const body = data.d as StreamWatchSchema;
|
const body = data.d as StreamWatchSchema;
|
||||||
|
|
||||||
// TODO: apply perms: check if user is allowed to watch
|
// TODO: apply perms: check if user is allowed to watch
|
||||||
@ -56,8 +61,13 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
|
|||||||
|
|
||||||
await streamSession.save();
|
await streamSession.save();
|
||||||
|
|
||||||
|
// get the viewers: stream session tokens for this stream that have been used but not including stream owner
|
||||||
const viewers = await StreamSession.find({
|
const viewers = await StreamSession.find({
|
||||||
where: { stream_id: stream.id },
|
where: {
|
||||||
|
stream_id: stream.id,
|
||||||
|
used: true,
|
||||||
|
user_id: Not(stream.owner_id),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
|
13
src/util/schemas/StreamCreateSchema.ts
Normal file
13
src/util/schemas/StreamCreateSchema.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
export interface StreamCreateSchema {
|
||||||
|
type: "guild" | "call";
|
||||||
|
channel_id: string;
|
||||||
|
guild_id?: string;
|
||||||
|
preferred_region?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StreamCreateSchema = {
|
||||||
|
type: String,
|
||||||
|
channel_id: String,
|
||||||
|
$guild_id: String,
|
||||||
|
$preferred_region: String,
|
||||||
|
};
|
7
src/util/schemas/StreamDeleteSchema.ts
Normal file
7
src/util/schemas/StreamDeleteSchema.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface StreamDeleteSchema {
|
||||||
|
stream_key: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StreamDeleteSchema = {
|
||||||
|
stream_key: String,
|
||||||
|
};
|
7
src/util/schemas/StreamWatchSchema.ts
Normal file
7
src/util/schemas/StreamWatchSchema.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface StreamWatchSchema {
|
||||||
|
stream_key: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StreamWatchSchema = {
|
||||||
|
stream_key: String,
|
||||||
|
};
|
@ -68,6 +68,9 @@ export * from "./responses";
|
|||||||
export * from "./RoleModifySchema";
|
export * from "./RoleModifySchema";
|
||||||
export * from "./RolePositionUpdateSchema";
|
export * from "./RolePositionUpdateSchema";
|
||||||
export * from "./SelectProtocolSchema";
|
export * from "./SelectProtocolSchema";
|
||||||
|
export * from "./StreamCreateSchema";
|
||||||
|
export * from "./StreamDeleteSchema";
|
||||||
|
export * from "./StreamWatchSchema";
|
||||||
export * from "./TeamCreateSchema";
|
export * from "./TeamCreateSchema";
|
||||||
export * from "./TemplateCreateSchema";
|
export * from "./TemplateCreateSchema";
|
||||||
export * from "./TemplateModifySchema";
|
export * from "./TemplateModifySchema";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user