summary refs log tree commit diff
path: root/src/gateway/opcodes/StreamDelete.ts
blob: 773b65087c0cf82f764d9f1723e1c0b98e2031a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2026 Spacebar and Spacebar Contributors

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { Member, Stream, VoiceState } from "@spacebar/database";
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
import { emitEvent, StreamDeleteEvent, VoiceStateUpdateEvent } from "@spacebar/util";
import { check } from "./instanceOf";
import { StreamDeleteSchema } from "@spacebar/schemas";

export async function onStreamDelete(this: WebSocket, data: Payload) {
    const startTime = Date.now();
    check.call(this, StreamDeleteSchema, data.d);
    const body = data.d as StreamDeleteSchema;

    let parsedKey: {
        type: "guild" | "call";
        channelId: string;
        guildId?: string;
        userId: string;
    };

    try {
        parsedKey = parseStreamKey(body.stream_key);
    } catch (e) {
        return this.rawSocket.close(4000, "Invalid stream key");
    }

    // noinspection JSUnusedLocalSymbols - TODO: what is type here?
    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) {
        await emitEvent({
            event: "STREAM_DELETE",
            data: {
                stream_key: body.stream_key,
            },
            user_id: this.user_id,
        } satisfies StreamDeleteEvent);
        return;
    }

    const stream = await Stream.findOne({
        where: { channel_id: channelId, owner_id: userId },
    });

    if (!stream) return;

    await stream.remove();

    const voiceState = await VoiceState.findOne({
        where: { user_id: this.user_id },
        // relations: { member: true }, // TODO: actually add the relation
    });

    if (voiceState) {
        voiceState.self_stream = false;
        await voiceState.save();
        voiceState.member = await Member.findOneOrFail({
            where: {
                id: voiceState.user_id,
                guild_id: voiceState.guild_id,
            },
        });

        await emitEvent({
            event: "VOICE_STATE_UPDATE",
            data: {
                ...voiceState.toPublicVoiceState(),
                member: voiceState.member.toPublicMember(),
            },
            guild_id: guildId,
            channel_id: channelId,
        } satisfies VoiceStateUpdateEvent);
    }

    await emitEvent({
        event: "STREAM_DELETE",
        data: {
            stream_key: body.stream_key,
        },
        guild_id: guildId,
        channel_id: channelId,
    } satisfies StreamDeleteEvent);

    console.log(`[Gateway/${this.user_id}] STREAM_DELETE for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`);
}