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

188 lines
5.8 KiB
TypeScript

// Metro module 10113 (f10114).
import ChannelStore from "../../stores/ChannelStore";
import { Endpoints, SearchTypes } from "../../Constants";
import Durations from "../../utils/Durations";
import Logger from "../debug/Logger";
import { HTTP } from "../../_bundle/sendRequest";
import Module1464 from "../../_bundle/Module1464";
export class SearchFetcher {
constructor(a0, a1, a2) {
const r3 = {
__proto__: new.target.prototype,
isCanceled: false
};
r3.searchId = a0;
r3.searchType = a1;
r3.query = a2;
return r3;
}
async fetch(c17707_0, c17707_1, c17707_2) {
const _this = this;
let attempts, c26625_3;
let c26625_4 = 0;
let c30195_0, c30195_1, c30195_2;
L31: {
L28: {
L30: {
L29: {
L26: {
try {
c30195_0 = void 0;
c30195_1 = void 0;
if (_this.isCanceled) {
break L31;
}
c26625_4 = 1;
c30195_0 = await _this.makeRequest({
rejectWithError: false
});
if (c30195_0 == null) {
c26625_4 = 0;
break L30;
}
if (_this.isCanceled) {
c26625_4 = 0;
break L29;
}
if (c30195_0.status === 200) {
(0, c17707_0)(c30195_0);
} else if (c30195_0.status === 202) {
const query = _this.query;
const attempts2 = _this.query.attempts;
attempts = attempts2;
if (attempts2 == null) {
attempts = 0;
}
query.attempts = attempts + 1;
if (_this.query.attempts > 5) {
c26625_4 = 0;
break L26;
}
c30195_1 = parseInt(c30195_0.headers["retry-after"]);
_this.retryDelay = isNaN(c30195_1) ? 5000 : c30195_1 !== 0 ? c30195_1 * Durations.Millis.SECOND : 5000;
_this.retryLater(c17707_0, c17707_1, c17707_2);
c17707_1(c30195_0);
}
c26625_4 = 0;
break L28;
} catch ($e) {
const r35 = $e;
let r8 = r35;
c26625_3 = r35;
if (c26625_4 !== 0) {
while (true) {
try {
const r134 = c26625_3;
c26625_4 = 0;
c30195_2 = r134;
new Logger("SearchFetcher").error(c30195_2);
(0, c17707_2)(c30195_2);
break L28;
} catch (r130) {
r8 = r130;
c26625_3 = r130;
if (c26625_4 === 0) {
break;
}
}
}
}
throw r8;
}
}
return void 0;
}
return void 0;
}
return void 0;
}
return void 0;
}
}
cancel() {
this.isCanceled = true;
if (this.indexingPollId != null) {
clearTimeout(this.indexingPollId);
}
}
retryLater(a0, a1, a2) {
if (this.indexingPollId != null) {
clearTimeout(this.indexingPollId);
}
this.indexingPollId = setTimeout(this.fetch.bind(this, a0, a1, a2), this.retryDelay);
}
}
class SearchFetcherImpl extends SearchFetcher {
getEndpoint() {
const searchType = this.searchType;
if (SearchTypes.GUILD === searchType) {
if (this.searchId != null && this.searchId !== "") {
return Endpoints.SEARCH_GUILD(this.searchId);
}
return void 0;
}
if (SearchTypes.GUILD_CHANNEL === searchType) {
if (this.searchId != null && this.searchId !== "") {
const channel = ChannelStore.getChannel(this.searchId);
const r37 = channel == null ? void 0 : channel.getGuildId();
if (r37 == null) {
return void 0;
}
return Endpoints.SEARCH_GUILD(r37);
}
return void 0;
}
if (SearchTypes.CHANNEL === searchType) {
if (this.searchId != null && this.searchId !== "") {
return Endpoints.SEARCH_CHANNEL(this.searchId);
}
return void 0;
}
throw new Error(`[SearchFetcher] Unhandled search type: ${this.searchType}`);
}
makeRequest({ rejectWithError }) {
const endpoint = this.getEndpoint();
return endpoint != null ? HTTP.get({
url: endpoint,
query: Module1464.stringify(this.query),
oldFormErrors: true,
rejectWithError
}) : null;
}
}
export { SearchFetcherImpl };
class SearchTabFetcherImpl extends SearchFetcher {
constructor(a0, a1, a2, a3) {
super(a0, a1, a2);
this.payload = a3;
}
getEndpoint() {
const searchType = this.searchType;
if (SearchTypes.DMS === searchType) {
return Endpoints.SEARCH_TABS_DMS;
}
if (SearchTypes.GUILD_CHANNEL !== searchType && SearchTypes.GUILD !== searchType && SearchTypes.THREAD !== searchType) {
if (SearchTypes.CHANNEL === searchType) {
if (this.searchId != null && this.searchId !== "") {
return Endpoints.SEARCH_TABS_CHANNEL(this.searchId);
}
return void 0;
}
throw new Error(`[SearchFetcher] Unhandled search type: ${this.searchType}`);
}
if (this.searchId != null && this.searchId !== "") {
return Endpoints.SEARCH_TABS_GUILD(this.searchId);
}
}
makeRequest({ rejectWithError }) {
const endpoint = this.getEndpoint();
return endpoint != null ? HTTP.post({
url: endpoint,
body: this.payload,
oldFormErrors: true,
rejectWithError
}) : null;
}
}
export { SearchTabFetcherImpl };