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

165 lines
5.7 KiB
TypeScript

// Metro module 1862 (f1863).
import { constructInPlace, set } from "../lib/PlainRecord";
import { LibdiscoreStore } from "../modules/libdiscore/stores/LibdiscoreStore";
import { GuildRecordTypeTag, updateGameApplications, updateJoinedAt } from "../records/GuildRecord";
import AuthenticationStore from "./AuthenticationStore";
import { FAVORITES_GUILD_RECORD } from "../modules/favorites/FavoritesConstants";
import { toSetInplace } from "../_bundle/Module1414";
import { isFavoritesGuildId } from "../modules/favorites/FavoritesUtils";
import SnowflakeUtils from "../utils/SnowflakeUtils";
import { fromBackgroundSync, fromServer, fromGuildPropertiesWithAdditionalFields, fromSerializedGuildRecord, fromGuild } from "../utils/GuildRecordUtils";
import { LibdiscoreBatchStoreRefactorExperiment } from "../modules/libdiscore/libdiscoreExperiments";
const createGuildRecordFromRust = function createGuildRecordFromRust(a0) {
return constructInPlace(GuildRecordTypeTag, {
...a0,
features: toSetInplace(a0.features),
joinedAt: a0.joinedAt != null ? new Date(a0.joinedAt) : null,
premiumProgressBarEnabledUserUpdatedAt: a0.premiumProgressBarEnabledUserUpdatedAt != null ? new Date(a0.premiumProgressBarEnabledUserUpdatedAt) : null
});
};
class GuildStore extends LibdiscoreStore {
static displayName = "GuildStore";
database = this.addKVDatabase("guilds", createGuildRecordFromRust);
getGuild = (a0) => {
if (a0 != null) {
return isFavoritesGuildId(a0) ? FAVORITES_GUILD_RECORD : this.database.get(a0);
}
};
getGuilds = this.database.memoized((a0) => ({
...a0
}));
getGuildsArray = this.database.memoized((a0) => Object.values(a0));
getGuildIds = this.database.memoized((a0) => SnowflakeUtils.keys(a0));
stateWrapper() {
return this.database;
}
getGuildCount() {
return this.database.length();
}
}
const guildStore = new GuildStore({
BACKGROUND_SYNC: (a0, a1) => {
for (const guild of a0.guilds) {
const r34 = a1.get(guild.id);
if (r34 != null && guild.data_mode !== "unavailable") {
const set2 = a1.set;
set2.call(a1, guild.id, fromBackgroundSync(guild, r34));
}
}
},
LOGOUT: (a0, a1) => {
a1.clear();
},
RESET_SOCKET: (a0, a1) => {
a1.clear();
},
CONNECTION_OPEN: ({ guilds, unavailableGuilds }, a1) => {
let r28;
const allRecords = a1.getAllRecords();
const set2 = new Set(Object.keys(allRecords));
let r37 = guilds[Symbol.iterator]();
const next = r37.next;
while (true) {
const r40 = r37 === void 0 ? void 0 : (r28 = next.call(r37)).done ? r37 = void 0 : r28.value;
if (r37 === void 0) {
for (const item of unavailableGuilds) {
set2.delete(item);
}
for (const item2 of set2) {
a1.remove(item2);
}
return void 0;
} else {
try {
set2.delete(r40.id);
if (r40.properties == null && allRecords[r40.id] == null) {
throw new Error("Guild data was missing from store, but hash was still available.");
}
const set3 = a1.set;
set3.call(a1, r40.id, fromServer(r40, allRecords[r40.id]));
} catch (r63) {
if (r37 !== void 0 && typeof r37.return === "function") {
try {
r37.return();
} catch {}
}
throw r63;
}
}
}
},
OVERLAY_INITIALIZE: ({ guilds }, a1) => {
a1.clear();
if (guilds != null) {
for (const item of guilds) {
const { properties, additionalFields } = item;
a1.set.call(a1, properties.id, fromGuildPropertiesWithAdditionalFields(properties, {
joinedAt: additionalFields.joinedAt != null ? new Date(additionalFields.joinedAt) : null,
premiumSubscriberCount: additionalFields.premiumSubscriberCount
}));
}
}
},
CACHE_LOADED: ({ guilds }, a1) => {
a1.clear();
for (const item of guilds) {
a1.set(item.id, fromSerializedGuildRecord(item));
}
},
CACHE_LOADED_LAZY: ({ guilds }, a1) => {
if (guilds.length !== 0) {
a1.clear();
for (const item of guilds) {
a1.set(item.id, fromSerializedGuildRecord(item));
}
}
},
GUILD_CREATE: ({ guild }, a1) => {
const r6 = a1.get(guild.id);
if (guild.properties == null && r6 == null) {
throw new Error("Guild data was missing from store, but hash was still available.");
}
a1.set(guild.id, fromServer(guild, r6));
},
GUILD_UPDATE: ({ guild }, a1) => {
a1.set(guild.id, fromGuild(guild, a1.get(guild.id)));
},
GUILD_THEME_PREVIEW_SAVE_SUCCESS: ({ guildId, guildTheme }, a1) => {
const r6 = a1.get(guildId);
if (r6 != null) {
a1.set(guildId, set(r6, "guildTheme", guildTheme));
}
},
GUILD_SETTINGS_GUILD_THEME_SAVE_SUCCESS: ({ guildId, guildTheme }, a1) => {
const r6 = a1.get(guildId);
if (r6 != null) {
a1.set(guildId, set(r6, "guildTheme", guildTheme));
}
},
GUILD_DELETE: ({ guild }, a1) => {
if (!guild.unavailable) {
a1.remove(guild.id);
}
},
GUILD_MEMBER_ADD: ({ guildId, joinedAt, user }, a1) => {
const id = AuthenticationStore.getId();
const r16 = a1.get(guildId);
if (id === user.id && r16 != null) {
let r18 = joinedAt;
if (typeof r18 === "string") {
r18 = new Date(joinedAt);
}
if (r18 !== r16.joinedAt && r18 != null) {
a1.set(guildId, updateJoinedAt(r16, r18));
}
}
},
GUILD_OFFICIAL_GAME_APPLICATIONS_UPDATE: ({ guildId, gameApplicationIds }, a1) => {
const r5 = a1.get(guildId);
if (r5 != null) {
a1.set(guildId, updateGameApplications(r5, gameApplicationIds));
}
}
}, LibdiscoreBatchStoreRefactorExperiment.getCachedBridgedStoreMode());
export default guildStore;