174 lines
6.0 KiB
TypeScript
174 lines
6.0 KiB
TypeScript
// Metro module 1935 (f1936).
|
|
import { constructInPlace } from "../lib/PlainRecord";
|
|
import { LibdiscoreStore } from "../modules/libdiscore/stores/LibdiscoreStore";
|
|
import { getGuildEveryoneRoleId } from "../records/GuildRecord";
|
|
import { GuildRoleRecordTypeTag } from "../records/GuildRoleRecord";
|
|
import { deserialize } from "../_bundle/HighLow";
|
|
import { fromSyncOperation, toSerializedPartition, fromServerArray, fromSerializedPartition, fromServer } from "../utils/GuildRoleRecordUtils";
|
|
import { sortGuildRoleRecords, filterRoleDeletes } from "../utils/GuildRoleUtils";
|
|
import { LibdiscoreBatchStoreRefactorExperiment } from "../modules/libdiscore/libdiscoreExperiments";
|
|
let syncRoles, checkGuildRolesExist;
|
|
const createGuildRoleRecordFromRust = function createGuildRoleRecordFromRust(a0) {
|
|
return constructInPlace(GuildRoleRecordTypeTag, {
|
|
...a0,
|
|
permissions: deserialize(a0.permissions)
|
|
});
|
|
};
|
|
syncRoles = function syncRoles(id, roles, a2) {
|
|
if (!(roles.op === "update" && roles.writes.length === 0 && roles.deletes.length === 0)) {
|
|
a2.setPartition(id, fromSyncOperation(id, roles, a2.getPartition(id)));
|
|
}
|
|
};
|
|
checkGuildRolesExist = function checkGuildRolesExist(a0, id, a2) {
|
|
if (a2.partitionLength(id) === 0) {
|
|
throw new Error(`Guild data was missing from store for guild ${id}: missing roles. (phase: ${a0})`);
|
|
}
|
|
};
|
|
class GuildRoleStore extends LibdiscoreStore {
|
|
static displayName = "GuildRoleStore";
|
|
database = this.addKKVDatabase("guild_roles", createGuildRoleRecordFromRust);
|
|
getSortedRoles = this.database.memoizedPartition((a0, a1) => sortGuildRoleRecords(Object.values(a1)));
|
|
getRolesSnapshot = this.database.memoizedPartition((a0, a1) => ({
|
|
...a1
|
|
}));
|
|
stateWrapper() {
|
|
return this.database;
|
|
}
|
|
serializeAllGuildRoles() {
|
|
return this.database.mapPartitions(toSerializedPartition);
|
|
}
|
|
getUnsafeMutableRoles(a0) {
|
|
return this.database.getPartition(a0);
|
|
}
|
|
getManyRoles(a0, a1) {
|
|
return this.database.getManyRecords(a0, a1);
|
|
}
|
|
getRole(a0, a1) {
|
|
return this.database.getRecord(a0, a1);
|
|
}
|
|
getNumRoles(a0) {
|
|
return this.database.partitionLength(a0);
|
|
}
|
|
getEveryoneRole(a0) {
|
|
const record = this.database.getRecord(a0.id, getGuildEveryoneRoleId(a0));
|
|
if (record == null) {
|
|
throw new Error("Guild does not have an @everyone role");
|
|
}
|
|
return record;
|
|
}
|
|
partitionVersion(a0) {
|
|
return this.database.partitionVersion(a0);
|
|
}
|
|
}
|
|
const guildRoleStore = new GuildRoleStore({
|
|
BACKGROUND_SYNC: (a0, a1) => {
|
|
let r53;
|
|
for (const guild of a0.guilds) {
|
|
const nullablePartition = a1.getNullablePartition(guild.id);
|
|
if (nullablePartition != null && guild.data_mode !== "unavailable") {
|
|
const setPartition = a1.setPartition;
|
|
const id = guild.id;
|
|
if (guild.data_mode === "partial") {
|
|
r53 = filterRoleDeletes(guild.id, nullablePartition, guild.partial_updates.roles, guild.partial_updates.deleted_role_ids);
|
|
} else {
|
|
r53 = fromServerArray(guild.id, guild.roles);
|
|
}
|
|
setPartition.call(a1, id, r53);
|
|
}
|
|
}
|
|
},
|
|
OVERLAY_INITIALIZE: (a0, a1) => {
|
|
a1.clear();
|
|
for (const serializedGuildRole of a0.serializedGuildRoles) {
|
|
const partitionKey = serializedGuildRole.partitionKey;
|
|
a1.setPartition(partitionKey, fromSerializedPartition(partitionKey, serializedGuildRole.values));
|
|
}
|
|
},
|
|
LOGOUT: (a0, a1) => {
|
|
a1.clear();
|
|
},
|
|
RESET_SOCKET: (a0, a1) => {
|
|
a1.clear();
|
|
},
|
|
CONNECTION_OPEN: ({ guilds, unavailableGuilds }, a1) => {
|
|
let r22;
|
|
const set = new Set(guilds.map((guild) => guild.id));
|
|
let r31 = unavailableGuilds[Symbol.iterator]();
|
|
const next = r31.next;
|
|
while (true) {
|
|
const r8 = r31 === void 0 ? void 0 : (r22 = next.call(r31)).done ? r31 = void 0 : r22.value;
|
|
if (r31 === void 0) {
|
|
for (const item of a1.getPartitionKeys()) {
|
|
if (!set.has(item)) {
|
|
a1.removePartition(item);
|
|
}
|
|
}
|
|
for (const item2 of guilds) {
|
|
const id = item2.id;
|
|
syncRoles(id, item2.roles, a1);
|
|
checkGuildRolesExist("connection_open", id, a1);
|
|
}
|
|
return void 0;
|
|
} else {
|
|
try {
|
|
set.add(r8);
|
|
} catch (r37) {
|
|
if (r31 !== void 0 && typeof r31.return === "function") {
|
|
try {
|
|
r31.return();
|
|
} catch {}
|
|
}
|
|
throw r37;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
CACHE_LOADED: ({ guilds }, a1) => {
|
|
a1.clear();
|
|
for (const item of guilds) {
|
|
const id = item.id;
|
|
a1.setPartition(id, fromSerializedPartition(id, item.roles));
|
|
checkGuildRolesExist("cache_loaded", id, a1);
|
|
}
|
|
},
|
|
CACHE_LOADED_LAZY: (a0, a1) => {
|
|
if (a0.guilds.length !== 0) {
|
|
a1.clear();
|
|
for (const guild of a0.guilds) {
|
|
const id = guild.id;
|
|
a1.setPartition(id, fromSerializedPartition(id, guild.roles));
|
|
checkGuildRolesExist("cache_loaded_lazy", id, a1);
|
|
}
|
|
}
|
|
},
|
|
GUILD_CREATE: ({ guild }, a1) => {
|
|
const { id, roles } = guild;
|
|
if (!(roles.op === "update" && roles.writes.length === 0 && roles.deletes.length === 0)) {
|
|
a1.setPartition(id, fromSyncOperation(id, roles, a1.getPartition(id)));
|
|
}
|
|
if (a1.partitionLength(id) === 0) {
|
|
throw new Error(`Guild data was missing from store for guild ${id}: missing roles. (phase: guild_create)`);
|
|
}
|
|
},
|
|
GUILD_UPDATE: ({ guild }, a1) => {
|
|
const id = guild.id;
|
|
a1.setPartition(id, fromServerArray(id, guild.roles));
|
|
},
|
|
GUILD_DELETE: ({ guild }, a1) => {
|
|
const id = guild.id;
|
|
if (!guild.unavailable) {
|
|
a1.removePartition(id);
|
|
}
|
|
},
|
|
GUILD_ROLE_CREATE: (a0, a1) => {
|
|
a1.setRecord(a0.guildId, a0.role.id, fromServer(a0.guildId, a0.role));
|
|
},
|
|
GUILD_ROLE_UPDATE: (a0, a1) => {
|
|
a1.setRecord(a0.guildId, a0.role.id, fromServer(a0.guildId, a0.role));
|
|
},
|
|
GUILD_ROLE_DELETE: (a0, a1) => {
|
|
a1.removeRecord(a0.guildId, a0.roleId);
|
|
}
|
|
}, LibdiscoreBatchStoreRefactorExperiment.getCachedBridgedStoreMode());
|
|
export default guildRoleStore;
|