Files
discord-decomp/stores/ApplicationStreamingStore.tsx
T
2026-11-01 12:12:55 +02:00

676 lines
22 KiB
TypeScript

// Metro module 4208 (f4209).
import GameConsoleStore from "../modules/game_console/GameConsoleStore";
import RunningGameStore_native from "../modules/game_detection/RunningGameStore.native";
import AuthenticationStore from "./AuthenticationStore";
import ChannelStore from "./ChannelStore";
import GuildStore from "./GuildStore";
import MediaEngineStore from "./MediaEngineStore";
import PermissionStore from "./PermissionStore";
import RTCConnectionStore from "./RTCConnectionStore";
import SelectedChannelStore from "./SelectedChannelStore";
import VoiceStateStore from "./VoiceStateStore";
import { ApplicationStreamDeleteReasons, ApplicationStreamStates, BasicPermissions, NULL_STRING_GUILD_ID, RTCConnectionStates } from "../Constants";
import { StreamTypes } from "../modules/go_live/Constants";
import Durations from "../utils/Durations";
import { decodeStreamKey, encodeStreamKey, isStreamKey } from "../modules/go_live/utils/StreamKeyUtils";
import windowSourceMatches from "../modules/go_live/utils/windowSourceMatches";
import getTitleFromPickedStreamContent from "../modules/go_live/utils/getTitleFromPickedStreamContent";
import { canWatchStream } from "../modules/go_live/utils/StreamPermissionUtils";
import Module589 from "../_bundle/Module589";
import canSpectate from "../modules/go_live/utils/canSpectate";
import Dispatcher from "../Dispatcher";
let c2428_3, streamsByUserAndGuild, rtcStreams, streamerActiveStreamMetadatas, c2428_7, id, selfStreamParticipantsHidden, intent, c2428_27, pid, gameForContent;
const reset = function reset() {
c2428_3 = new Map();
streamsByUserAndGuild = {};
rtcStreams = {};
streamerActiveStreamMetadatas = {};
c2428_7 = new Map();
};
const handleStreamUpdate = function handleStreamUpdate({ streamKey, region, viewerIds, paused }) {
const r15 = c2428_7.get(streamKey);
const r16 = r15 == null || Date.now() - r15 < c2428_27;
const r21 = r16 ? !r16 : c2428_7.delete(streamKey);
c2428_3.set(streamKey, {
...decodeStreamKey(streamKey),
state: paused ? ApplicationStreamStates.PAUSED : ApplicationStreamStates.ACTIVE
});
rtcStreams[streamKey] = {
streamKey,
region,
viewerIds
};
};
id = null;
selfStreamParticipantsHidden = {};
intent = null;
c2428_27 = 10 * Durations.Millis.SECOND;
c2428_3 = new Map();
streamsByUserAndGuild = {};
rtcStreams = {};
streamerActiveStreamMetadatas = {};
c2428_7 = new Map();
pid = void 0;
gameForContent = void 0;
class ApplicationStreamingStore extends Module589.PersistedStore {
static displayName = "ApplicationStreamingStore";
static persistKey = "ApplicationStreamingStore";
initialize(a0) {
this.syncWith([PermissionStore], () => true);
this.waitFor(AuthenticationStore, ChannelStore, PermissionStore, RTCConnectionStore, RunningGameStore_native, SelectedChannelStore);
if (a0?.selfStreamParticipantsHidden !== void 0) {
Object.assign(selfStreamParticipantsHidden, a0?.selfStreamParticipantsHidden);
}
}
getState() {
return {
selfStreamParticipantsHidden
};
}
isSelfStreamHidden(a0) {
let r4 = selfStreamParticipantsHidden[a0];
if (r4 == null) {
r4 = false;
}
return r4;
}
getLastActiveStream() {
const canSpectate2 = canSpectate(MediaEngineStore);
let r11 = null;
if (canSpectate2) {
let r18 = Array.from(c2428_3.values()).pop();
if (r18 == null) {
r18 = null;
}
r11 = r18;
}
return r11;
}
getAllActiveStreams() {
return canSpectate(MediaEngineStore) ? Array.from(c2428_3.values()) : [];
}
getAllActiveStreamsForChannel(c13982_0) {
return canSpectate(MediaEngineStore) ? Array.from(c2428_3.values()).filter((item) => item.channelId === c13982_0) : [];
}
getActiveStreamForStreamKey(a0) {
const canSpectate2 = canSpectate(MediaEngineStore);
let r10 = null;
if (canSpectate2) {
let r13 = c2428_3.get(a0);
if (r13 == null) {
r13 = null;
}
r10 = r13;
}
return r10;
}
getActiveStreamForApplicationStream(a0) {
if (canSpectate(MediaEngineStore) && a0 != null) {
let r17 = this.getActiveStreamForStreamKey(encodeStreamKey(a0));
if (r17 == null) {
r17 = null;
}
return r17;
}
return null;
}
getCurrentUserActiveStream() {
const channel = ChannelStore.getChannel(SelectedChannelStore.getVoiceChannelId());
return channel != null ? this.getActiveStreamForUser(AuthenticationStore.getId(), channel.getGuildId()) : null;
}
isStreamMarkedFull(a0) {
return c2428_7.has(a0);
}
getActiveStreamForUser(a0, a1) {
let r11;
const streamForUser = this.getStreamForUser(a0, a1);
if (streamForUser != null) {
r11 = this.getActiveStreamForApplicationStream(streamForUser);
} else {
r11 = this.getAllActiveStreams().find((item) => item.ownerId === a0);
if (r11 == null) {
r11 = null;
}
}
return r11;
}
getStreamerActiveStreamMetadata() {
const channel = ChannelStore.getChannel(SelectedChannelStore.getVoiceChannelId());
if (channel == null) {
return null;
}
const activeStreamForUser = this.getActiveStreamForUser(AuthenticationStore.getId(), channel.getGuildId());
const r1 = activeStreamForUser == null;
let r17 = null;
if (!r1) {
let r26 = streamerActiveStreamMetadatas[encodeStreamKey(activeStreamForUser)];
if (r26 == null) {
r26 = null;
}
r17 = r26;
}
return r17;
}
getStreamerActiveStreamMetadataForStream(a0) {
let r4 = streamerActiveStreamMetadatas[a0];
if (r4 == null) {
r4 = null;
}
return r4;
}
getIsActiveStreamPreviewDisabled(a0) {
let r6 = streamerActiveStreamMetadatas[a0]?.previewDisabled;
if (r6 == null) {
r6 = false;
}
return r6;
}
getAnyStreamForUser(a0) {
if (canSpectate(MediaEngineStore)) {
const r6 = streamsByUserAndGuild[a0];
const r1 = r6 == null;
let r14 = null;
if (!r1) {
let r20 = Object.values(r6).find((item) => {
const streamType = item.streamType;
const basicChannel = ChannelStore.getBasicChannel(item.channelId);
const r18 = streamType === StreamTypes.CALL || basicChannel != null && PermissionStore.canBasicChannel(BasicPermissions.VIEW_CHANNEL, basicChannel);
let r22 = true;
if (!r18) {
const basicChannel2 = ChannelStore.getBasicChannel(item.channelId);
r22 = basicChannel2 != null && canWatchStream(basicChannel2, VoiceStateStore, GuildStore, PermissionStore, GameConsoleStore)[0];
}
return r22;
});
if (r20 == null) {
r20 = null;
}
r14 = r20;
}
return r14;
}
return null;
}
getAnyDiscoverableStreamForUser(a0) {
if (canSpectate(MediaEngineStore)) {
const r6 = streamsByUserAndGuild[a0];
const r1 = r6 == null;
let r14 = null;
if (!r1) {
let r20 = Object.values(r6).find((item) => {
const streamType = item.streamType;
const basicChannel = ChannelStore.getBasicChannel(item.channelId);
const r19 = streamType === StreamTypes.CALL || basicChannel != null && PermissionStore.canBasicChannel(BasicPermissions.VIEW_CHANNEL, basicChannel);
let r23 = true;
if (!r19) {
const basicChannel2 = ChannelStore.getBasicChannel(item.channelId);
r23 = basicChannel2 != null && canWatchStream(basicChannel2, VoiceStateStore, GuildStore, PermissionStore, GameConsoleStore)[0];
}
if (r23) {
r23 = item.discoverable !== false;
}
return r23;
});
if (r20 == null) {
r20 = null;
}
r14 = r20;
}
return r14;
}
return null;
}
getStreamForUser(a0, a1) {
if (canSpectate(MediaEngineStore)) {
const r5 = streamsByUserAndGuild[a0];
const r25 = r5 == null ? void 0 : r5[a1 == null ? NULL_STRING_GUILD_ID : a1];
const r27 = r25 == null;
let r28 = null;
if (!r27) {
const streamType = r25.streamType;
const basicChannel = ChannelStore.getBasicChannel(r25.channelId);
const r32 = streamType === StreamTypes.CALL || basicChannel != null && PermissionStore.canBasicChannel(BasicPermissions.VIEW_CHANNEL, basicChannel);
let r36 = true;
if (!r32) {
const basicChannel2 = ChannelStore.getBasicChannel(r25.channelId);
r36 = basicChannel2 != null && canWatchStream(basicChannel2, VoiceStateStore, GuildStore, PermissionStore, GameConsoleStore)[0];
}
r28 = r36 ? r25 : null;
}
return r28;
}
return null;
}
getRTCStream(a0) {
const canSpectate2 = canSpectate(MediaEngineStore);
let r9 = null;
if (canSpectate2) {
let r12 = rtcStreams[a0];
if (r12 == null) {
r12 = null;
}
r9 = r12;
}
return r9;
}
getAllApplicationStreams() {
let r22;
const canSpectate2 = canSpectate(MediaEngineStore);
const r8 = [];
if (canSpectate2) {
for (const key in streamsByUserAndGuild) {
const r9 = streamsByUserAndGuild[key];
for (const key2 in r9) {
r8.push(streamsByUserAndGuild[key][key2]);
}
}
r22 = r8.filter((item) => {
let r1 = item != null;
if (r1) {
const streamType = item.streamType;
const basicChannel = ChannelStore.getBasicChannel(item.channelId);
r1 = streamType === StreamTypes.CALL || basicChannel != null && PermissionStore.canBasicChannel(BasicPermissions.VIEW_CHANNEL, basicChannel);
}
return r1;
});
} else {
r22 = r8;
}
return r22;
}
getAllApplicationStreamsForChannel(c13984_0) {
let r24;
const canSpectate2 = canSpectate(MediaEngineStore);
const r8 = [];
if (canSpectate2) {
for (const key in streamsByUserAndGuild) {
const r9 = streamsByUserAndGuild[key];
for (const key2 in r9) {
r8.push(streamsByUserAndGuild[key][key2]);
}
}
r24 = r8.filter((item) => {
let r2 = item != null && item.channelId === c13984_0;
if (r2) {
const streamType = item.streamType;
const basicChannel = ChannelStore.getBasicChannel(item.channelId);
r2 = streamType === StreamTypes.CALL || basicChannel != null && PermissionStore.canBasicChannel(BasicPermissions.VIEW_CHANNEL, basicChannel);
}
return r2;
});
} else {
r24 = r8;
}
return r24;
}
getViewerIds(a0) {
if (canSpectate(MediaEngineStore)) {
const r11 = typeof a0 !== "string" ? encodeStreamKey(a0) : a0;
const r17 = r11 != null ? rtcStreams[r11] : null;
return r17 != null ? r17.viewerIds : [];
}
return [];
}
getCurrentAppIntent() {
return intent;
}
getStreamingState() {
let r17;
const canSpectate2 = canSpectate(MediaEngineStore);
const r12 = {
activeStreams: null,
streamsByUserAndGuild: null,
rtcStreams: null,
streamerActiveStreamMetadatas: null
};
if (canSpectate2) {
r12.activeStreams = Array.from(c2428_3.entries());
r12.streamsByUserAndGuild = streamsByUserAndGuild;
r12.rtcStreams = rtcStreams;
r12.streamerActiveStreamMetadatas = streamerActiveStreamMetadatas;
r17 = r12;
} else {
r12.activeStreams = [];
r12.streamsByUserAndGuild = {};
r12.rtcStreams = {};
r12.streamerActiveStreamMetadatas = {};
r17 = r12;
}
return r17;
}
}
const applicationStreamingStore = new ApplicationStreamingStore(Dispatcher, {
MEDIA_ENGINE_SET_GO_LIVE_SOURCE: function handleSetGoLiveSource({ settings, endReason, errorCode }) {
let r3;
if (settings?.desktopSettings == null && settings?.cameraSettings == null) {
if (errorCode == null) {
return false;
}
let r2 = false;
let r23 = false;
for (const key in streamerActiveStreamMetadatas) {
r3 = r2;
r23 = r3;
const r17 = c2428_3.get(key);
r2 = r3;
if (r17 != null) {
c2428_3.set(key, {
...r17,
state: ApplicationStreamStates.FAILED,
endReason,
errorCode
});
r2 = true;
}
}
r3 = r2;
r23 = r3;
return r23;
}
return false;
},
NATIVE_SCREEN_SHARE_PICKER_UPDATE: function handleNativePickerUpdate({ existing, content }) {
let r3;
if (content == null) {
return false;
}
gameForContent = (function getGameForContent(a0) {
for (const application of a0.applications) {
const gameForPID = RunningGameStore_native.getGameForPID(application.id);
if (gameForPID == null) {
continue;
}
return gameForPID;
}
for (const window2 of a0.windows) {
const r40 = window2;
if (window2.owningApplication == null) {
continue;
}
const gameForPID2 = RunningGameStore_native.getGameForPID(r40.owningApplication.id);
if (gameForPID2 == null) {
continue;
}
return gameForPID2;
}
})(content);
pid = gameForContent?.pid;
if (existing) {
const titleFromPickedStreamContent = getTitleFromPickedStreamContent(content);
const r13 = {
pid,
id: gameForContent?.id
};
if (titleFromPickedStreamContent != null) {
r13.sourceName = titleFromPickedStreamContent;
}
let r2 = false;
let r35 = false;
for (const key in streamerActiveStreamMetadatas) {
r3 = r2;
r35 = r3;
const r16 = streamerActiveStreamMetadatas[key];
const r6 = r16 == null;
let r39 = void 0;
if (!r6) {
const sourceId2 = r16.sourceId;
r39 = sourceId2 == null ? void 0 : sourceId2.startsWith("prepicked:");
}
r2 = r3;
if (r39) {
streamerActiveStreamMetadatas[key] = {
...streamerActiveStreamMetadatas[key],
...r13
};
r2 = true;
}
}
r3 = r2;
r35 = r3;
return r35;
}
return false;
},
OVERLAY_INITIALIZE: function handleOverlayInitialize({ applicationStreamState }) {
streamsByUserAndGuild = applicationStreamState.streamsByUserAndGuild;
c2428_3 = new Map(applicationStreamState.activeStreams);
rtcStreams = applicationStreamState.rtcStreams;
streamerActiveStreamMetadatas = applicationStreamState.streamerActiveStreamMetadatas;
c2428_7 = new Map();
},
VOICE_STATE_UPDATES: function handleVoiceStateUpdates(a0) {
return a0.voiceStates.reduce((acc, { userId: userId2, guildId: guildId3, channelId: channelId2, sessionId, selfStream, discoverable }) => {
if (selfStream && channelId2 != null) {
const r39 = {
streamType: guildId3 != null ? StreamTypes.GUILD : StreamTypes.CALL,
ownerId: userId2,
guildId: guildId3,
channelId: channelId2,
discoverable
};
if (streamsByUserAndGuild[r39.ownerId] == null) {
streamsByUserAndGuild[r39.ownerId] = {};
}
const r48 = streamsByUserAndGuild[r39.ownerId];
let guildId4 = r39.guildId;
if (guildId4 == null) {
guildId4 = NULL_STRING_GUILD_ID;
}
r48[guildId4] = r39;
return true;
}
const id2 = AuthenticationStore.getId();
const sessionId2 = AuthenticationStore.getSessionId();
if (userId2 === id2 && sessionId !== sessionId2 && RTCConnectionStore.getChannelId() != null) {
return acc;
}
const userId = userId2;
const guildId2 = guildId3;
let c24974_2 = false;
c2428_7.forEach((item, index) => {
const r12 = decodeStreamKey(index);
let r0 = r12.ownerId === userId;
if (r0) {
let guildId3 = r12.guildId;
if (guildId3 == null) {
guildId3 = null;
}
let r15 = guildId2;
if (guildId2 == null) {
r15 = null;
}
r0 = guildId3 === r15;
}
if (r0) {
c24974_2 = c2428_7.delete(index) || c24974_2;
}
});
const r28 = guildId2 == null ? NULL_STRING_GUILD_ID : guildId2;
const r30 = streamsByUserAndGuild[userId];
let r32 = (r30 == null ? void 0 : r30[r28]) != null;
if (r32) {
delete streamsByUserAndGuild[userId][r28];
r32 = true;
}
if (!r32) {
r32 = c24974_2;
}
if (!r32) {
r32 = acc;
}
return r32;
}, false);
},
STREAM_WATCH: function handleStreamWatch({ streamKey }) {
const r15 = decodeStreamKey(streamKey);
c2428_3.delete(streamKey);
c2428_3.set(streamKey, {
...r15,
state: ApplicationStreamStates.CONNECTING
});
if (r15.ownerId === AuthenticationStore.getId()) {
selfStreamParticipantsHidden[r15.channelId] = false;
}
},
STREAM_START: function handleStreamStart(a0) {
const { streamType, guildId: guildId3, channelId: channelId2 } = a0;
let pid2 = a0.pid;
const { sourceName, sourceId, sourceIcon, previewDisabled } = a0;
const r23 = encodeStreamKey({
streamType,
guildId: guildId3,
channelId: channelId2,
ownerId: AuthenticationStore.getId()
});
if ((sourceId == null ? void 0 : sourceId.startsWith("prepicked:")) && pid2 == null) {
pid2 = pid;
}
let r33 = (sourceId == null ? void 0 : sourceId.startsWith("prepicked:")) && gameForContent != null ? gameForContent : pid2 != null ? RunningGameStore_native.getGameForPID(pid2) : sourceId != null ? RunningGameStore_native.getRunningGames().find((item) => windowSourceMatches(sourceId, item.windowHandle)) : null;
if (r33 == null) {
r33 = null;
}
streamerActiveStreamMetadatas[r23] = {
id: r33?.id,
pid: pid2,
sourceName,
previewDisabled,
sourceIcon,
sourceId
};
c2428_3.delete(r23);
c2428_3.set(r23, {
streamType,
guildId: guildId3,
channelId: channelId2,
ownerId: AuthenticationStore.getId(),
state: ApplicationStreamStates.CONNECTING
});
},
STREAM_STOP: function handleStreamStop(a0) {
streamerActiveStreamMetadatas[a0.streamKey] = null;
},
STREAM_CREATE: handleStreamUpdate,
STREAM_UPDATE: handleStreamUpdate,
STREAM_TIMED_OUT: function handleStreamTimedOut({ streamKey }) {
const r6 = c2428_3.get(streamKey);
if (r6 == null) {
return false;
}
c2428_3.set(streamKey, {
...r6,
state: ApplicationStreamStates.FAILED
});
},
STREAM_DELETE: function handleStreamDelete({ streamKey, unavailable, reason }) {
let guildId;
guildId = void 0;
delete rtcStreams[streamKey];
const STREAM_FULL = ApplicationStreamDeleteReasons.STREAM_FULL;
let r18 = false;
if (reason === STREAM_FULL) {
r18 = !c2428_7.has(streamKey);
c2428_7.set(streamKey, Date.now());
}
const r28 = c2428_3.get(streamKey);
if (r28 == null) {
return r18;
}
let ENDED = ApplicationStreamStates.ENDED;
if (unavailable) {
ENDED = ApplicationStreamStates.RECONNECTING;
} else if (reason === ApplicationStreamDeleteReasons.UNAUTHORIZED) {
ENDED = ApplicationStreamStates.FAILED;
} else if (reason === ApplicationStreamDeleteReasons.SAFETY_GUILD_RATE_LIMITED) {
guildId = decodeStreamKey(streamKey).guildId;
import("../modules/guild_limited_access/openGuildLimitedAccessInfoModal.native").then((result) => {
(0, result.default)(guildId);
});
ENDED = ApplicationStreamStates.ENDED;
} else if (r28.state === ApplicationStreamStates.FAILED && reason === ApplicationStreamDeleteReasons.USER_REQUESTED) {
ENDED = ApplicationStreamStates.FAILED;
}
c2428_3.set(streamKey, {
...r28,
state: ENDED
});
if (ENDED === ApplicationStreamStates.ENDED && id !== streamKey) {
c2428_3.delete(streamKey);
}
},
STREAM_CLOSE: function handleStreamClose(a0) {
c2428_3.delete(a0.streamKey);
},
STREAM_UPDATE_SELF_HIDDEN: function handleUpdateSelfStreamHidden({ channelId: channelId2, selfStreamHidden }) {
let r15 = isStreamKey(id);
if (r15) {
r15 = id == null ? void 0 : id.includes(AuthenticationStore.getId());
}
if (r15) {
r15 = selfStreamParticipantsHidden[channelId2] === false;
}
if (r15) {
r15 = selfStreamHidden === true;
}
if (r15) {
id = null;
}
selfStreamParticipantsHidden[channelId2] = selfStreamHidden;
},
VOICE_CHANNEL_SELECT: function handleVoiceChannelSelectForFullStreams({ channelId }) {
if (channelId == null) {
return false;
}
let c13987_1 = false;
c2428_7.forEach((item, index) => {
if (decodeStreamKey(index).channelId !== channelId) {
c13987_1 = c2428_7.delete(index) || c13987_1;
}
});
return c13987_1;
},
SET_STREAM_APP_INTENT: function handleStreamSetAppIntent(a0) {
intent = a0.intent;
},
RTC_CONNECTION_STATE: function handleRTCConnectionState({ streamKey, state }) {
if (streamKey == null) {
return false;
}
const r6 = c2428_3.get(streamKey);
if (r6 != null && r6.state !== ApplicationStreamStates.ENDED) {
if (r6.state === ApplicationStreamStates.FAILED && r6.ownerId === AuthenticationStore.getId()) {
return false;
}
let state2 = r6.state;
if (RTCConnectionStates.DISCONNECTED === state) {
state2 = ApplicationStreamStates.RECONNECTING;
} else if (RTCConnectionStates.RTC_CONNECTED === state) {
state2 = ApplicationStreamStates.ACTIVE;
}
if (state2 === r6.state) {
return false;
}
c2428_3.set(streamKey, {
...r6,
state: state2
});
return void 0;
}
return false;
},
CHANNEL_RTC_SELECT_PARTICIPANT: function handleStreamCloseAll({ id: id2, channelId: channelId2 }) {
id = id2;
Array.from(c2428_3.values()).forEach((item) => {
if (encodeStreamKey(item) !== id && item.state === ApplicationStreamStates.ENDED) {
c2428_3.delete(encodeStreamKey(item));
}
});
if (id2 != null && isStreamKey(id2) && id2.includes(AuthenticationStore.getId())) {
selfStreamParticipantsHidden[channelId2] = false;
}
},
CONNECTION_OPEN: reset,
CONNECTION_CLOSED: reset,
LOGOUT: reset
});
export default applicationStreamingStore;