Files
discord-decomp/modules/rpc/server/RPCServerManager.tsx
T
2026-11-01 12:12:55 +02:00

496 lines
22 KiB
TypeScript

// Metro module 13522 (f13523).
import _slicedToArray from "../../../_bundle/_slicedToArray";
import QuestStore from "../../quests/QuestStore";
import ChannelStore from "../../../stores/ChannelStore";
import GuildMemberStore from "../../../stores/GuildMemberStore";
import GuildStore from "../../../stores/GuildStore";
import MediaEngineStore from "../../../stores/MediaEngineStore";
import PresenceStore from "../../../stores/PresenceStore";
import RTCConnectionStore from "../../../stores/RTCConnectionStore";
import RelationshipStore from "../../../stores/RelationshipStore";
import SelectedChannelStore from "../../../stores/SelectedChannelStore";
import UserStore from "../../../stores/UserStore";
import VoiceStateStore from "../../../stores/VoiceStateStore";
import { ActivityActionTypes, AnalyticEvents, RPCCloseCodes, RPCEvents, RelationshipTypes } from "../../../Constants";
import { ActivityLayoutMode } from "../../activities/Constants";
import { FrameLayoutModes } from "../../frames/FramesConstants";
import { MediaEngineContextTypes } from "../../../_bundle/Module4250";
import { isPlatformEmbedded } from "../../../utils/PlatformUtils";
import Dispatcher from "../../../Dispatcher";
import AnalyticsUtils from "../../../utils/AnalyticsUtils";
import { BatchedStoreListener } from "../../../_bundle/Module589";
import { assertNever } from "../../../utils/GlobalUtils";
import { transformInternalTextMessage, getRemoteIconURL, transformBaseRelationship, transformApplicationRelationship } from "../RPCHelpers";
import { getThermalState } from "../../device/useThermalState";
import { activityInstanceConnectedParticipants } from "../helpers/activityInstanceConnectedParticipants";
import transformUser from "../helpers/transformUser";
import { getActivityApplicationId } from "../../quests/utils/QuestTaskUtils";
class RPCServerManager {
constructor(a0) {
const _this = this;
this.handleMessage = (a0) => {
let r7, r41, channelId, message;
if (_this.rpcServer.subscriptions.length !== 0) {
if (a0.type === "MESSAGE_CREATE") {
_this.handleActivityMessage(a0);
}
const type = a0.type;
if (type === "MESSAGE_CREATE") {
if (a0.message.state === "SENDING") {
return void 0;
}
const MESSAGE_CREATE = RPCEvents.MESSAGE_CREATE;
channelId = a0.channelId;
message = a0.message;
r7 = `${MESSAGE_CREATE}${a0.message.id}`;
r41 = MESSAGE_CREATE;
} else if (type === "MESSAGE_UPDATE") {
r41 = RPCEvents.MESSAGE_UPDATE;
channelId = a0.message.channel_id;
message = a0.message;
r7 = null;
} else {
if (type !== "MESSAGE_DELETE") {
return assertNever(a0);
}
const MESSAGE_DELETE = RPCEvents.MESSAGE_DELETE;
channelId = a0.channelId;
message = {
id: a0.id
};
r7 = `${MESSAGE_DELETE}${a0.id}`;
r41 = MESSAGE_DELETE;
}
if (channelId != null) {
_this.rpcServer.dispatchToSubscriptions(r41, {
channel_id: channelId
}, {
channel_id: channelId,
message: transformInternalTextMessage(message)
}, r7);
}
}
};
this.handleSpeaking = (a0) => {
if (_this.rpcServer.subscriptions.length !== 0) {
const r6 = a0.speakingFlags !== 0 ? RPCEvents.SPEAKING_START : RPCEvents.SPEAKING_STOP;
if (a0.context === MediaEngineContextTypes.DEFAULT) {
const voiceChannelId = SelectedChannelStore.getVoiceChannelId();
if (voiceChannelId != null) {
const channel = ChannelStore.getChannel(voiceChannelId);
if (channel == null) {
return void 0;
}
const voiceState = VoiceStateStore.getVoiceState(channel.getGuildId(), a0.userId);
if (voiceState == null) {
return void 0;
}
_this.rpcServer.dispatchToSubscriptions(r6, {
channel_id: voiceState.channelId
}, {
channel_id: voiceState.channelId,
user_id: a0.userId
});
}
}
}
};
this.handleVoiceChannelSelect = ({ guildId, channelId }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.VOICE_CHANNEL_SELECT, {}, {
channel_id: channelId,
guild_id: guildId
});
}
};
this.handleNotificationCreate = ({ channelId, message, icon, title, body }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
const rpcServer = _this.rpcServer;
const NOTIFICATION_CREATE = RPCEvents.NOTIFICATION_CREATE;
const r21 = {
channel_id: channelId,
message: transformInternalTextMessage(message),
icon_url: null,
title: null,
body: null
};
const icon_url = icon != null ? getRemoteIconURL(icon) : null;
const r30 = {};
r21.icon_url = icon_url;
r21.title = title;
r21.body = body;
rpcServer.dispatchToSubscriptions(NOTIFICATION_CREATE, r30, r21);
}
};
this.handleActivityJoin = ({ applicationId, parentApplicationId, secret, intent, embedded }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
const r15 = {
application_id: applicationId,
secret
};
if (embedded) {
r15.intent = intent;
}
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_JOIN, (a0) => a0.socket.application.id === applicationId || parentApplicationId != null && a0.socket.application.parentId === parentApplicationId, r15);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.GAME_JOIN, (a0) => a0.socket.application.id === applicationId, r15);
}
};
this.handleActivityLayoutModeUpdate = ({ applicationId: applicationId2, layoutMode }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_PIP_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId2, {
is_pip_mode: layoutMode !== ActivityLayoutMode.FOCUSED
});
const r21 = {
layout_mode: layoutMode
};
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_LAYOUT_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId2, r21);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.FRAME_LAYOUT_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId2, r21);
}
};
this.handleFrameUpdateLayoutMode = ({ applicationId: applicationId3, layoutMode }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
const layout_mode = layoutMode === FrameLayoutModes.PIP ? ActivityLayoutMode.PIP : ActivityLayoutMode.FOCUSED;
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_PIP_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId3, {
is_pip_mode: layout_mode !== ActivityLayoutMode.FOCUSED
});
const r26 = {
layout_mode
};
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_LAYOUT_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId3, r26);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.FRAME_LAYOUT_MODE_UPDATE, (a0) => a0.socket.application.id === applicationId3, r26);
}
};
this.handleThermalStateChange = ({ applicationId: applicationId4 }) => {
if (_this.rpcServer.subscriptions.length !== 0 && applicationId4 != null) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.THERMAL_STATE_UPDATE, (a0) => a0.socket.application.id === applicationId4, {
thermal_state: getThermalState()
});
}
};
this.handleScreenOrientationUpdate = ({ screenOrientation }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ORIENTATION_UPDATE, {}, {
screen_orientation: screenOrientation
});
}
};
this.handleEmbeddedActivityUpdate = () => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE, {}, activityInstanceConnectedParticipants());
}
};
this.handleActivityMessage = ({ channelId, message }) => {
let application_id;
application_id = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
const { application, activity } = message;
if (application != null && activity != null && activity.party_id != null) {
const user = UserStore.getUser(message.author?.id);
if (user != null) {
const currentUser = UserStore.getCurrentUser();
if (currentUser != null && user.id !== currentUser.id) {
const activity2 = activity.type === ActivityActionTypes.JOIN_REQUEST ? PresenceStore.getApplicationActivity(currentUser.id, application.id) : PresenceStore.getApplicationActivity(user.id, application.id);
if (activity2 != null && activity2.party != null && activity2.party.id === activity.party_id) {
application_id = activity2.application_id;
const type = activity.type;
if (ActivityActionTypes.JOIN === type) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_INVITE, (a0) => a0.socket.application.id === application_id, {
user: transformUser(user),
activity: activity2,
type: activity.type,
channel_id: channelId,
message_id: message.id
});
} else if (ActivityActionTypes.JOIN_REQUEST === type) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ACTIVITY_JOIN_REQUEST, (a0) => a0.socket.application.id === application_id, {
user: transformUser(user),
activity: activity2,
type: activity.type,
channel_id: channelId,
message_id: message.id
});
}
}
}
}
}
}
};
this.handleOAuth2TokenRevoke = ({ accessToken }) => {
_this.rpcServer.sockets.forEach((socket) => {
if (socket.authorization.accessToken === accessToken) {
socket.close(RPCCloseCodes.TOKEN_REVOKED, "Token revoked");
}
});
};
this.handleGuildCreate = (a0) => {
const id = a0.guild.id;
const guild = GuildStore.getGuild(id);
if (_this.rpcServer.subscriptions.length !== 0 && guild != null) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.GUILD_CREATE, {}, {
id,
name: guild.name
});
}
};
this.handleChannelCreate = ({ channel }) => {
const { id, name, type } = channel;
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.CHANNEL_CREATE, {}, {
id,
name,
type
});
}
};
this.handleLogout = () => {
_this.rpcServer.sockets.forEach((socket) => socket.close(RPCCloseCodes.CLOSE_NORMAL, "User logout"));
};
this.handleRelationshipAdd = ({ relationship }) => {
let c27486_0;
const { id, type } = relationship;
c27486_0 = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
const user = UserStore.getUser(id);
if (user != null) {
c27486_0 = transformBaseRelationship(type, user);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c27486_0, a0.socket.application.id));
}
}
};
this.handleRelationshipUpdate = ({ relationship }) => {
let c27487_0;
const { id, type } = relationship;
c27487_0 = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
const user = UserStore.getUser(id);
if (user != null) {
c27487_0 = transformBaseRelationship(type, user);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c27487_0, a0.socket.application.id));
}
}
};
this.handleRelationshipRemove = (a0) => {
let c27488_0;
const id = a0.relationship.id;
c27488_0 = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
const user = UserStore.getUser(id);
if (user != null) {
c27488_0 = transformBaseRelationship(RelationshipTypes.NONE, user);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c27488_0, a0.socket.application.id));
}
}
};
this.handlePresenceUpdates = ({ updates }) => {
let c27489_1;
c27489_1 = void 0;
let c27489_0 = _this;
if (_this.rpcServer.subscriptions.length !== 0) {
const set = new Set(updates.map((update) => update.user.id));
const _loop = function _loop() {
const relationshipType = RelationshipStore.getRelationshipType(c27489_1);
if (relationshipType === RelationshipTypes.NONE) {
return 0;
}
const user = UserStore.getUser(c27489_1);
if (user == null) {
return 0;
}
let c30633_0 = transformBaseRelationship(relationshipType, user);
c27489_0.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c30633_0, a0.socket.application.id));
};
for (const item of set.values()) {
c27489_1 = item;
_loop();
}
}
};
this.handlePresencesReplace = () => {
let c27490_1, c27490_2;
let c27490_0 = _this;
if (_this.rpcServer.subscriptions.length !== 0) {
const _loop2 = function _loop2() {
if (c27490_2 === RelationshipTypes.NONE) {
return 0;
}
const user = UserStore.getUser(c27490_1);
if (user == null) {
return 0;
}
let c30634_0 = transformBaseRelationship(c27490_2, user);
c27490_0.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c30634_0, a0.socket.application.id));
};
for (const item of RelationshipStore.getMutableRelationships().entries()) {
[c27490_1, c27490_2] = item;
_loop2();
}
}
};
this.handleUserUpdate = (a0) => {
let c27491_0;
const id = a0.user.id;
c27491_0 = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
const relationshipType = RelationshipStore.getRelationshipType(id);
if (relationshipType !== RelationshipTypes.NONE) {
const user = UserStore.getUser(id);
if (user != null) {
c27491_0 = transformBaseRelationship(relationshipType, user);
_this.rpcServer.dispatchToSubscriptions(RPCEvents.RELATIONSHIP_UPDATE, {}, (a0) => transformApplicationRelationship(c27491_0, a0.socket.application.id));
}
}
}
};
this.handleEntitlementCreate = ({ entitlement }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ENTITLEMENT_CREATE, (a0) => a0.socket.application.id === entitlement.application_id, {
entitlement
});
}
};
this.handleEntitlementDelete = ({ entitlement: entitlement2 }) => {
if (_this.rpcServer.subscriptions.length !== 0) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.ENTITLEMENT_DELETE, (a0) => a0.socket.application.id === entitlement2.application_id, {
entitlement: entitlement2
});
}
};
this.handleQuestEnrollSuccess = ({ enrolledQuestUserStatus }) => {
let questId, activityApplicationId;
questId = void 0;
activityApplicationId = void 0;
if (_this.rpcServer.subscriptions.length !== 0) {
questId = enrolledQuestUserStatus.questId;
const quest = QuestStore.getQuest(questId);
if (quest != null) {
activityApplicationId = getActivityApplicationId(quest);
if (activityApplicationId != null) {
_this.rpcServer.dispatchToSubscriptions(RPCEvents.QUEST_ENROLLMENT_STATUS_UPDATE, (a0) => a0.socket.application.id === activityApplicationId && a0.args?.quest_id === questId, {
quest_id: questId,
is_enrolled: enrolledQuestUserStatus.enrolledAt != null,
enrolled_at: enrolledQuestUserStatus.enrolledAt
});
}
}
}
};
this.rpcServer = a0.server;
this.transports = a0.transports;
this.rpcCommandHandlers = a0.commands;
this.rpcEventHandlers = a0.events;
this.stores = a0.stores;
this.registerTransportsForEmbeddedPlatform = a0.registerTransportsForEmbeddedPlatform;
}
loadServer() {
let r22;
if (isPlatformEmbedded) {
this.registerTransportsForEmbeddedPlatform();
}
let r31 = this.transports[Symbol.iterator]();
const next = r31.next;
while (true) {
const r7 = r31 === void 0 ? void 0 : (r22 = next.call(r31)).done ? r31 = void 0 : r22.value;
if (r31 === void 0) {
for (const item of Object.entries(this.rpcCommandHandlers)) {
const r50 = _slicedToArray(item, 2);
this.rpcServer.setCommandHandler(r50[0], r50[1]);
}
const rpcEventHandlers = this.rpcEventHandlers;
for (const item2 of Object.entries(rpcEventHandlers)) {
const r66 = _slicedToArray(item2, 2);
this.rpcServer.setEventHandler(r66[0], r66[1]);
}
return void 0;
} else {
try {
this.rpcServer.registerTransport(r7);
} catch (r37) {
if (r31 !== void 0 && typeof r31.return === "function") {
try {
r31.return();
} catch {}
}
throw r37;
}
}
}
}
init() {
this.rpcServer.getCurrentUser = () => UserStore.getCurrentUser();
this.rpcServer.onConnect = (a0) => {
Dispatcher.dispatch({
type: "RPC_APP_CONNECTED",
socketId: a0.id,
application: a0.application
});
AnalyticsUtils.track(AnalyticEvents.AUTHORIZED_APP_CONNECTED, {
app_id: a0.application.id,
transport: a0.transport
});
};
this.rpcServer.onDisconnect = (a0, a1) => {
Dispatcher.dispatch({
type: "RPC_APP_DISCONNECTED",
socketId: a0.id,
application: a0.application,
reason: a1
});
};
new BatchedStoreListener([ChannelStore, GuildMemberStore, PresenceStore, VoiceStateStore, MediaEngineStore, RTCConnectionStore].concat(this.stores), () => this.rpcServer.updateSubscriptions()).attach("RPCServerManager");
Dispatcher.subscribe("MESSAGE_CREATE", this.handleMessage);
Dispatcher.subscribe("MESSAGE_UPDATE", this.handleMessage);
Dispatcher.subscribe("MESSAGE_DELETE", this.handleMessage);
Dispatcher.subscribe("SPEAKING", this.handleSpeaking);
Dispatcher.subscribe("OAUTH2_TOKEN_REVOKE", this.handleOAuth2TokenRevoke);
Dispatcher.subscribe("GUILD_CREATE", this.handleGuildCreate);
Dispatcher.subscribe("CHANNEL_CREATE", this.handleChannelCreate);
Dispatcher.subscribe("LOGOUT", this.handleLogout);
Dispatcher.subscribe("VOICE_CHANNEL_SELECT", this.handleVoiceChannelSelect);
Dispatcher.subscribe("RPC_NOTIFICATION_CREATE", this.handleNotificationCreate);
Dispatcher.subscribe("ACTIVITY_JOIN", this.handleActivityJoin);
Dispatcher.subscribe("ACTIVITY_LAYOUT_MODE_UPDATE", this.handleActivityLayoutModeUpdate);
Dispatcher.subscribe("FRAME_UPDATE_LAYOUT_MODE", this.handleFrameUpdateLayoutMode);
Dispatcher.subscribe("THERMAL_STATE_CHANGE", this.handleThermalStateChange);
Dispatcher.subscribe("ACTIVITY_SCREEN_ORIENTATION_UPDATE", this.handleScreenOrientationUpdate);
Dispatcher.subscribe("EMBEDDED_ACTIVITY_UPDATE", this.handleEmbeddedActivityUpdate);
Dispatcher.subscribe("RELATIONSHIP_ADD", this.handleRelationshipAdd);
Dispatcher.subscribe("RELATIONSHIP_UPDATE", this.handleRelationshipUpdate);
Dispatcher.subscribe("RELATIONSHIP_REMOVE", this.handleRelationshipRemove);
Dispatcher.subscribe("PRESENCE_UPDATES", this.handlePresenceUpdates);
Dispatcher.subscribe("PRESENCES_REPLACE", this.handlePresencesReplace);
Dispatcher.subscribe("USER_UPDATE", this.handleUserUpdate);
Dispatcher.subscribe("ENTITLEMENT_CREATE", this.handleEntitlementCreate);
Dispatcher.subscribe("ENTITLEMENT_DELETE", this.handleEntitlementDelete);
Dispatcher.subscribe("QUESTS_ENROLL_SUCCESS", this.handleQuestEnrollSuccess);
}
terminate() {
Dispatcher.unsubscribe("MESSAGE_CREATE", this.handleMessage);
Dispatcher.unsubscribe("MESSAGE_UPDATE", this.handleMessage);
Dispatcher.unsubscribe("MESSAGE_DELETE", this.handleMessage);
Dispatcher.unsubscribe("SPEAKING", this.handleSpeaking);
Dispatcher.unsubscribe("OAUTH2_TOKEN_REVOKE", this.handleOAuth2TokenRevoke);
Dispatcher.unsubscribe("GUILD_CREATE", this.handleGuildCreate);
Dispatcher.unsubscribe("CHANNEL_CREATE", this.handleChannelCreate);
Dispatcher.unsubscribe("LOGOUT", this.handleLogout);
Dispatcher.unsubscribe("VOICE_CHANNEL_SELECT", this.handleVoiceChannelSelect);
Dispatcher.unsubscribe("RPC_NOTIFICATION_CREATE", this.handleNotificationCreate);
Dispatcher.unsubscribe("ACTIVITY_JOIN", this.handleActivityJoin);
Dispatcher.unsubscribe("ACTIVITY_LAYOUT_MODE_UPDATE", this.handleActivityLayoutModeUpdate);
Dispatcher.unsubscribe("FRAME_UPDATE_LAYOUT_MODE", this.handleFrameUpdateLayoutMode);
Dispatcher.unsubscribe("THERMAL_STATE_CHANGE", this.handleThermalStateChange);
Dispatcher.unsubscribe("ACTIVITY_SCREEN_ORIENTATION_UPDATE", this.handleScreenOrientationUpdate);
Dispatcher.unsubscribe("EMBEDDED_ACTIVITY_UPDATE", this.handleEmbeddedActivityUpdate);
Dispatcher.unsubscribe("RELATIONSHIP_ADD", this.handleRelationshipAdd);
Dispatcher.unsubscribe("RELATIONSHIP_UPDATE", this.handleRelationshipUpdate);
Dispatcher.unsubscribe("RELATIONSHIP_REMOVE", this.handleRelationshipRemove);
Dispatcher.unsubscribe("PRESENCE_UPDATES", this.handlePresenceUpdates);
Dispatcher.unsubscribe("PRESENCES_REPLACE", this.handlePresencesReplace);
Dispatcher.unsubscribe("USER_UPDATE", this.handleUserUpdate);
Dispatcher.unsubscribe("ENTITLEMENT_CREATE", this.handleEntitlementCreate);
Dispatcher.unsubscribe("ENTITLEMENT_DELETE", this.handleEntitlementDelete);
Dispatcher.unsubscribe("QUESTS_ENROLL_SUCCESS", this.handleQuestEnrollSuccess);
}
}
export default RPCServerManager;