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

437 lines
14 KiB
TypeScript

// Metro module 16698 (f16699).
import ChannelStore from "../../stores/ChannelStore";
import GuildStore from "../../stores/GuildStore";
import PermissionStore from "../../stores/PermissionStore";
import RelationshipStore from "../../stores/RelationshipStore";
import SelectedChannelStore from "../../stores/SelectedChannelStore";
import UserStore from "../../stores/UserStore";
import { ChannelTypes, Links, ME, Permissions, Routes } from "../../Constants";
import NativeIntentsExperiment from "./NativeIntentsExperiment";
import IntentsBindings_android from "./IntentsBindings.android";
import { computeChannelName } from "../channel/useChannelName";
import { getChannelIconURL } from "../channel/getChannelIcon";
import AvatarUtils from "../../utils/AvatarUtils";
import { isNotNullish } from "../../utils/GlobalUtils";
import UserUtils from "../../utils/UserUtils";
import AutomaticLifecycleManager from "../../lib/AutomaticLifecycleManager";
import { HermesBuiltin } from "../../_hermes/prelude";
let c12031_15, makeSearchItem, getGuildThumbnail, makeGuildDomain, setChannelActivity, indexChannelUpdates;
const indexingEnabled = function indexingEnabled() {
return NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled;
};
makeSearchItem = function makeSearchItem(a0, guild, isUpdate = false) {
let thumbnailURL;
const r14 = computeChannelName(a0, UserStore, RelationshipStore, true);
const r15 = computeChannelName(a0, UserStore, RelationshipStore, false);
const alternateNames = [r14, r15];
if (a0.isGuildVocal()) {
alternateNames.push(`!${r15}`);
}
const r17 = [];
const keywords = [];
const channel = ChannelStore.getChannel(a0.parent_id);
if (channel != null) {
const r54 = computeChannelName(channel, UserStore, RelationshipStore, true);
const r63 = computeChannelName(channel, UserStore, RelationshipStore, false);
keywords.push(r54);
keywords.push(r63);
r17.push(r54);
}
if (guild != null) {
keywords.push(guild.name);
r17.push(guild.name);
}
const title = r14 + (r17.length > 0 ? ` (${r17.join(", ")})` : "");
let r91 = guild?.id;
if (r91 == null) {
r91 = ME;
}
const id = Routes.CHANNEL(r91, a0.id);
const channelIconURL = getChannelIconURL(a0, 128, false);
const r100 = channelIconURL == null;
let r101 = void 0;
if (!r100) {
const startsWith = channelIconURL.startsWith;
r101 = startsWith == null ? void 0 : startsWith.call(channelIconURL, "/");
}
if (r101) {
thumbnailURL = location.origin + channelIconURL;
} else {
thumbnailURL = channelIconURL;
if (thumbnailURL == null) {
thumbnailURL = void 0;
}
}
return {
id,
relatedUniqueIdentifier: id,
type: "url",
title,
displayName: title,
thumbnailURL,
rankingHint: a0.type === ChannelTypes.DM ? c12031_15.DM : c12031_15.OTHER_CHANNEL,
keywords,
alternateNames,
isUpdate
};
};
getGuildThumbnail = function getGuildThumbnail(guild) {
let r20;
const r2 = guild != null;
let r3 = void 0;
if (r2) {
const guildIconURL = AvatarUtils.getGuildIconURL({
id: guild.id,
icon: guild.icon,
size: 128
});
const r15 = guildIconURL == null;
let r16 = void 0;
if (!r15) {
const startsWith = guildIconURL.startsWith;
r16 = startsWith == null ? void 0 : startsWith.call(guildIconURL, "/");
}
if (r16) {
r20 = location.origin + guildIconURL;
} else {
r20 = guildIconURL;
if (r20 == null) {
r20 = void 0;
}
}
r3 = r20;
}
return r3;
};
makeGuildDomain = function makeGuildDomain(guild2, isUpdate = false) {
const guildThumbnail = getGuildThumbnail(guild2);
const id = Routes.CHANNEL(guild2.id);
const items = [{
id,
relatedUniqueIdentifier: id,
type: "url",
title: guild2.name,
displayName: guild2.name,
alternateNames: [`*${guild2.name}`],
rankingHint: c12031_15.GUILD
}];
const mutableGuildChannelsForGuild = ChannelStore.getMutableGuildChannelsForGuild(guild2.id);
for (const key in mutableGuildChannelsForGuild) {
const r16 = mutableGuildChannelsForGuild[key];
if (PermissionStore.can(Permissions.VIEW_CHANNEL, r16)) {
items.push(makeSearchItem(r16, guild2, isUpdate));
}
}
for (const item of ChannelStore.getAllThreadsForGuild(guild2.id)) {
if (PermissionStore.can(Permissions.VIEW_CHANNEL, item)) {
items.push(makeSearchItem(item, guild2, isUpdate));
}
}
return {
id: guild2.id,
items,
defaultThumbnailURL: guildThumbnail,
isUpdate
};
};
setChannelActivity = function setChannelActivity(channelId) {
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasUserActivity()
}).activityEnabled) {
const r34 = channelId != null ? ChannelStore.getChannel(channelId) : void 0;
if (r34 != null) {
const guild = GuildStore.getGuild(r34.guild_id);
const r13 = computeChannelName(r34, UserStore, RelationshipStore, true);
const title = r13 + (guild != null ? ` (${guild.name})` : "");
const r57 = Set;
const r58 = [r13];
const r59 = [];
if (r34.name !== "") {
r59.push(r34.name);
}
if (r34.nicks != null) {
r59.push.apply(r59, [...Object.values(r34.nicks)]);
}
if (r34.type === ChannelTypes.DM) {
const [r89] = r34.recipients.map(UserStore.getUser).filter(isNotNullish);
if (r89 != null) {
const globalName = UserUtils.getGlobalName(r89);
if (globalName != null) {
r59.push(globalName);
}
r59.push(r89.username);
r59.push("@" + r89.username);
const nickname = RelationshipStore.getNickname(r89.id);
if (nickname != null) {
r59.push(nickname);
}
const name = UserUtils.getName(r89);
if (name != null) {
r59.push(name);
}
}
}
HermesBuiltin.arraySpread(r58, r59, 1);
const keywords = [...new r57(r58)];
const relatedUniqueIdentifier = Routes.CHANNEL(r34.guild_id, r34.id);
IntentsBindings_android.setActivity({
webpageURL: `${Links.BASE_URL}${relatedUniqueIdentifier}`,
relatedUniqueIdentifier,
eligibleForHandoff: true,
eligibleForSearch: true,
title,
keywords,
displayName: title,
type: "com.discord.view-channel"
});
} else {
IntentsBindings_android.resignActivity();
}
}
};
indexChannelUpdates = function indexChannelUpdates(channels) {
let r32;
if (indexingEnabled()) {
const r6 = [];
const r3 = [];
const r9 = {};
let r5 = channels[Symbol.iterator]();
const next = r5.next;
while (true) {
const r18 = r5 === void 0 ? void 0 : (r32 = next.call(r5)).done ? r5 = void 0 : r32.value;
if (r5 === void 0) {
if (r6.length > 0) {
IntentsBindings_android.indexDomains(r6);
}
if (!(r3.length > 0)) {
break;
}
IntentsBindings_android.deleteSearchItems(r3);
break;
}
try {
if (PermissionStore.can(Permissions.VIEW_CHANNEL, r18)) {
const guild = GuildStore.getGuild(r18.guild_id);
let r53 = guild?.id;
if (r53 == null) {
r53 = ME;
}
const r54 = r53;
const r58 = makeSearchItem(r18, guild, true);
const r59 = r9[r53];
if (r59 != null) {
r59.push(r58);
} else {
const guildThumbnail = getGuildThumbnail(guild);
const items = [r58];
const id = r54;
r6.push({
id,
items,
defaultThumbnailURL: guildThumbnail
});
r9[id] = items;
}
} else {
r3.push(r18.id);
}
} catch (r73) {
if (r5 !== void 0 && typeof r5.return === "function") {
try {
r5.return();
} catch {}
}
throw r73;
}
}
}
};
c12031_15 = {
GUILD: 100,
100: "GUILD",
DM: 75,
75: "DM",
OTHER_CHANNEL: 50,
50: "OTHER_CHANNEL"
};
class NativeIntentsManager extends AutomaticLifecycleManager {
actions = {
POST_CONNECTION_OPEN: this.handleInit,
LOGOUT: this.handleLogout,
CHANNEL_SELECT: this.handleChannelSelect,
CHANNEL_CREATE: this.handleChannelCreate,
CHANNEL_DELETE: this.handleChannelDelete,
CHANNEL_UPDATES: this.handleChannelUpdates,
GUILD_CREATE: this.handleGuildCreateOrUpdate,
GUILD_UPDATE: this.handleGuildCreateOrUpdate,
GUILD_DELETE: this.handleGuildDelete,
RELATIONSHIP_ADD: this.handleRelationshipChange,
RELATIONSHIP_REMOVE: this.handleRelationshipChange,
RELATIONSHIP_UPDATE: this.handleRelationshipChange,
THREAD_CREATE: this.handleChannelCreate,
THREAD_DELETE: this.handleChannelDelete,
THREAD_UPDATE: this.handleThreadUpdate,
USER_UPDATE: this.handleUserUpdate
};
handleInit() {
setChannelActivity(SelectedChannelStore.getCurrentlySelectedChannelId());
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).clearEnabled) {
IntentsBindings_android.clearSearchIndex();
}
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled && NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled) {
const r59 = GuildStore.getGuildsArray().map((item) => makeGuildDomain(item));
const items = [];
const mutablePrivateChannels = ChannelStore.getMutablePrivateChannels();
for (const key in mutablePrivateChannels) {
items.push(makeSearchItem(mutablePrivateChannels[key]));
}
r59.push({
id: ME,
items
});
IntentsBindings_android.indexDomains(r59);
}
}
handleLogout() {
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).clearEnabled) {
IntentsBindings_android.clearSearchIndex();
}
}
handleChannelSelect(a0) {
setChannelActivity(a0.channelId);
}
handleChannelCreate(a0) {
let r44;
L19: {
const channel = a0.channel;
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled && PermissionStore.can(Permissions.VIEW_CHANNEL, channel)) {
const guild = GuildStore.getGuild(channel.guild_id);
if (guild == null && channel.guild_id != null) {
break L19;
}
const r3 = guild != null;
let defaultThumbnailURL = void 0;
if (r3) {
const guildIconURL = AvatarUtils.getGuildIconURL({
id: guild.id,
icon: guild.icon,
size: 128
});
const r39 = guildIconURL == null;
let r40 = void 0;
if (!r39) {
const startsWith = guildIconURL.startsWith;
r40 = startsWith == null ? void 0 : startsWith.call(guildIconURL, "/");
}
if (r40) {
r44 = location.origin + guildIconURL;
} else {
r44 = guildIconURL;
if (r44 == null) {
r44 = void 0;
}
}
defaultThumbnailURL = r44;
}
let id = guild?.id;
if (id == null) {
id = ME;
}
IntentsBindings_android.indexDomains([{
id,
items: [makeSearchItem(channel, guild)],
defaultThumbnailURL
}]);
}
}
}
handleChannelDelete({ channel }) {
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled) {
IntentsBindings_android.deleteSearchItems([channel.id]);
}
}
handleChannelUpdates(a0) {
indexChannelUpdates(a0.channels);
}
handleGuildCreateOrUpdate({ guild, type }) {
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled) {
const guild2 = GuildStore.getGuild(guild.id);
if (guild2 != null) {
IntentsBindings_android.indexDomains([makeGuildDomain(guild2, type === "GUILD_UPDATE")]);
} else {
IntentsBindings_android.deleteSearchDomains([guild.id]);
}
}
}
handleGuildDelete({ guild }) {
if (NativeIntentsExperiment.getCurrentConfig({
location: "NativeIntentsManager"
}, {
autoTrackExposure: true,
disable: !IntentsBindings_android.hasSearch()
}).searchEnabled) {
IntentsBindings_android.deleteSearchDomains([guild.id]);
}
}
handleThreadUpdate(a0) {
indexChannelUpdates([a0.channel]);
}
handleUserUpdate(a0) {
const dMChannelFromUserId = ChannelStore.getDMChannelFromUserId(a0.user.id);
if (dMChannelFromUserId != null) {
indexChannelUpdates([dMChannelFromUserId]);
}
}
handleRelationshipChange(a0) {
const dMChannelFromUserId = ChannelStore.getDMChannelFromUserId(a0.relationship.id);
if (dMChannelFromUserId != null) {
indexChannelUpdates([dMChannelFromUserId]);
}
}
}
const nativeIntentsManager = new NativeIntentsManager();
export default nativeIntentsManager;