Files
discord-decomp/modules/gateway/GatewayConnectionStore.tsx
T
2026-11-01 12:12:55 +02:00

431 lines
16 KiB
TypeScript

// Metro module 4865 (f4866).
import UserSettingsProtoStore from "../user_settings/UserSettingsProtoStore";
import AuthenticationStore from "../../stores/AuthenticationStore";
import CallStore from "../../stores/CallStore";
import ChannelStore from "../../stores/ChannelStore";
import MediaEngineStore from "../../stores/MediaEngineStore";
import RTCConnectionStore from "../../stores/RTCConnectionStore";
import RTCRegionStore from "../../stores/RTCRegionStore";
import SelectedChannelStore from "../../stores/SelectedChannelStore";
import SelfPresenceStore from "../../stores/SelfPresenceStore";
import StreamRTCConnectionStore from "../../stores/StreamRTCConnectionStore";
import { AppStates, RTCConnectionStates } from "../../Constants";
import { UserSettingsTypes } from "../user_settings/UserSettingsConstants";
import * as _GatewaySocketSingleton from "./GatewaySocketSingleton";
import { localPresenceState, localVoiceState } from "./GatewaySocketSingleton";
import dispatchSocketMessage from "./dispatchSocketMessage";
import Logger from "../debug/Logger";
import { Storage } from "../../_bundle/Module595";
import { muteCustomJoinSound } from "../soundboard/SoundboardActionCreators";
import SelectedChannelActionCreators from "../../actions/SelectedChannelActionCreators";
import { isIOS } from "../../utils/PlatformUtils";
import { setIsPaused } from "./PauseGatewaySocket";
import Module12 from "../../_bundle/Module12";
import { decodeStreamKey } from "../go_live/utils/StreamKeyUtils";
import ConnectionState from "./ConnectionState";
import Module589 from "../../_bundle/Module589";
import Dispatcher from "../../Dispatcher";
let DiscordNative, c2996_19, c2996_20, sessionId, c2996_22, state, channelId, handleMediaEngineChange, handleLocalPresenceChange;
let _handleConnectionOpen = function _handleConnectionOpen() {
const r8 = async (c25303_0) => {
let c25303_2;
let c29444_0, c29444_1, c29444_2;
L37: {
L34: {
c25303_2 = void 0;
c29444_0 = void 0;
c29444_1 = void 0;
c29444_2 = void 0;
c2996_20 = Date.now();
sessionId = c25303_0.sessionId;
localPresenceState.handleConnectionOpen();
c29444_0 = {};
const voiceChannelId = SelectedChannelStore.getVoiceChannelId();
c29444_1 = voiceChannelId;
if (voiceChannelId != null) {
const r17 = Storage.get("discord_watchdog_restart_timestamp");
const r64 = r17 != null && Date.now() - parseInt(r17, 10) < 60000;
Storage.remove("discord_watchdog_restart_timestamp");
const r72 = window;
const r73 = r72 == null;
let r74 = void 0;
if (!r73) {
const performance2 = r72.performance;
const r84 = performance2 == null;
r74 = void 0;
if (!r84) {
const getEntriesByType = performance2.getEntriesByType;
const r86 = getEntriesByType == null;
r74 = void 0;
if (!r86) {
const r88 = getEntriesByType.call(performance2, "navigation");
r74 = r88 == null ? void 0 : r88[0]?.type;
}
}
}
if (r74 !== "reload" && !r64) {
const r101 = DiscordNative == null;
let r102 = void 0;
if (!r101) {
const processUtils = DiscordNative.processUtils;
const r105 = processUtils == null;
r102 = void 0;
if (!r105) {
const getLastCrash = processUtils.getLastCrash;
r102 = getLastCrash == null ? void 0 : getLastCrash.call(processUtils);
}
}
const rendererCrashReason = (await r102)?.rendererCrashReason;
L33: {
if (rendererCrashReason == null) {
if (c2996_22) {
RTCConnectionStore.setLastSessionVoiceChannelId(c29444_1 != null ? c29444_1 : null);
SelectedChannelActionCreators.selectVoiceChannel(null);
break L33;
}
}
c29444_2 = ChannelStore.getChannel(c29444_1);
if (c29444_2 != null) {
c29444_0 = {
guildId: c29444_2.getGuildId(),
channelId: c29444_1
};
muteCustomJoinSound(c29444_1);
}
}
localVoiceState.update(c29444_0, true);
c2996_22 = false;
channelId = null;
break L34;
}
c29444_2 = ChannelStore.getChannel(c29444_1);
if (c29444_2 != null) {
c29444_0 = {
guildId: c29444_2.getGuildId(),
channelId: c29444_1
};
muteCustomJoinSound(c29444_1);
}
}
localVoiceState.update(c29444_0, true);
c2996_22 = false;
channelId = null;
break L37;
}
return void 0;
}
};
_handleConnectionOpen = r8;
return r8.apply(this, arguments);
};
const handleClipsFlags = function handleClipsFlags() {
localVoiceState.update();
};
handleMediaEngineChange = function handleMediaEngineChange() {
localVoiceState.update();
return false;
};
handleLocalPresenceChange = function handleLocalPresenceChange() {
localPresenceState.update();
return false;
};
DiscordNative = window.DiscordNative;
_GatewaySocketSingleton.socket.dispatcher.getDispatchHandler = dispatchSocketMessage;
c2996_19 = new Logger("ConnectionStore");
c2996_20 = 0;
sessionId = null;
c2996_22 = true;
state = null;
channelId = null;
class GatewayConnectionStore extends Module589.Store {
static displayName = "GatewayConnectionStore";
initialize() {
this.waitFor(AuthenticationStore, CallStore, ChannelStore, MediaEngineStore, RTCConnectionStore, RTCRegionStore, SelectedChannelStore, SelfPresenceStore, StreamRTCConnectionStore, UserSettingsProtoStore);
this.syncWith([MediaEngineStore], handleMediaEngineChange);
this.syncWith([SelfPresenceStore], handleLocalPresenceChange);
}
getSocket() {
return _GatewaySocketSingleton.socket;
}
isTryingToConnect() {
return !_GatewaySocketSingleton.socket.isClosed();
}
isConnected() {
return _GatewaySocketSingleton.socket.isSessionEstablished();
}
isConnectedOrOverlay() {
return _GatewaySocketSingleton.socket.isSessionEstablished() || false;
}
lastTimeConnectedChanged() {
return c2996_20;
}
}
const gatewayConnectionStore = new GatewayConnectionStore(Dispatcher, {
START_SESSION: function handleSessionStart() {
let r13;
if (_GatewaySocketSingleton.socket.isClosed()) {
c2996_19.verbose("Socket is reconnecting because of starting new session");
r13 = _GatewaySocketSingleton.socket.connect();
} else {
c2996_19.verbose("Socket is not reconnecting during a new session because it is not closed");
r13 = false;
}
return r13;
},
LOGIN_SUCCESS: function handleSessionRefresh() {
c2996_19.verbose("session refresh dispatched", {
isEstablished: _GatewaySocketSingleton.socket.isSessionEstablished()
});
let r20 = !!_GatewaySocketSingleton.socket.isSessionEstablished();
if (r20) {
_GatewaySocketSingleton.socket.close();
r20 = _GatewaySocketSingleton.socket.connect();
}
return r20;
},
LOGOUT: function handleLogout(a0) {
if (a0.isSwitchingAccount) {
localPresenceState.handleAccountSwitch();
}
c2996_19.verbose("Closing socket because of logout");
_GatewaySocketSingleton.socket.close();
},
CLEAR_CACHES: function handleClearCaches(a0) {
if (a0.resetSocket) {
_GatewaySocketSingleton.socket.close();
_GatewaySocketSingleton.socket.dispatcher.clear();
_GatewaySocketSingleton.socket.connect();
}
return false;
},
CONNECTION_OPEN: (a0) => {
(function handleConnectionOpen() {
return _handleConnectionOpen.apply(this, arguments);
})(a0);
},
CONNECTION_RESUMED: function handleConnectionResumed() {
channelId = null;
},
CONNECTION_CLOSED: function handleConnectionClosed() {
c2996_19.verbose("connection closed dispatched");
c2996_20 = Date.now();
},
RTC_CONNECTION_STATE: function handleRTCConnectionState(a0) {
if (a0.state !== RTCConnectionStates.DISCONNECTED) {
return false;
}
if (a0.willReconnect) {
if (a0.streamKey != null) {
_GatewaySocketSingleton.socket.streamPing(a0.streamKey);
} else {
_GatewaySocketSingleton.socket.voiceServerPing();
}
}
},
VOICE_CHANNEL_SELECT: function handleVoiceChannelSelect(a0) {
localVoiceState.update({
guildId: a0.guildId,
channelId: a0.channelId
});
channelId = a0.lockVoiceStateForResume ? a0.channelId != null ? a0.channelId : null : null;
if (isIOS() && state === AppStates.BACKGROUND) {
if (a0.channelId == null) {
_GatewaySocketSingleton.socket.close(true);
} else if (_GatewaySocketSingleton.socket.isClosed()) {
setIsPaused(false);
_GatewaySocketSingleton.socket.connect();
}
}
return false;
},
VOICE_STATE_UPDATES: function handleVoiceStateUpdates(a0) {
return a0.voiceStates.reduce((acc, voiceState) => {
if (AuthenticationStore.getId() !== voiceState.userId) {
return acc;
}
if (voiceState.sessionId === sessionId) {
if (channelId != null) {
c2996_19.verbose("Ignoring voice state for own session due to VSU lock on channel:", channelId);
return acc;
}
localVoiceState.setState({
guildId: voiceState.guildId,
channelId: voiceState.channelId
});
} else {
if (voiceState.guildId !== localVoiceState.guildId) {
return acc;
}
localVoiceState.setState({
guildId: null,
channelId: null
});
}
return true;
}, false);
},
GUILD_DELETE: function handleGuildDelete(a0) {
if (a0.guild.id === localVoiceState.guildId) {
localVoiceState.setState({
guildId: null,
channelId: null
});
}
},
CHANNEL_DELETE: function handleChannelDelete(a0) {
if (a0.channel.id === localVoiceState.channelId) {
localVoiceState.setState({
guildId: null,
channelId: null
});
}
},
CALL_DELETE: function handleCallDelete({ channelId: channelId2 }) {
if (channelId2 === localVoiceState.channelId) {
if (channelId === channelId2) {
return false;
}
localVoiceState.setState({
guildId: null,
channelId: null
});
}
},
APP_STATE_UPDATE: function handleFocus(a0) {
if (isIOS()) {
L15: {
if (AuthenticationStore.isAuthenticated()) {
if (state === AppStates.INACTIVE && a0.state === AppStates.BACKGROUND && localVoiceState.channelId == null) {
_GatewaySocketSingleton.socket.close(true);
break L15;
}
if (state === AppStates.BACKGROUND && a0.state === AppStates.ACTIVE && _GatewaySocketSingleton.socket.isClosed()) {
setIsPaused(false);
_GatewaySocketSingleton.socket.connect();
}
}
}
state = a0.state;
} else if (a0.state === AppStates.ACTIVE) {
setIsPaused(false);
if (AuthenticationStore.isAuthenticated()) {
_GatewaySocketSingleton.socket.resetBackoff("App state is active");
}
}
return false;
},
GUILD_MEMBERS_REQUEST: function handleGuildMembersRequest(a0) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
if ("userIds" in a0) {
Module12(a0.userIds).chunk(100).forEach((item) => {
_GatewaySocketSingleton.socket.requestGuildMembers(a0.guildIds, {
userIds: item,
presences: !!a0.presences
});
});
} else {
_GatewaySocketSingleton.socket.requestGuildMembers(a0.guildIds, {
query: a0.query,
limit: a0.limit,
presences: !!a0.presences
});
}
}
return false;
},
GUILD_SEARCH_RECENT_MEMBERS: function handleGuildSearchRecentMembers({ guildId, query, continuationToken }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.searchRecentMembers(guildId, {
query,
continuationToken
});
}
},
GUILD_SUBSCRIPTIONS_FLUSH: function handleGuildSubscriptionsFlush({ subscriptions }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.updateGuildSubscriptions(subscriptions);
}
return false;
},
CALL_CONNECT: function handleCallConnect({ channelId: channelId2 }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.callConnect(channelId2);
}
return false;
},
CALL_CONNECT_MULTIPLE: function handleCallConnectMultiple({ channelIds }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
channelIds.forEach((channelId) => {
_GatewaySocketSingleton.socket.callConnect(channelId);
});
}
return false;
},
STREAM_CREATE: handleClipsFlags,
STREAM_START: function handleStreamStart({ streamType, guildId, channelId: channelId2 }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
let r24 = guildId != null ? ChannelStore.getChannel(channelId2)?.rtcRegion : CallStore.getCall(channelId2)?.region;
if (r24 == null) {
r24 = RTCRegionStore.getPreferredRegion();
}
_GatewaySocketSingleton.socket.streamCreate(streamType, guildId, channelId2, r24);
}
return false;
},
STREAM_WATCH: function handleStreamWatch({ streamKey, allowMultiple }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
if (!allowMultiple) {
let c14477_0 = StreamRTCConnectionStore.getAllActiveStreamKeys().find((item) => decodeStreamKey(item).ownerId === AuthenticationStore.getId());
StreamRTCConnectionStore.getAllActiveStreamKeys().filter((item) => item !== c14477_0).forEach((item) => {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.streamDelete(item);
}
});
}
_GatewaySocketSingleton.socket.streamWatch(streamKey);
}
return false;
},
STREAM_STOP: function handleStreamStop({ streamKey }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.streamDelete(streamKey);
}
localVoiceState.update();
return false;
},
STREAM_SET_PAUSED: function handleStreamSetPaused({ streamKey, paused }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.streamSetPaused(streamKey, paused);
}
},
PUSH_NOTIFICATION_CLICK: function handlePushNotificationClick() {
_GatewaySocketSingleton.socket.expeditedHeartbeat(5000, "user clicked on notification", true);
return false;
},
REQUEST_FORUM_UNREADS: function handleRequestForumUnreads(a0) {
_GatewaySocketSingleton.socket.requestForumUnreads(a0.guildId, a0.channelId, a0.threads);
},
REQUEST_SOUNDBOARD_SOUNDS: function handleRequestSoundboardSounds(a0) {
_GatewaySocketSingleton.socket.requestSoundboardSounds(a0.guildIds);
},
REMOTE_COMMAND: function handleRemoteCommand({ sessionId: sessionId2, payload }) {
if (_GatewaySocketSingleton.socket.isSessionEstablished()) {
_GatewaySocketSingleton.socket.remoteCommand(sessionId2, payload);
}
return false;
},
RESET_SOCKET: function handleResetSocket(a0) {
if (_GatewaySocketSingleton.socket.connectionState !== ConnectionState.WILL_RECONNECT) {
_GatewaySocketSingleton.socket.resetSocketAndClearCacheOnError(a0.args);
}
},
CLIPS_SETTINGS_UPDATE: handleClipsFlags,
RUNNING_GAMES_CHANGE: handleClipsFlags,
USER_SETTINGS_PROTO_UPDATE: function handleUserSettingsProtoUpdate(a0) {
if (a0.settings.type === UserSettingsTypes.PRELOADED_USER_SETTINGS && a0.settings.proto.clips?.allowVoiceRecording != null) {
localVoiceState.update();
}
}
});
export default gatewayConnectionStore;