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

448 lines
17 KiB
TypeScript

// Metro module 6164 (f6165).
import ThreadMemberListStore from "../threads/ThreadMemberListStore";
import LinkRecord from "../../records/LinkRecord";
import { GUILD_VOCAL_CHANNELS_KEY } from "../../stores/GuildChannelStore";
import RelationshipStore from "../../stores/RelationshipStore";
import UserStore from "../../stores/UserStore";
import { AutocompleterResultTypes } from "./AutocompleterConstants";
import AutocompleteUtils, { getBoosterMap, calculateScore, getGameProfileMatchTier } from "../../utils/AutocompleteUtils";
import UserSearchManager from "./UserSearchManager";
import { FrecencyUserSettingsActionCreators } from "../user_settings/UserSettingsProtoActionCreators";
import GuildUtils from "../../utils/GuildUtils";
import { queryGamesAutocomplete } from "../games/autocomplete/queryGamesAutocomplete";
import * as _StringUtils from "../../utils/StringUtils";
import { findCodedLink } from "../coded_links/findCodedLinks";
import { CodedLinkType } from "../coded_links/CodedLink";
import Module3888 from "../../_bundle/Module3888";
import URLUtils from "../../utils/URLUtils";
import Module12 from "../../_bundle/Module12";
import sortByMatchScore from "./sortByMatchScore";
let options;
const getAutocompleterBoosterMap = function getAutocompleterBoosterMap(a0, options) {
return options.frecencyBoosters ? getBoosterMap(a0) : {};
};
options = Object.freeze({});
class Autocompleter {
constructor(a0, a1, _limit = 100, r7 = options, _refetchForSingleCategoryLimit = 0) {
this.query = "";
this.options = options;
this.results = [];
this._userResults = [];
this._groupDMResults = [];
this._textChannelResults = [];
this._voiceChannelResults = [];
this._guildResults = [];
this._applicationResults = [];
this._gameProfileResults = [];
this._linkResults = [];
this._inAppNavigations = [];
this._userBlacklist = null;
this._refetched = false;
this.parseUserResults = ({ results }) => {
if (this._include(AutocompleterResultTypes.USER)) {
this._userResults = [];
for (const item of results) {
const { id, score, comparator } = item;
const user = UserStore.getUser(id);
const record = user;
if (user != null) {
const _userResults = this._userResults;
const r49 = {
type: AutocompleterResultTypes.USER,
record,
score: calculateScore(score),
comparator: null
};
let comparator2 = comparator;
if (comparator2 == null) {
comparator2 = void 0;
}
r49.comparator = comparator2;
_userResults.push(r49);
}
}
const r62 = this._willRefetchIfSingleCategoryResults();
let r63 = !r62;
if (!r62) {
r63 = this._userResults.length > this._limit;
}
if (r63) {
this._userResults.length = this._limit;
}
if (r62) {
this.refetchIfSingleCategoryResults();
}
this.updateAllResults();
}
};
this.updateAllResults = () => {
const r3 = clearTimeout;
r3(this._asyncTimeout);
this.results = Module12([...this._userResults, ...this._groupDMResults, ...this._textChannelResults, ...this._voiceChannelResults, ...this._guildResults, ...this._gameProfileResults, ...this._linkResults, ...this._inAppNavigations]).uniqBy((item) => `${item.type}-${item.record.id}`).sort(sortByMatchScore).value();
this.onResultsChange(this.results, this.query);
};
this.onResultsChange = a0;
this.setOptions(r7, true);
this._limit = _limit;
this._refetchForSingleCategoryLimit = _refetchForSingleCategoryLimit;
this.createSearchContext();
this.setResultTypes(a1);
}
createSearchContext() {
if (this.userSearchContext == null) {
this.userSearchContext = UserSearchManager.getUserSearchContext(this.parseUserResults, this._limit);
}
}
setLimit(a0) {
const userSearchContext = this.userSearchContext;
this._limit = a0;
if (userSearchContext != null) {
userSearchContext.setLimit(a0);
}
if (this._userResults.length > this._limit) {
this._userResults.length = this._limit;
}
if (this._groupDMResults.length > this._limit) {
this._groupDMResults.length = this._limit;
}
if (this._textChannelResults.length > this._limit) {
this._textChannelResults.length = this._limit;
}
if (this._voiceChannelResults.length > this._limit) {
this._voiceChannelResults.length = this._limit;
}
if (this._guildResults.length > this._limit) {
this._guildResults.length = this._limit;
}
if (this._applicationResults.length > this._limit) {
this._applicationResults.length = this._limit;
}
if (this._gameProfileResults.length > this._limit) {
this._gameProfileResults.length = this._limit;
}
if (this._linkResults.length > this._limit) {
this._linkResults.length = this._limit;
}
if (this._inAppNavigations.length > this._limit) {
this._inAppNavigations.length = this._limit;
}
}
setRefetchForSingleCategoryLimit(a0) {
this._refetchForSingleCategoryLimit = a0;
}
setResultTypes(a0) {
this.resultTypes = a0 != null ? new Set(a0) : null;
this._userResults = this._include(AutocompleterResultTypes.USER) ? this._userResults : [];
this._groupDMResults = this._include(AutocompleterResultTypes.GROUP_DM) ? this._groupDMResults : [];
this._textChannelResults = this._include(AutocompleterResultTypes.TEXT_CHANNEL) ? this._textChannelResults : [];
this._voiceChannelResults = this._include(AutocompleterResultTypes.VOICE_CHANNEL) ? this._voiceChannelResults : [];
this._guildResults = this._include(AutocompleterResultTypes.GUILD) ? this._guildResults : [];
this._applicationResults = this._include(AutocompleterResultTypes.APPLICATION) ? this._applicationResults : [];
this._gameProfileResults = this._include(AutocompleterResultTypes.GAME_PROFILE) ? this._gameProfileResults : [];
this._linkResults = this._include(AutocompleterResultTypes.LINK) ? this._linkResults : [];
this._inAppNavigations = this._include(AutocompleterResultTypes.IN_APP_NAVIGATION) ? this._inAppNavigations : [];
}
_include(a0) {
return this.resultTypes == null || this.resultTypes.has(a0);
}
_isAsyncSearch() {
let r8 = this._include(AutocompleterResultTypes.USER);
if (r8) {
const options2 = this.options;
r8 = (options2 == null ? void 0 : options2.userFilters?.thread) != null;
}
return r8;
}
setOptions(a0, r4 = false) {
this.options = r4 ? {
...this.options,
...a0
} : a0;
if (this.options.blacklist != null) {
this._userBlacklist = Array.from(this.options.blacklist).map((item) => item.startsWith("user:") ? item.replace("user:", "") : "").filter((item) => item !== "");
} else {
this._userBlacklist = null;
}
}
_willRefetchIfSingleCategoryResults() {
const r5 = this._refetched || this._refetchForSingleCategoryLimit <= 5;
return r5 ? !r5 : this.options.voiceChannelGuildFilter == null && this.options.userFilters == null && [this._userResults, this._groupDMResults, this._textChannelResults, this._voiceChannelResults, this._guildResults, this._applicationResults, this._gameProfileResults, this._linkResults, this._inAppNavigations].filter((item) => item.length > 0).length === 1;
}
refetchIfSingleCategoryResults() {
if (this._willRefetchIfSingleCategoryResults()) {
this._refetched = true;
const query = this.query;
if (this._userResults.length > 0) {
this.queryUsers(query, null, this._refetchForSingleCategoryLimit);
} else if (this._groupDMResults.length > 0) {
this._groupDMResults = this.queryGroupDMs(query, this._refetchForSingleCategoryLimit);
} else if (this._textChannelResults.length > 0) {
this._textChannelResults = this.queryTextChannels(query, this._refetchForSingleCategoryLimit);
} else if (this._voiceChannelResults.length > 0) {
this._voiceChannelResults = this.queryVoiceChannels(query, this._refetchForSingleCategoryLimit);
} else if (this._guildResults.length > 0) {
this._guildResults = this.queryGuilds(query, this._refetchForSingleCategoryLimit);
} else if (this._applicationResults.length > 0) {
this._applicationResults = this.queryApplications(query, this._refetchForSingleCategoryLimit);
} else if (this._gameProfileResults.length > 0) {
this._gameProfileResults = this.queryGameProfiles(query, this._refetchForSingleCategoryLimit);
} else if (this._linkResults.length > 0) {
this._linkResults = this.queryLink(query, this._refetchForSingleCategoryLimit);
} else if (this._inAppNavigations.length > 0) {
this._inAppNavigations = this.queryInAppNavigations(query, this._refetchForSingleCategoryLimit);
}
}
}
search(a0, c15541_0) {
this.query = a0;
this._refetched = false;
if (a0.trim() === "") {
this.clear();
this.updateAllResults();
return void 0;
}
(this.options.frecencyBoosters ? FrecencyUserSettingsActionCreators.loadIfNecessary() : Promise.resolve()).finally(() => {
this.queryUsers(a0, c15541_0, this._limit);
this._groupDMResults = this.queryGroupDMs(a0, this._limit);
this._textChannelResults = this.queryTextChannels(a0, this._limit);
this._voiceChannelResults = this.queryVoiceChannels(a0, this._limit);
this._guildResults = this.queryGuilds(a0, this._limit);
this._applicationResults = this.queryApplications(a0, this._limit);
this._gameProfileResults = this.queryGameProfiles(a0, this._limit);
this._inAppNavigations = this.queryInAppNavigations(a0, this._limit);
if (this._isAsyncSearch()) {
clearTimeout(this._asyncTimeout);
this._asyncTimeout = setTimeout(this.updateAllResults, 300);
} else if (!this._include(AutocompleterResultTypes.USER)) {
this.updateAllResults();
}
});
}
clear() {
const userSearchContext = this.userSearchContext;
if (userSearchContext != null) {
userSearchContext.clearQuery();
}
this.results = [];
this._userResults = [];
this._groupDMResults = [];
this._textChannelResults = [];
this._voiceChannelResults = [];
this._guildResults = [];
this._applicationResults = [];
this._gameProfileResults = [];
this._linkResults = [];
this._inAppNavigations = [];
}
clean() {
this.clear();
this.destroy();
this.query = "";
this.updateAllResults();
}
pause() {
const userSearchContext = this.userSearchContext;
if (userSearchContext != null) {
const unsubscribe = userSearchContext.unsubscribe;
if (unsubscribe != null) {
unsubscribe.call(userSearchContext);
}
}
}
resume() {
const userSearchContext = this.userSearchContext;
if (userSearchContext != null) {
const subscribe = userSearchContext.subscribe;
if (subscribe != null) {
subscribe.call(userSearchContext);
}
}
}
destroy() {
const userSearchContext = this.userSearchContext;
if (userSearchContext != null) {
userSearchContext.destroy();
this.userSearchContext = null;
}
}
queryTextChannels(a0, a1) {
if (this._include(AutocompleterResultTypes.TEXT_CHANNEL)) {
const TEXT_CHANNEL = AutocompleterResultTypes.TEXT_CHANNEL;
const boosters = this.options.frecencyBoosters ? getBoosterMap(TEXT_CHANNEL) : {};
const options2 = this.options;
const { allowSnowflake, blacklist } = options2;
return AutocompleteUtils.queryChannels({
query: a0,
guildId: null,
limit: a1,
fuzzy: true,
allowSnowflake,
filter: blacklist != null ? (a0) => !blacklist.has(`channel:${a0.id}`) : void 0,
boosters
});
}
return [];
}
queryVoiceChannels(a0, a1) {
if (this._include(AutocompleterResultTypes.VOICE_CHANNEL)) {
const options2 = this.options;
const { allowSnowflake, voiceChannelGuildFilter } = options2;
const VOICE_CHANNEL = AutocompleterResultTypes.VOICE_CHANNEL;
return AutocompleteUtils.queryChannels({
query: a0,
guildId: voiceChannelGuildFilter,
limit: a1,
fuzzy: true,
type: GUILD_VOCAL_CHANNELS_KEY,
allowSnowflake,
boosters: this.options.frecencyBoosters ? getBoosterMap(VOICE_CHANNEL) : {}
});
}
return [];
}
queryGuilds(a0, a1) {
if (this._include(AutocompleterResultTypes.GUILD)) {
const GUILD = AutocompleterResultTypes.GUILD;
const boosters = this.options.frecencyBoosters ? getBoosterMap(GUILD) : {};
const options2 = this.options;
const { allowSnowflake, blacklist: blacklist2 } = options2;
return AutocompleteUtils.queryGuilds({
query: a0,
limit: a1,
fuzzy: true,
filter: blacklist2 != null ? (a0) => !blacklist2.has(`guild:${a0.id}`) : void 0,
boosters,
allowSnowflake
});
}
return [];
}
queryUsers(a0, a1, a2) {
const userSearchContext = this.userSearchContext;
if (userSearchContext != null && this._include(AutocompleterResultTypes.USER)) {
const options2 = this.options;
const { allowSnowflake, userFilters } = options2;
const autocompleterBoosterMap = getAutocompleterBoosterMap(AutocompleterResultTypes.USER, this.options);
if (userFilters?.thread == null) {
if (a1 !== void 0) {
GuildUtils.requestMembers(a1, a0, 100);
}
userSearchContext.setLimit(a2);
userSearchContext.setQuery({
query: a0,
filters: userFilters,
blacklist: this._userBlacklist,
boosters: autocompleterBoosterMap
});
} else {
const memberListSections = ThreadMemberListStore.getMemberListSections(userFilters.thread);
const users = [];
for (const key in memberListSections) {
const r51 = memberListSections[key];
for (const userId of r51.userIds) {
let r59 = userFilters?.friends && !RelationshipStore.isFriend(userId);
if (!r59) {
const _userBlacklist = this._userBlacklist;
r59 = _userBlacklist == null ? void 0 : _userBlacklist.includes(userId);
}
if (!r59) {
const userId2 = userId;
users.push({
userId: userId2,
nick: r51.usersById[userId2]?.displayName
});
}
}
}
this._userResults = AutocompleteUtils.queryUsers({
query: a0,
users,
limit: a2,
boosters: autocompleterBoosterMap,
allowSnowflake
});
}
}
}
queryGroupDMs(a0, a1) {
if (this._include(AutocompleterResultTypes.GROUP_DM)) {
const blacklist3 = this.options.blacklist;
const GROUP_DM = AutocompleterResultTypes.GROUP_DM;
return AutocompleteUtils.queryGroupDMs({
query: a0,
limit: a1,
fuzzy: true,
filter: blacklist3 != null ? (a0) => !blacklist3.has(`channel:${a0.id}`) : void 0,
boosters: this.options.frecencyBoosters ? getBoosterMap(GROUP_DM) : {}
});
}
return [];
}
queryApplications(a0, a1) {
return this._include(AutocompleterResultTypes.APPLICATION) ? AutocompleteUtils.queryApplications({
query: a0,
limit: a1,
fuzzy: true
}) : [];
}
queryGameProfiles(a0, a1) {
if (this._include(AutocompleterResultTypes.GAME_PROFILE)) {
let r15 = queryGamesAutocomplete(a0);
if (r15 == null) {
r15 = [];
}
let c15545_0 = a0.toLocaleLowerCase();
return r15.slice(0, a1).map((item, index) => ({
type: AutocompleterResultTypes.GAME_PROFILE,
record: item,
score: calculateScore(getGameProfileMatchTier(item.name, c15545_0, index)),
comparator: item.name,
sortable: item.name.toLocaleLowerCase()
}));
}
return [];
}
refreshGameProfiles() {
const isNullOrEmpty = _StringUtils.isNullOrEmpty(this.query.trim());
if (isNullOrEmpty ? !isNullOrEmpty : this._include(AutocompleterResultTypes.GAME_PROFILE)) {
this._gameProfileResults = this.queryGameProfiles(this.query, this._limit);
this.updateAllResults();
}
}
queryLink(a0) {
let uRL;
if (this._include(AutocompleterResultTypes.LINK)) {
const r22 = findCodedLink(a0);
if (r22?.type === CodedLinkType.INVITE) {
return [{
type: AutocompleterResultTypes.LINK,
record: LinkRecord.fromInviteCode(r22.code),
score: calculateScore(11)
}];
}
const r12 = Module3888.sanitizeUrl(a0);
try {
uRL = new URL(r12);
} catch ($e) {
return [];
}
const { pathname, hostname = "", host } = uRL;
const r42 = URLUtils.isDiscordHostname(hostname) || window.location.host === host;
return pathname !== null && (r42 && URLUtils.isAppRoute(pathname)) ? [{
type: AutocompleterResultTypes.LINK,
record: LinkRecord.fromPath(pathname),
score: calculateScore(11)
}] : [];
}
return [];
}
queryInAppNavigations(a0, a1) {
return this._include(AutocompleterResultTypes.IN_APP_NAVIGATION) ? AutocompleteUtils.queryInAppNavigations({
query: a0,
limit: a1,
fuzzy: true
}) : [];
}
}
export default Autocompleter;