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

175 lines
4.8 KiB
TypeScript

// Metro module 1342 (f1343).
import { UserSettingsTypes } from "./UserSettingsConstants";
import { ZERO_STRING_GUILD_ID } from "../../Constants";
import { base64decode, BinaryReader, base64encode } from "../../_bundle/Module1307";
import { PreloadedUserSettings, AllGuildSettings, GuildSettings, ChannelSettings } from "../../_bundle/Module1306";
import { FrecencyUserSettings, FrecencyItem } from "../../_bundle/Module1341";
import { Versions } from "../../_bundle/Module1335";
import Module12 from "../../_bundle/Module12";
let c1115_5, c1115_6;
const b64ToProto = function b64ToProto(a0, a1) {
let r16;
if (a1 == null) {
return null;
}
const r12 = base64decode(a1);
try {
r16 = a0.fromBinary(r12, c1115_5);
} catch ($e) {
throw new Error(`Settings proto failed to deserialize (potentially corrupt): ${$e}`);
}
return r16;
};
c1115_5 = {
readerFactory: (a0) => new BinaryReader(a0, new TextDecoder("utf-8"))
};
const r23 = {};
r23[UserSettingsTypes.PRELOADED_USER_SETTINGS] = PreloadedUserSettings;
r23[UserSettingsTypes.FRECENCY_AND_FAVORITES_SETTINGS] = FrecencyUserSettings;
c1115_6 = r23;
export { c1115_5 as BINARY_READ_OPTIONS };
export function b64ToProtoWithType(a0, a1) {
return a1 == null ? null : a0 in c1115_6 ? b64ToProto(c1115_6[a0], a1) : null;
}
export { b64ToProto };
export function b64ToPreloadedUserSettingsProto(a0) {
return b64ToProto(PreloadedUserSettings, a0);
}
export function protoToB64WithType(a0, a1) {
return base64encode(c1115_6[a0].toBinary(a1));
}
export function protoToB64(a0, a1) {
return base64encode(a0.toBinary(a1));
}
export function mergeTopLevelFields(a0, a1, a2) {
const r3 = {
...a1
};
for (const key in a2) {
delete r3[key];
}
a0.mergePartial(r3, a2);
return r3;
}
export function mutateUserGuildSettings(a0, a1, a2) {
if (a0.guilds == null) {
a0.guilds = AllGuildSettings.create();
}
let r13 = a1;
const guilds = a0.guilds;
if (!(a1 != null && a1 !== "null")) {
r13 = ZERO_STRING_GUILD_ID;
}
if (!(r13 in guilds.guilds)) {
guilds.guilds[r13] = GuildSettings.create();
}
return a2(guilds.guilds[r13]);
}
export function mutateUserGuildSettingsInternal(a0, a1, a2) {
const r4 = a1 != null && a1 !== "null" ? a1 : ZERO_STRING_GUILD_ID;
if (!(r4 in a0.guilds)) {
a0.guilds[r4] = GuildSettings.create();
}
return a2(a0.guilds[r4]);
}
export function mutateUserChannelSettings(a0, a1, a2, a3) {
if (a0.guilds == null) {
a0.guilds = AllGuildSettings.create();
}
let r14 = a1;
const guilds = a0.guilds;
if (!(a1 != null && a1 !== "null")) {
r14 = ZERO_STRING_GUILD_ID;
}
if (!(r14 in guilds.guilds)) {
guilds.guilds[r14] = GuildSettings.create();
}
const r29 = guilds.guilds[r14];
if (!(a2 in r29.channels)) {
r29.channels[a2] = ChannelSettings.create();
}
return a3(r29.channels[a2]);
}
export function mutateUserChannelSettingsInternal(a0, a1, a2) {
if (!(a1 in a0.channels)) {
a0.channels[a1] = ChannelSettings.create();
}
return a2(a0.channels[a1]);
}
export function runMigrations(a0, a1) {
if (a0.versions == null) {
a0.versions = Versions.create();
}
let r34 = 0;
for (const item of a1) {
if (item.version <= r34) {
throw Error("Migrations are out of order or there is a duplicate version");
}
r34 = item.version;
}
const r52 = Math.random() < 0.1;
let isDirty = false;
const cleanupFuncs = [];
for (const item2 of a1) {
if (item2.version <= a0.versions.clientVersion) {
if (r52) {
const cleanup2 = item2.cleanup;
if (cleanup2 != null) {
cleanup2.call(item2);
}
}
} else {
const r67 = item2.run(a0);
a0.versions.clientVersion = item2.version;
if (r67 !== false) {
isDirty = true;
if (item2.cleanup != null) {
cleanupFuncs.push(item2.cleanup);
}
} else {
const cleanup = item2.cleanup;
if (cleanup != null) {
cleanup.call(item2);
}
}
}
}
return {
proto: a0,
isDirty,
cleanupFuncs
};
}
export function serializeUsageHistory(a0, a1) {
let length3;
const r7 = Object.entries(a0);
const length = r7.length;
let r26 = r7;
if (length > a1) {
const r35 = Module12.sortBy(r7, (a0) => {
const [, r14] = a0;
return r14.recentUses[r14.recentUses.length - 1];
}).reverse();
const length2 = r35.length;
r26 = r35;
if (length2 > a1) {
do {
r35.pop();
length3 = r35.length;
r26 = r35;
} while (length3 > a1);
}
}
const r40 = {};
for (const item of r26) {
const [r50, r9] = item;
const r56 = FrecencyItem.create();
r56.frecency = r9.frecency;
r56.recentUses = r9.recentUses.filter((recentUse) => recentUse != null && recentUse > 0).map(String);
r56.score = Math.round(r9.score);
r56.totalUses = r9.totalUses;
r40[r50] = r56;
}
return r40;
}