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

443 lines
13 KiB
TypeScript

// Metro module 1932 (f1933).
import GatedChannelStore from "../modules/channel/GatedChannelStore";
import FavoriteStore from "../modules/favorites/FavoriteStore";
import { createFavoritesGuildChannelRecord } from "../modules/favorites/utils/createFavoritesGuildChannelRecord";
import { GUILD_NON_CATEGORY_CHANNEL_TYPES, isGuildSelectableChannelType, isGuildVocalChannelType, createChannelRecord } from "../records/ChannelRecord";
import AuthenticationStore from "./AuthenticationStore";
import ChannelStore from "./ChannelStore";
import GuildMemberStore from "./GuildMemberStore";
import GuildStore from "./GuildStore";
import PermissionStore from "./PermissionStore";
import RelationshipStore from "./RelationshipStore";
import UserStore from "./UserStore";
import * as _Constants from "../Constants";
import { ChannelTypes, ElevatedPermissions, FAVORITES, Permissions } from "../Constants";
import * as _FavoritesUtils from "../modules/favorites/FavoritesUtils";
import Module12 from "../_bundle/Module12";
import { computeChannelName } from "../modules/channel/useChannelName";
import { hasAny } from "../_bundle/HighLow";
import { computePermissions } from "../utils/PermissionUtils";
import Module589 from "../_bundle/Module589";
import Dispatcher from "../Dispatcher";
let c1618_21, c1618_22, c1618_23, c1618_24, c1618_25, c1618_26, channelId, c1618_28, c1618_29, c1618_30, c1618_31, c1618_32, rebuildGuildChannels, hasElevatedPermissions2, handleFavoritesUpdate;
const comparator = function comparator(a0, a1) {
return a0.comparator - a1.comparator;
};
const resetAllGuildChannels = function resetAllGuildChannels() {
c1618_24 = {};
c1618_28 = {};
c1618_25 = {};
c1618_26 = {};
if (c1618_23 != null) {
rebuildGuildChannels(c1618_23);
}
};
rebuildGuildChannels = function rebuildGuildChannels(a0) {
let r37;
const r5 = {
id: a0
};
r5[c1618_21] = [];
r5[c1618_22] = [];
r5[ChannelTypes.GUILD_CATEGORY] = [c1618_29];
r5.count = 0;
c1618_24[a0] = r5;
c1618_28[a0] = [];
let c13350_0 = r5;
const id = r5.id;
const isFavoritesGuildId = _FavoritesUtils.isFavoritesGuildId(id);
const r14 = {};
if (isFavoritesGuildId) {
const favoriteChannels = FavoriteStore.getFavoriteChannels();
r37 = r14;
for (const key2 in favoriteChannels) {
r37 = r14;
const channel = ChannelStore.getChannel(key2);
if (channel != null) {
const favoritesGuildChannelRecord = createFavoritesGuildChannelRecord(favoriteChannels, favoriteChannels[key2], channel);
r14[key2] = {
channel: favoritesGuildChannelRecord,
comparator: favoritesGuildChannelRecord.position
};
}
}
r37 = r14;
} else {
const mutableGuildChannelsForGuild = ChannelStore.getMutableGuildChannelsForGuild(id);
r37 = r14;
for (const key in mutableGuildChannelsForGuild) {
r37 = r14;
r14[key] = {
channel: mutableGuildChannelsForGuild[key],
comparator: mutableGuildChannelsForGuild[key].position
};
}
r37 = r14;
}
let c13351_0, c13351_1;
Module12.forEach(r37, (a0) => {
const channel = a0.channel;
c13350_0.count = c13350_0.count + 1;
if (!(GUILD_NON_CATEGORY_CHANNEL_TYPES.has(channel.type) && (!PermissionStore.can(Permissions.VIEW_CHANNEL, channel) && (!GatedChannelStore.isChannelGated(channel.guild_id, channel.id) && channel.id !== channelId)))) {
let type = channel.type;
if (isGuildSelectableChannelType(type)) {
type = c1618_21;
} else if (isGuildVocalChannelType(type)) {
type = c1618_22;
}
if (channel.type === ChannelTypes.GUILD_DIRECTORY) {
if (c1618_28[id] == null) {
c1618_28[id] = [];
}
c1618_28[id].push(a0);
}
if (c13350_0[type] != null) {
c13350_0[type].push(a0);
}
}
});
r5[c1618_21].sort(comparator);
r5[c1618_22].sort(comparator);
r5[ChannelTypes.GUILD_CATEGORY].sort(comparator);
c13351_0 = void 0;
c13351_1 = void 0;
const id2 = r5.id;
const r81 = {};
c1618_25[id2] = r81;
c13351_0 = r81;
c13351_1 = {};
r5[c1618_21].forEach(({ channel }) => {
let name;
const r15 = computeChannelName(channel, UserStore, RelationshipStore);
const hasOwnProperty = Object.prototype.hasOwnProperty;
const r22 = hasOwnProperty.call(c13351_1, r15) ? c13351_1[r15] : null;
if (r22 == null) {
c13351_1[r15] = 1;
name = r15;
} else {
c13351_1[r15] = r22 + 1;
name = r15 + `~${r22}`;
}
c13351_0[channel.id] = {
id: channel.id,
name
};
});
const r91 = (function calculateGuildHasElevatedPermissions(a0, a1) {
const guild = GuildStore.getGuild(a1);
if (guild != null && hasElevatedPermissions2(a0, guild)) {
return true;
}
let r26 = c1618_24[a1];
if (r26 == null) {
r26 = rebuildGuildChannels(a1);
}
const r30 = r26[c1618_21];
const r32 = r26[c1618_22];
for (const item of r30) {
if (!hasElevatedPermissions2(a0, item.channel)) {
continue;
}
return true;
}
for (const item2 of r32) {
if (!hasElevatedPermissions2(a0, item2.channel)) {
continue;
}
return true;
}
return false;
})(UserStore.getCurrentUser(), a0);
if (r91) {
c1618_26[a0] = true;
} else {
delete c1618_26[a0];
}
return r5;
};
const handleGuildUpdates = function handleGuildUpdates(a0) {
const id2 = a0.guild.id;
if (id2 == null) {
return false;
}
c1618_24[id2] = void 0;
if (c1618_23 === id2) {
rebuildGuildChannels(id2);
}
};
const handleChannelUpdate = function handleChannelUpdate(a0) {
const guild_id = a0.channel.guild_id;
if (guild_id == null) {
return false;
}
c1618_24[guild_id] = void 0;
if (guild_id === c1618_23) {
rebuildGuildChannels(guild_id);
}
};
const handleGuildRoleUpdate = function handleGuildRoleUpdate({ guildId }) {
c1618_24[guildId] = void 0;
if (guildId === c1618_23) {
rebuildGuildChannels(guildId);
}
};
hasElevatedPermissions2 = function hasElevatedPermissions(a0, a1) {
return hasAny(computePermissions({
user: a0,
context: a1,
checkElevated: false
}), ElevatedPermissions);
};
handleFavoritesUpdate = function handleFavoritesUpdate() {
rebuildGuildChannels(FAVORITES);
};
const { NULL_STRING_GUILD_ID, NULL_STRING_CHANNEL_ID } = _Constants;
c1618_21 = "SELECTABLE";
c1618_22 = "VOCAL";
c1618_23 = null;
c1618_24 = {};
c1618_25 = {};
c1618_26 = {};
channelId = null;
c1618_28 = {};
c1618_29 = {
comparator: -1,
channel: createChannelRecord({
id: NULL_STRING_CHANNEL_ID,
type: ChannelTypes.GUILD_CATEGORY,
name: "Uncategorized"
})
};
const r72 = {
id: NULL_STRING_GUILD_ID,
SELECTABLE: [],
VOCAL: []
};
r72[ChannelTypes.GUILD_CATEGORY] = [c1618_29];
r72.count = 0;
c1618_30 = r72;
c1618_31 = [];
c1618_32 = {};
class GuildChannelStore extends Module589.Store {
static displayName = "GuildChannelStore";
initialize() {
this.waitFor(AuthenticationStore, ChannelStore, FavoriteStore, GatedChannelStore, GuildMemberStore, GuildStore, PermissionStore, UserStore);
this.syncWith([FavoriteStore], handleFavoritesUpdate);
}
getAllGuilds() {
return c1618_24;
}
getChannels(a0) {
let r2;
if (a0 != null) {
let r6 = c1618_24[a0];
if (r6 == null) {
r6 = rebuildGuildChannels(a0);
}
r2 = r6;
} else {
r2 = c1618_30;
}
return r2;
}
getFirstChannelOfType(a0, a1, a2) {
const r9 = this.getChannels(a0)[a2].find(a1);
return r9 != null ? r9.channel : null;
}
getFirstChannel(a0, a1, r4 = false) {
let r8 = this.getFirstChannelOfType(a0, a1, c1618_21);
if (r8 == null) {
r8 = r4 ? this.getFirstChannelOfType(a0, a1, c1618_22) : null;
}
return r8;
}
getDefaultChannel(a0, r6 = false, r3 = Permissions.VIEW_CHANNEL) {
const VIEW_CHANNEL = r3;
return this.getFirstChannel(a0, (a0) => PermissionStore.can(VIEW_CHANNEL, a0.channel), r6);
}
getSFWDefaultChannel(a0, r6 = false, r3 = Permissions.VIEW_CHANNEL) {
const VIEW_CHANNEL2 = r3;
return this.getFirstChannel(a0, (a0) => PermissionStore.can(VIEW_CHANNEL2, a0.channel) && !a0.channel.nsfw, r6);
}
getSelectableChannelIds(a0) {
return this.getChannels(a0)[c1618_21].map((item) => item.channel.id);
}
getSelectableChannels(a0) {
return this.getChannels(a0)[c1618_21];
}
getVocalChannelIds(a0) {
return this.getChannels(a0)[c1618_22].map((item) => item.channel.id);
}
getDirectoryChannelIds(a0) {
const r7 = c1618_28[a0];
let r8 = r7 == null ? void 0 : r7.map((item) => item.channel.id);
if (r8 == null) {
r8 = c1618_31;
}
return r8;
}
hasSelectableChannel(a0, a1) {
return this.getSelectableChannelIds(a0).includes(a1);
}
hasElevatedPermissions(a0) {
return c1618_26[a0] || false;
}
hasChannels(a0) {
return this.getChannels(a0).count > 0;
}
hasCategories(a0) {
return this.getChannels(a0)[ChannelTypes.GUILD_CATEGORY].length > 1;
}
getTextChannelNameDisambiguations(a0) {
let r1;
if (a0 != null) {
let r5 = c1618_25[a0];
if (r5 == null) {
r5 = c1618_32;
}
r1 = r5;
} else {
r1 = c1618_32;
}
return r1;
}
}
const guildChannelStore = new GuildChannelStore(Dispatcher, {
BACKGROUND_SYNC: resetAllGuildChannels,
CHANNEL_SELECT: function handleChannelSelect({ guildId }) {
let r6 = guildId;
if (r6 == null) {
r6 = null;
}
c1618_23 = r6;
let r2 = guildId != null;
if (r2) {
let r10 = c1618_24[guildId] == null;
if (r10) {
rebuildGuildChannels(guildId);
r10 = void 0;
}
r2 = r10;
}
return r2;
},
CONNECTION_OPEN: resetAllGuildChannels,
OVERLAY_INITIALIZE: resetAllGuildChannels,
CACHE_LOADED_LAZY: resetAllGuildChannels,
GUILD_CREATE: handleGuildUpdates,
GUILD_UPDATE: handleGuildUpdates,
GUILD_DELETE: function handleGuildDelete(a0) {
const id2 = a0.guild.id;
delete c1618_24[id2];
delete c1618_25[id2];
delete c1618_26[id2];
delete c1618_28[id2];
return true;
},
GUILD_MEMBER_UPDATE: function handleGuildMemberUpdate({ guildId, user }) {
if (AuthenticationStore.getId() !== user.id) {
return false;
}
c1618_24[guildId] = void 0;
if (guildId === c1618_23) {
rebuildGuildChannels(guildId);
}
},
CHANNEL_CREATE: handleChannelUpdate,
CHANNEL_DELETE: handleChannelUpdate,
CHANNEL_UPDATES: function handleChannelUpdates({ channels }) {
let r0 = false;
for (const item of channels) {
const guild_id = item.guild_id;
if (guild_id != null) {
c1618_24[guild_id] = void 0;
r0 = true;
if (c1618_23 === guild_id) {
rebuildGuildChannels(guild_id);
}
}
}
return r0;
},
GUILD_ROLE_CREATE: handleGuildRoleUpdate,
GUILD_ROLE_UPDATE: handleGuildRoleUpdate,
GUILD_ROLE_DELETE: handleGuildRoleUpdate,
IMPERSONATE_UPDATE: handleGuildRoleUpdate,
IMPERSONATE_STOP: handleGuildRoleUpdate,
VOICE_CHANNEL_SELECT: function handleVoiceChannelSelect({ channelId: channelId2 }) {
let r14;
if (channelId2 == null && channelId != null) {
const channel2 = ChannelStore.getChannel(channelId);
channelId = null;
let r24 = channel2 == null ? void 0 : channel2.getGuildId();
if (r24 == null) {
r24 = null;
}
let r26 = r24 != null;
if (r26) {
c1618_24[r24] = void 0;
r26 = true;
if (r24 === c1618_23) {
rebuildGuildChannels(r24);
r26 = true;
}
}
r14 = r26;
} else {
const channel = ChannelStore.getChannel(channelId2);
channelId = channelId2;
let r12 = channel == null ? void 0 : channel.getGuildId();
if (r12 == null) {
r12 = null;
}
r14 = r12 != null;
if (r14) {
c1618_24[r12] = void 0;
r14 = true;
if (r12 === c1618_23) {
rebuildGuildChannels(r12);
r14 = true;
}
}
}
return r14;
},
VOICE_CHANNEL_STATUS_UPDATE: function handleVoiceChannelStatusUpdate(a0) {
const basicChannel = ChannelStore.getBasicChannel(a0.id);
if (basicChannel != null && basicChannel.guild_id != null) {
rebuildGuildChannels(basicChannel.guild_id);
}
},
VOICE_STATE_UPDATES: function handleVoiceStateUpdates(a0) {
return a0.voiceStates.reduce((acc, { channelId: channelId2, sessionId }) => {
const sessionId2 = AuthenticationStore.getSessionId();
let r12 = acc;
if (sessionId2 === sessionId) {
const channel = ChannelStore.getChannel(channelId2);
channelId = channelId2;
let r16 = channel == null ? void 0 : channel.getGuildId();
if (r16 == null) {
r16 = null;
}
let r18 = r16 != null;
if (r18) {
c1618_24[r16] = void 0;
r18 = true;
if (r16 === c1618_23) {
rebuildGuildChannels(r16);
r18 = true;
}
}
if (!r18) {
r18 = acc;
}
r12 = r18;
}
return r12;
}, false);
}
});
export default guildChannelStore;
export const GUILD_SELECTABLE_CHANNELS_KEY = "SELECTABLE";
export const GUILD_VOCAL_CHANNELS_KEY = "VOCAL";