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

487 lines
17 KiB
TypeScript

// Metro module 16149 (f16150).
import AuthenticationStore from "../../stores/AuthenticationStore";
import ChannelStore from "../../stores/ChannelStore";
import GuildAvailabilityStore from "../../stores/GuildAvailabilityStore";
import GuildStore from "../../stores/GuildStore";
import GatewayConnectionStore from "./GatewayConnectionStore";
import { AnalyticEvents } from "../../Constants";
import { ChannelFlags } from "../channel/ChannelConstants";
import Logger from "../debug/Logger";
import Durations from "../../utils/Durations";
import AutomaticLifecycleManager from "../../lib/AutomaticLifecycleManager";
import AnalyticsUtils from "../../utils/AnalyticsUtils";
import Dispatcher from "../../Dispatcher";
import GuildsRequiringChannelSync from "../app_database/modules/GuildsRequiringChannelSync";
import { isChannelMetadataIntegrityCheckEnabled } from "./PrivateChannelHidingExperiment";
import * as _Module1384 from "../../_bundle/Module1384";
import { v4 } from "../../_bundle/Module514";
function $mk117190($env) {
return (a0) => {
const r6 = $env[0];
if (!(r6 != null && a0.id !== r6)) {
const { id, requestId } = a0;
let env31790 = {
$parent: $env.$parent
};
env31790[0] = id;
c11570_17[id] = {
guildId: id,
requestId,
source: "resync",
requestedUserId: AuthenticationStore.getId()
};
if (c11570_19[id] != null) {
clearTimeout(c11570_19[id]);
}
c11570_19[id] = setTimeout(() => {
let r1;
const r12 = env31790.$parent;
const r11 = r12[19];
const guild_id3 = env31790[0];
delete r11[guild_id3];
const r15 = r12[0];
const r13 = r12[2];
if (r15(r13[14]).isChannelMetadataObfuscationEnabled("triggerGuildChannelResync")) {
let request_id2 = r12[17][guild_id3]?.requestId;
if (request_id2 == null) {
request_id2 = "unknown";
}
if (r12[7].getGuild(guild_id3) == null) {
(0, r12[1])(r13[11]).track(r12[9].GUILD_CHANNEL_RESYNC_FAILED, {
guild_id: guild_id3,
request_id: request_id2,
failure_reason: "guild_not_found"
});
const r85 = r12[20];
if (r85[guild_id3] != null) {
clearTimeout(r85[guild_id3]);
delete r85[guild_id3];
}
if (r11[guild_id3] != null) {
clearTimeout(r11[guild_id3]);
delete r11[guild_id3];
}
} else {
const r35 = [];
const mutableGuildChannelsForGuild2 = r12[5].getMutableGuildChannelsForGuild(guild_id3);
let r0 = 0;
let num_total_channels = 0;
for (const key in mutableGuildChannelsForGuild2) {
r1 = r0;
num_total_channels = r1;
const r20 = mutableGuildChannelsForGuild2[key];
if ((0, r12[0])(r12[2][15]).hasFlag(r20.flags, r12[10].OBFUSCATED)) {
r35.push(key);
}
r0 = r1 + 1;
}
r1 = r0;
num_total_channels = r1;
(0, r12[1])(r12[2][11]).track(r12[9].GUILD_CHANNEL_RESYNC_EXECUTED, {
guild_id: guild_id3,
request_id: request_id2,
num_obfuscated_channels: r35.length,
num_total_channels
});
r12[8].getSocket().triggerGuildChannelResync(guild_id3, r35);
let env32145 = {
$parent: r12
};
env32145[0] = guild_id3;
env32145[1] = request_id2;
const r64 = r12[20];
if (r64[guild_id3] != null) {
clearTimeout(r64[guild_id3]);
}
r64[guild_id3] = setTimeout(() => {
const guild_id3 = env32145[0];
const request_id2 = env32145[1];
const r12 = env32145.$parent;
r12[11].warn(`Resync timeout for guild ${guild_id3} with request ${request_id2}`);
(0, r12[1])(r12[2][11]).track(r12[9].GUILD_CHANNEL_RESYNC_FAILED, {
guild_id: guild_id3,
request_id: request_id2,
failure_reason: "timeout"
});
const r25 = r12[20];
if (r25[guild_id3] != null) {
clearTimeout(r25[guild_id3]);
delete r25[guild_id3];
}
const r30 = r12[19];
if (r30[guild_id3] != null) {
clearTimeout(r30[guild_id3]);
delete r30[guild_id3];
}
}, r12[15]);
(0, r12[32])(guild_id3);
}
}
}, Math.ceil(Math.random() * c11570_12));
}
};
}
function $mk117192($env) {
return (a0, a1) => {
if (GuildStore.getGuild(a1.id) == null && !GuildAvailabilityStore.isUnavailable(a1.id)) {
a0.ineligible.push(a1);
} else {
a0.eligible.push(a1);
}
return a0;
};
}
let handlePostConnectionOpen, handleChannelSync, handleLogout, scheduleGuildResyncs, scheduleIntegrityCheck, c11570_11, c11570_12, c11570_13, c11570_14, c11570_15, c11570_16, c11570_33, c11570_17, c11570_18, c11570_19, c11570_20, c11570_21, c11570_22, c11570_23;
let env11570 = {};
const handleGuildCreate = function handleGuildCreate({ guild }) {
let c22823_1;
c22823_1 = void 0;
if (guild.unavailable !== true) {
if (c11570_23[guild.id] != null) {
clearTimeout(c11570_23[guild.id]);
}
c22823_1 = env11570[24];
c11570_23[guild.id] = setTimeout(() => {
delete c11570_23[guild.id];
if (c22823_1 === env11570[24]) {
c11570_18.delete(guild.id);
scheduleGuildResyncs(guild.id);
scheduleIntegrityCheck(guild.id);
}
}, 0);
}
};
handlePostConnectionOpen = function handlePostConnectionOpen() {
c11570_18.clear();
scheduleGuildResyncs();
GuildStore.getGuildIds().forEach((item) => {
scheduleIntegrityCheck(item);
});
};
handleChannelSync = function handleChannelSync(a0) {
const { integrity_check, guild_id: guild_id3 } = a0;
if (integrity_check) {
const channels = a0.channels;
let request_id3 = c11570_17[guild_id3]?.requestId;
if (request_id3 == null) {
request_id3 = "unknown";
}
const mutableGuildChannelsForGuild = ChannelStore.getMutableGuildChannelsForGuild(guild_id3);
const set = new Set();
let c22824_1 = set;
const r61 = [];
let c22824_2 = r61;
channels.forEach((channel) => {
const r7 = mutableGuildChannelsForGuild[channel.id];
if (r7 != null) {
let flags = channel.flags;
if (flags == null) {
flags = 0;
}
const hasFlag = _Module1384.hasFlag(flags, ChannelFlags.OBFUSCATED);
const hasFlag2 = _Module1384.hasFlag(r7.flags, ChannelFlags.OBFUSCATED);
if (hasFlag !== hasFlag2) {
c11570_11.warn(`Integrity check failure: ${channel.id} serverObfuscated: ${hasFlag} != clientObfuscated: ${hasFlag2}`);
c22824_1.add(channel.id);
let flags2 = channel.flags;
if (flags2 == null) {
flags2 = 0;
}
let flags3 = r7.flags;
if (flags3 == null) {
flags3 = 0;
}
let parent_id = channel.parent_id;
if (parent_id == null) {
parent_id = null;
}
c22824_2.push({
channel_id: channel.id,
server_obfuscated: hasFlag,
client_obfuscated: hasFlag2,
server_flags: flags2,
client_flags: flags3,
channel_type: channel.type,
parent_id
});
}
} else {
c11570_11.warn(`Integrity check failure: ${channel.id} was missing.`);
}
});
const discrepancy_channel_ids = Array.from(set).join(", ");
const discrepancies_details = r61.length > 0 ? JSON.stringify(r61) : null;
(set.size > 0 ? c11570_11.warn : c11570_11.verbose)(`Integrity check for guild ${guild_id3} completed. Discrepancies found: ${discrepancy_channel_ids}`);
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_COMPLETED, {
guild_id: guild_id3,
request_id: request_id3,
num_channels_received: channels.length,
num_discrepancies_found: set.size,
discrepancy_channel_ids,
discrepancies_details
});
if (c11570_22[guild_id3] != null) {
clearTimeout(c11570_22[guild_id3]);
delete c11570_22[guild_id3];
}
if (c11570_21[guild_id3] != null) {
clearTimeout(c11570_21[guild_id3]);
delete c11570_21[guild_id3];
}
c11570_18.add(guild_id3);
} else {
let request_id2 = c11570_17[guild_id3]?.requestId;
if (request_id2 == null) {
request_id2 = "unknown";
}
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_RESYNC_COMPLETED, {
guild_id: a0.guild_id,
request_id: request_id2,
num_new_channels: a0.channels.length
});
const guild_id4 = a0.guild_id;
if (c11570_20[guild_id4] != null) {
clearTimeout(c11570_20[guild_id4]);
delete c11570_20[guild_id4];
}
if (c11570_19[guild_id4] != null) {
clearTimeout(c11570_19[guild_id4]);
delete c11570_19[guild_id4];
}
scheduleIntegrityCheck(a0.guild_id);
}
};
handleLogout = function handleLogout(a0) {
env11570[24] = env11570[24] + 1;
const cancellation_reason = a0.isSwitchingAccount === true ? "account_switch" : "logout";
const set = new Set([...Object.keys(c11570_19), ...Object.keys(c11570_20), ...Object.keys(c11570_21), ...Object.keys(c11570_22)]);
set.forEach((item) => {
const r9 = c11570_17[item];
if (r9 != null) {
const had_scheduled_timer = c11570_19[item] != null;
const had_pending_timeout = c11570_20[item] != null;
if (had_scheduled_timer || had_pending_timeout) {
const GUILD_CHANNEL_RESYNC_CANCELED = AnalyticEvents.GUILD_CHANNEL_RESYNC_CANCELED;
let requestedUserId = r9.requestedUserId;
if (requestedUserId == null) {
requestedUserId = null;
}
AnalyticsUtils.track(GUILD_CHANNEL_RESYNC_CANCELED, {
guild_id: item,
request_id: r9.requestId,
requested_user_id: requestedUserId,
cancellation_reason,
had_scheduled_timer,
had_pending_timeout
});
}
const had_scheduled_timer2 = c11570_21[item] != null;
const had_pending_timeout2 = c11570_22[item] != null;
if (had_scheduled_timer2 || had_pending_timeout2) {
const GUILD_CHANNEL_INTEGRITY_CHECK_CANCELED = AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_CANCELED;
let requestedUserId2 = r9.requestedUserId;
if (requestedUserId2 == null) {
requestedUserId2 = null;
}
AnalyticsUtils.track(GUILD_CHANNEL_INTEGRITY_CHECK_CANCELED, {
guild_id: item,
request_id: r9.requestId,
requested_user_id: requestedUserId2,
cancellation_reason,
had_scheduled_timer: had_scheduled_timer2,
had_pending_timeout: had_pending_timeout2
});
}
}
});
set.forEach((item) => {
if (c11570_20[item] != null) {
clearTimeout(c11570_20[item]);
delete c11570_20[item];
}
if (c11570_19[item] != null) {
clearTimeout(c11570_19[item]);
delete c11570_19[item];
}
if (c11570_22[item] != null) {
clearTimeout(c11570_22[item]);
delete c11570_22[item];
}
if (c11570_21[item] != null) {
clearTimeout(c11570_21[item]);
delete c11570_21[item];
}
});
for (const key in c11570_23) {
const r12 = clearTimeout;
r12(c11570_23[key]);
delete c11570_23[key];
}
for (const key2 in c11570_17) {
delete c11570_17[key2];
}
c11570_18.clear();
};
scheduleGuildResyncs = async function scheduleGuildResyncs(c28243_0) {
let env30966;
const r9 = env11570;
let c28243_2 = r9;
const r12 = env30966 = {
$parent: r9
};
let c28243_1 = r12;
env30966[0] = c28243_0;
env30966[2] = void 0;
env30966[3] = void 0;
env30966[1] = env11570[24];
const r35 = await (function getResyncGuilds() {
return env11570[31].apply(this, arguments);
})();
env30966[2] = r35;
if (env30966[1] === env11570[24]) {
c11570_11.verbose(`Resync guilds: ${JSON.stringify(env30966[2])}`);
env30966[2].eligible.forEach($mk117190(c28243_1));
if (env30966[2].ineligible.length > 0) {
env30966[3] = env30966[2].ineligible.map((item) => item.id);
c11570_11.verbose(`Guilds we are no longer part of are marked for resync. Unmarking them. Guilds: ${JSON.stringify(env30966[3])}`);
Dispatcher.dispatch({
type: "UNMARK_RESYNC_GUILDS",
guildIds: env30966[3]
});
}
}
};
env11570[31] = function _getResyncGuilds() {
const r8 = async () => {
let c28244_0 = env11570;
return (await GuildsRequiringChannelSync.getAll()).reduce($mk117192(c28244_0), {
eligible: [],
ineligible: []
});
};
env11570[31] = r8;
return r8.apply(this, arguments);
};
scheduleIntegrityCheck = function scheduleIntegrityCheck(a0) {
let requestId;
const guild_id = a0;
if (isChannelMetadataIntegrityCheckEnabled("scheduleIntegrityCheck")) {
const r2 = c11570_21[a0] != null;
if (c11570_22[a0] != null) {
clearTimeout(c11570_22[a0]);
delete c11570_22[a0];
}
if (c11570_21[a0] != null) {
clearTimeout(c11570_21[a0]);
delete c11570_21[a0];
}
const r32 = c11570_17[a0];
if (r32 != null) {
requestId = r32.requestId;
} else {
const requestId2 = v4();
c11570_17[a0] = {
guildId: a0,
requestId: requestId2,
source: "integrity_check",
requestedUserId: AuthenticationStore.getId()
};
requestId = requestId2;
}
if (!r2) {
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_REQUESTED, {
guild_id: a0,
request_id: requestId
});
}
c11570_21[a0] = setTimeout(() => {
delete c11570_21[guild_id];
if (isChannelMetadataIntegrityCheckEnabled("triggerIntegrityCheck") && !c11570_18.has(guild_id)) {
let request_id2 = c11570_17[guild_id]?.requestId;
if (request_id2 == null) {
request_id2 = "unknown";
}
if (GuildStore.getGuild(guild_id) == null) {
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_FAILED, {
guild_id,
request_id: request_id2,
failure_reason: "guild_not_found"
});
if (c11570_22[guild_id] != null) {
clearTimeout(c11570_22[guild_id]);
delete c11570_22[guild_id];
}
if (c11570_21[guild_id] != null) {
clearTimeout(c11570_21[guild_id]);
delete c11570_21[guild_id];
}
} else {
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_EXECUTED, {
guild_id,
request_id: request_id2
});
GatewayConnectionStore.getSocket().triggerGuildChannelResync(guild_id, null);
const guild_id2 = guild_id;
const request_id = request_id2;
if (c11570_22[guild_id] != null) {
clearTimeout(c11570_22[guild_id]);
}
c11570_22[guild_id] = setTimeout(() => {
c11570_11.warn(`Integrity check timeout for guild ${guild_id2} with request ${request_id}`);
AnalyticsUtils.track(AnalyticEvents.GUILD_CHANNEL_INTEGRITY_CHECK_FAILED, {
guild_id: guild_id2,
request_id,
failure_reason: "timeout"
});
if (c11570_22[guild_id2] != null) {
clearTimeout(c11570_22[guild_id2]);
delete c11570_22[guild_id2];
}
if (c11570_21[guild_id2] != null) {
clearTimeout(c11570_21[guild_id2]);
delete c11570_21[guild_id2];
}
}, c11570_16);
}
}
}, c11570_13 + Math.ceil(Math.random() * c11570_14));
}
};
c11570_11 = new Logger("ChannelResyncManager");
c11570_12 = 2 * Durations.Millis.SECOND;
c11570_13 = 30 * Durations.Millis.SECOND;
c11570_14 = 300 * Durations.Millis.SECOND;
c11570_15 = 30 * Durations.Millis.SECOND;
c11570_16 = 60 * Durations.Millis.SECOND;
class ChannelResyncManager extends AutomaticLifecycleManager {
actions = {
GUILD_CREATE: handleGuildCreate,
POST_CONNECTION_OPEN: handlePostConnectionOpen,
CHANNEL_SYNC: handleChannelSync,
LOGOUT: handleLogout
};
__unsafeGetTestState() {
return {
scheduledResyncTimerGuildIds: Object.keys(c11570_19),
pendingResyncTimeoutGuildIds: Object.keys(c11570_20),
scheduledIntegrityCheckTimerGuildIds: Object.keys(c11570_21),
pendingIntegrityCheckTimeoutGuildIds: Object.keys(c11570_22),
pendingGuildCreateDeferredGuildIds: Object.keys(c11570_23),
guildIdsWithLatestRequest: Object.keys(c11570_17),
guildsCompletedIntegrityCheck: Array.from(c11570_18),
sessionEpoch: env11570[24]
};
}
}
c11570_33 = ChannelResyncManager;
c11570_17 = {};
c11570_18 = new Set();
c11570_19 = {};
c11570_20 = {};
c11570_21 = {};
c11570_22 = {};
c11570_23 = {};
env11570[24] = 0;
const r96 = new ChannelResyncManager();
export default r96;