a lot of changes
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
import { Config, emitEvent, Region, VoiceState } from "@spacebar/util";
|
||||
import {
|
||||
genVoiceToken,
|
||||
Payload,
|
||||
WebSocket,
|
||||
generateStreamKey,
|
||||
} from "@spacebar/gateway";
|
||||
import {
|
||||
Config,
|
||||
emitEvent,
|
||||
Region,
|
||||
Snowflake,
|
||||
Stream,
|
||||
StreamSession,
|
||||
} from "@spacebar/util";
|
||||
|
||||
interface StreamCreateSchema {
|
||||
type: "guild" | "call";
|
||||
@@ -11,37 +23,65 @@ interface StreamCreateSchema {
|
||||
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: first check if we are in a voice channel already. cannot create a stream if there's no existing voice connection
|
||||
if (body.channel_id.trim().length === 0) 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}`;
|
||||
// create a new entry in db containing the token for authenticating user in stream gateway IDENTIFY
|
||||
const stream = Stream.create({
|
||||
id: Snowflake.generate(),
|
||||
owner_id: this.user_id,
|
||||
channel_id: body.channel_id,
|
||||
endpoint: guildRegion.endpoint,
|
||||
});
|
||||
|
||||
await stream.save();
|
||||
|
||||
const token = genVoiceToken();
|
||||
|
||||
const streamSession = StreamSession.create({
|
||||
stream_id: stream.id,
|
||||
user_id: this.user_id,
|
||||
session_id: this.session_id,
|
||||
token,
|
||||
});
|
||||
|
||||
await streamSession.save();
|
||||
|
||||
const streamKey = generateStreamKey(
|
||||
body.type,
|
||||
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
|
||||
rtc_server_id: stream.id, // 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
|
||||
viewer_ids: [],
|
||||
region: guildRegion.name,
|
||||
paused: false,
|
||||
},
|
||||
guild_id: body.guild_id,
|
||||
//user_id: this.user_id,
|
||||
channel_id: body.channel_id,
|
||||
});
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_SERVER_UPDATE",
|
||||
data: {
|
||||
token: "TEST",
|
||||
token: streamSession.token,
|
||||
stream_key: streamKey,
|
||||
endpoint: guildRegion.endpoint,
|
||||
guild_id: null, // not sure why its always null
|
||||
endpoint: stream.endpoint,
|
||||
},
|
||||
user_id: this.user_id,
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
|
||||
import { emitEvent, Stream } from "@spacebar/util";
|
||||
|
||||
interface StreamDeleteSchema {
|
||||
stream_key: string;
|
||||
@@ -7,23 +8,28 @@ interface StreamDeleteSchema {
|
||||
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 { userId, channelId, guildId, type } = parseStreamKey(
|
||||
body.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) {
|
||||
if (this.user_id !== userId) {
|
||||
return this.close(4000, "Cannot delete stream for another user");
|
||||
}
|
||||
|
||||
// TODO: actually delete stream
|
||||
const stream = await Stream.findOne({
|
||||
where: { channel_id: channelId, owner_id: userId },
|
||||
});
|
||||
|
||||
if (!stream) return this.close(4000, "Invalid stream key");
|
||||
|
||||
await stream.remove();
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_DELETE",
|
||||
data: {
|
||||
stream_key: body.stream_key,
|
||||
},
|
||||
guild_id: guildId,
|
||||
channel_id: channelId,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Payload, WebSocket } from "@spacebar/gateway";
|
||||
import {
|
||||
genVoiceToken,
|
||||
parseStreamKey,
|
||||
Payload,
|
||||
WebSocket,
|
||||
} from "@spacebar/gateway";
|
||||
import { Config, emitEvent, Stream, StreamSession } from "@spacebar/util";
|
||||
|
||||
interface StreamWatchSchema {
|
||||
stream_key: string;
|
||||
@@ -7,17 +13,60 @@ interface StreamWatchSchema {
|
||||
export async function onStreamWatch(this: WebSocket, data: Payload) {
|
||||
const body = data.d as StreamWatchSchema;
|
||||
|
||||
const splitStreamKey = body.stream_key.split(":");
|
||||
if (splitStreamKey.length < 3) {
|
||||
// TODO: apply perms: check if user is allowed to watch
|
||||
try {
|
||||
const { type, channelId, guildId, userId } = parseStreamKey(
|
||||
body.stream_key,
|
||||
);
|
||||
|
||||
const stream = await Stream.findOneOrFail({
|
||||
where: { channel_id: channelId, owner_id: userId },
|
||||
});
|
||||
|
||||
const streamSession = StreamSession.create({
|
||||
stream_id: stream.id,
|
||||
user_id: this.user_id,
|
||||
session_id: this.session_id,
|
||||
token: genVoiceToken(),
|
||||
});
|
||||
|
||||
await streamSession.save();
|
||||
|
||||
const regions = Config.get().regions;
|
||||
const guildRegion = regions.available.find(
|
||||
(r) => r.endpoint === stream.endpoint,
|
||||
);
|
||||
|
||||
if (!guildRegion) return this.close(4000, "Unknown region");
|
||||
|
||||
const viewers = await StreamSession.find({
|
||||
where: { stream_id: stream.id },
|
||||
});
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_CREATE",
|
||||
data: {
|
||||
stream_key: body.stream_key,
|
||||
rtc_server_id: stream.id, // 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
|
||||
viewer_ids: viewers.map((v) => v.user_id),
|
||||
region: guildRegion.name,
|
||||
paused: false,
|
||||
},
|
||||
guild_id: guildId,
|
||||
channel_id: channelId,
|
||||
});
|
||||
|
||||
await emitEvent({
|
||||
event: "STREAM_SERVER_UPDATE",
|
||||
data: {
|
||||
token: streamSession.token,
|
||||
stream_key: body.stream_key,
|
||||
guild_id: null, // not sure why its always null
|
||||
endpoint: stream.endpoint,
|
||||
},
|
||||
user_id: this.user_id,
|
||||
});
|
||||
} catch (e) {
|
||||
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()!;
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
|
||||
const isNew = body.channel_id === null && body.guild_id === null;
|
||||
let isChanged = false;
|
||||
|
||||
let prevState;
|
||||
|
||||
let voiceState: VoiceState;
|
||||
try {
|
||||
voiceState = await VoiceState.findOneOrFail({
|
||||
@@ -60,6 +62,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
|
||||
|
||||
//If a user change voice channel between guild we should send a left event first
|
||||
if (
|
||||
voiceState.guild_id &&
|
||||
voiceState.guild_id !== body.guild_id &&
|
||||
voiceState.session_id === this.session_id
|
||||
) {
|
||||
@@ -71,7 +74,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
|
||||
}
|
||||
|
||||
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
|
||||
if (body.guild_id === null) body.guild_id = voiceState.guild_id;
|
||||
//if (body.guild_id === null) body.guild_id = voiceState.guild_id;
|
||||
prevState = { ...voiceState };
|
||||
voiceState.assign(body);
|
||||
} catch (error) {
|
||||
voiceState = VoiceState.create({
|
||||
@@ -83,34 +87,46 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
|
||||
});
|
||||
}
|
||||
|
||||
// 'Fix' for this one voice state error. TODO: Find out why this is sent
|
||||
// It seems to be sent on client load,
|
||||
// so maybe its trying to find which server you were connected to before disconnecting, if any?
|
||||
if (body.guild_id == null) {
|
||||
return;
|
||||
// if user left voice channel, send an update to previous channel/guild to let other people know that the user left
|
||||
if (
|
||||
voiceState.session_id === this.session_id &&
|
||||
body.guild_id == null &&
|
||||
body.channel_id == null &&
|
||||
(prevState?.guild_id || prevState?.channel_id)
|
||||
) {
|
||||
await emitEvent({
|
||||
event: "VOICE_STATE_UPDATE",
|
||||
data: { ...voiceState, channel_id: null, guild_id: null },
|
||||
guild_id: prevState?.guild_id,
|
||||
channel_id: prevState?.channel_id,
|
||||
});
|
||||
}
|
||||
|
||||
//TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user
|
||||
//TODO the member.user should only have these properties: avatar, discriminator, id, username
|
||||
//TODO this may fail
|
||||
voiceState.member = await Member.findOneOrFail({
|
||||
where: { id: voiceState.user_id, guild_id: voiceState.guild_id },
|
||||
relations: ["user", "roles"],
|
||||
});
|
||||
if (body.guild_id) {
|
||||
voiceState.member = await Member.findOneOrFail({
|
||||
where: { id: voiceState.user_id, guild_id: voiceState.guild_id },
|
||||
relations: ["user", "roles"],
|
||||
});
|
||||
}
|
||||
|
||||
//If the session changed we generate a new token
|
||||
if (voiceState.session_id !== this.session_id)
|
||||
voiceState.token = genVoiceToken();
|
||||
voiceState.session_id = this.session_id;
|
||||
|
||||
const { id, ...newObj } = voiceState;
|
||||
const { id, member, ...newObj } = voiceState;
|
||||
|
||||
await Promise.all([
|
||||
voiceState.save(),
|
||||
emitEvent({
|
||||
event: "VOICE_STATE_UPDATE",
|
||||
data: newObj,
|
||||
data: { ...newObj, member: member?.toPublicMember() },
|
||||
guild_id: voiceState.guild_id,
|
||||
channel_id: voiceState.channel_id,
|
||||
user_id: voiceState.user_id,
|
||||
} as VoiceStateUpdateEvent),
|
||||
]);
|
||||
|
||||
@@ -137,8 +153,10 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
|
||||
token: voiceState.token,
|
||||
guild_id: voiceState.guild_id,
|
||||
endpoint: guildRegion.endpoint,
|
||||
channel_id: voiceState.guild_id
|
||||
? undefined
|
||||
: voiceState.channel_id, // only DM voice calls have this set, and DM channel is one where guild_id is null
|
||||
},
|
||||
guild_id: voiceState.guild_id,
|
||||
user_id: voiceState.user_id,
|
||||
} as VoiceServerUpdateEvent);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { VoiceOPCodes } from "@spacebar/webrtc";
|
||||
|
||||
export enum OPCODES {
|
||||
Dispatch = 0,
|
||||
Heartbeat = 1,
|
||||
@@ -63,7 +61,7 @@ export enum CLOSECODES {
|
||||
}
|
||||
|
||||
export interface Payload {
|
||||
op: OPCODES | VoiceOPCodes;
|
||||
op: OPCODES;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
d?: any;
|
||||
s?: number;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
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;
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import { Intents, ListenEventOpts, Permissions } from "@spacebar/util";
|
||||
import WS from "ws";
|
||||
import { Deflate, Inflate } from "fast-zlib";
|
||||
import { Capabilities } from "./Capabilities";
|
||||
import { WebRtcClient } from "@spacebar/webrtc";
|
||||
|
||||
export interface WebSocket extends WS {
|
||||
version: number;
|
||||
@@ -42,7 +41,5 @@ export interface WebSocket extends WS {
|
||||
member_events: Record<string, () => unknown>;
|
||||
listen_options: ListenEventOpts;
|
||||
capabilities?: Capabilities;
|
||||
voiceWs?: WebRtcClient<WebSocket>;
|
||||
streamWs?: WebRtcClient<WebSocket>;
|
||||
large_threshold: number;
|
||||
}
|
||||
|
||||
@@ -22,3 +22,4 @@ export * from "./SessionUtils";
|
||||
export * from "./Heartbeat";
|
||||
export * from "./WebSocket";
|
||||
export * from "./Capabilities";
|
||||
export * from "./Utils";
|
||||
|
||||
Reference in New Issue
Block a user