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

437 lines
14 KiB
TypeScript

// Metro module 9505 (f9506).
import _slicedToArray from "../../_bundle/_slicedToArray";
import ExperimentStore from "../experiments/ExperimentStore";
import QuickSwitcherStore from "../quickswitcher/QuickSwitcherStore";
import ChannelStore from "../../stores/ChannelStore";
import GuildStore from "../../stores/GuildStore";
import ReadStateStore from "../../stores/ReadStateStore";
import SelectedChannelStore from "../../stores/SelectedChannelStore";
import UserGuildSettingsStore from "../../stores/UserGuildSettingsStore";
import UserStore from "../../stores/UserStore";
import { SUMMARY_POLL_INTERVAL } from "./SummaryConstants";
import { AutocompleterResultTypes } from "../autocompleter/index";
import Module589 from "../../_bundle/Module589";
import SnowflakeUtils from "../../utils/SnowflakeUtils";
import Durations from "../../utils/Durations";
import { canSeeChannelSummaries } from "../../experiments/ChannelSummariesExperiment";
import Dispatcher from "../../Dispatcher";
import { createSummaryFromServer } from "./Summary";
import Module12, { sortBy } from "../../_bundle/Module12";
let shouldShowTopicsBar2, c6269_4, c6269_5, c6269_6, c6269_17, c6269_18, c6269_19, c6269_20, c6269_21, c6269_22, c6269_23, c6269_24, c6269_25;
const handleQuickSwitcherUpdate = function handleQuickSwitcherUpdate() {
c6269_24 = QuickSwitcherStore.getProps().results.filter((result) => result.type === AutocompleterResultTypes.TEXT_CHANNEL && result.record.type === 0).map((item) => item.record.id);
};
c6269_17 = {
FETCHING: "fetching",
OK: "ok",
ERROR: "error"
};
c6269_18 = {};
c6269_19 = {};
c6269_20 = {};
c6269_21 = [];
c6269_22 = {};
c6269_23 = {
status: c6269_17.OK,
lastRequest: null,
lastResponse: null
};
c6269_24 = [];
c6269_25 = [];
class SummaryStore extends Module589.PersistedStore {
static persistKey = "SummaryStore";
getState() {
return {
shouldShowTopicsBar: shouldShowTopicsBar2
};
}
initialize(a0) {
let r4 = a0?.shouldShowTopicsBar;
if (r4 == null) {
r4 = true;
}
shouldShowTopicsBar2 = r4;
this.waitFor(ChannelStore, ExperimentStore, GuildStore, QuickSwitcherStore, ReadStateStore, SelectedChannelStore, UserGuildSettingsStore, UserStore);
this.syncWith([QuickSwitcherStore], handleQuickSwitcherUpdate);
}
allSummaries() {
return c6269_18;
}
topSummaries() {
return Object.values(c6269_18).flat().filter((item) => item.people.length > 1 && SnowflakeUtils.extractTimestamp(item.endId) > new Date().getTime() - 5 * Durations.Millis.HOUR).sort((a, b) => SnowflakeUtils.extractTimestamp(b.endId) - SnowflakeUtils.extractTimestamp(a.endId));
}
summaries(a0) {
let r4 = c6269_18[a0];
if (r4 == null) {
r4 = c6269_25;
}
return r4;
}
shouldShowTopicsBar() {
return shouldShowTopicsBar2;
}
findSummary(a0, c17258_0) {
let r9 = this.summaries(a0).find((item) => item.id === c17258_0);
if (r9 == null) {
r9 = null;
}
return r9;
}
selectedSummary(a0) {
const r1 = c6269_6 != null;
const r8 = r1 ? c6269_6.channelId === a0 ? c6269_6.summaryId != null ? this.findSummary(a0, c6269_6?.summaryId) : null : null : null;
return r8;
}
summaryFeedback(a0) {
return a0 != null ? c6269_20[a0.id] : null;
}
isFetching(a0, a1) {
return a1 != null ? c6269_19[a0]?.summaryId === a1 : c6269_19[a0]?.fetching === true;
}
status(a0) {
return c6269_19[a0];
}
shouldFetch(a0, a1) {
const r9 = c6269_19[a0];
if (canSeeChannelSummaries(ChannelStore.getChannel(a0))) {
if (a1 != null) {
let r28 = r9?.summaryIdLastRequestedAt;
if (r28 == null) {
r28 = 0;
}
const r33 = Date.now() - r28;
return a1 !== r9?.summaryId || r33 > SUMMARY_POLL_INTERVAL;
}
let r21 = r9?.lastReceivedAt;
if (r21 == null) {
r21 = 0;
}
const r25 = Boolean(r9?.fetching);
return r25 ? !r25 : r21 === 0;
}
return false;
}
channelAffinities() {
return c6269_21;
}
channelAffinitiesById() {
return c6269_22;
}
channelAffinitiesStatus() {
return c6269_23;
}
shouldFetchChannelAffinities() {
return c6269_23.status !== c6269_17.FETCHING && !(c6269_23.lastResponse != null && Date.now() - c6269_23.lastResponse < 30 * Durations.Millis.SECOND);
}
defaultChannelIds({ withQuickSwitcher, withChannelAffinities, withUnreads, numChannels = 25 }) {
const r9 = [];
const r8 = withQuickSwitcher ? r9.concat(c6269_24) : r9;
const r13 = withChannelAffinities ? r8.concat(c6269_21.map((item) => item.channel_id)) : r8;
return (withUnreads ? r13.filter((item) => {
const channel = ChannelStore.getChannel(item);
return channel != null && !UserGuildSettingsStore.isChannelMuted(channel.guild_id, item) && ReadStateStore.hasUnread(item);
}) : r13).filter((item) => canSeeChannelSummaries(ChannelStore.getChannel(item), false, false)).slice(0, numChannels);
}
visibleSummaryIndex() {
return c6269_5;
}
}
const summaryStore = new SummaryStore(Dispatcher, {
CONNECTION_OPEN: () => false,
CHANNEL_SELECT: (a0) => {
if (c6269_6?.channelId !== a0.channelId) {
c6269_6 = null;
}
},
TOGGLE_TOPICS_BAR: () => {
shouldShowTopicsBar2 = !shouldShowTopicsBar2;
},
RECEIVE_CHANNEL_SUMMARY: ({ summary, channelId: channelId2, error: error2, receivedAt: receivedAt2 }) => {
let summaryFromServer;
summaryFromServer = void 0;
if (summary != null && Object.keys(summary).length > 0) {
summaryFromServer = createSummaryFromServer(summary, channelId2);
let r25 = c6269_18[channelId2];
if (r25 == null) {
r25 = [];
}
const r26 = [...r25];
const r30 = r26.findIndex((item) => item.id === summaryFromServer?.id);
if (r30 > -1) {
r26[r30] = summaryFromServer;
} else {
r26.push(summaryFromServer);
}
c6269_18[channelId2] = r26;
}
let r36 = c6269_19[channelId2];
if (r36 == null) {
r36 = {
fetching: false
};
}
c6269_19[channelId2] = {
...r36,
summaryId: void 0,
summaryIdLastReceivedAt: receivedAt2,
summaryIdError: error2
};
},
REQUEST_CHANNEL_SUMMARY: ({ channelId: channelId2, summaryId, requestedAt: requestedAt2 }) => {
let r6 = c6269_19[channelId2];
if (r6 == null) {
r6 = {
fetching: false
};
}
c6269_19[channelId2] = {
...r6,
summaryId,
summaryIdLastRequestedAt: requestedAt2
};
},
RECEIVE_CHANNEL_SUMMARIES: ({ summaries: summaries2, channelId, error: error2, receivedAt: receivedAt2 }) => {
const r11 = summaries2.filter((item) => Object.keys(item).length > 0).map((item) => createSummaryFromServer(item, channelId));
if (c6269_6 != null && c6269_6.channelId === channelId && !r11.some((item) => item.id === c6269_6?.summaryId)) {
let r25 = c6269_18[channelId];
if (r25 == null) {
r25 = [];
}
const r28 = r25.find((item) => item.id === c6269_6?.summaryId);
if (r28 != null) {
r11.push(r28);
}
}
c6269_18[channelId] = sortBy(r11, (a0) => SnowflakeUtils.extractTimestamp(a0.startId)).reverse();
const r41 = {
...c6269_19[channelId],
fetching: false,
error: void 0,
lastReceivedAt: receivedAt2
};
if (error2 != null) {
r41.error = error2;
}
c6269_19[channelId] = r41;
},
REQUEST_CHANNEL_SUMMARIES: (a0) => {
const channelId2 = a0.channelId;
let r9 = c6269_19[a0.channelId];
if (r9 == null) {
r9 = {};
}
c6269_19[channelId2] = {
...r9,
fetching: true,
lastRequestedAt: a0.requestedAt
};
},
SET_HIGHLIGHTED_SUMMARY: (a0) => {
if (c6269_4 == null && a0.channelId == null) {
return false;
}
if (a0.channelId === c6269_4?.channelId) {
if (a0.summaryId === c6269_4?.summaryId) {
return false;
}
}
const r15 = a0.channelId != null;
let r16 = null;
if (r15) {
let summaryId = a0.summaryId;
if (summaryId == null) {
summaryId = null;
}
r16 = {
channelId: a0.channelId,
summaryId
};
}
c6269_4 = r16;
if (r16 != null && c6269_4.channelId === a0.channelId && c6269_4.summaryId != null) {
const r28 = c6269_18[c6269_4.channelId];
c6269_5 = r28 == null ? void 0 : r28.findIndex((item) => item.id === c6269_4?.summaryId);
}
},
UPDATE_VISIBLE_MESSAGES: (a0) => {
let c17261_0;
L10: {
c17261_0 = a0;
const channelId2 = SelectedChannelStore.getChannelId();
if (channelId2 != null) {
if (c6269_4 != null && c6269_4.channelId === channelId2 && c6269_4.summaryId != null) {
const r21 = c6269_18[c6269_4.channelId];
c6269_5 = r21 == null ? void 0 : r21.findIndex((item) => item.id === c6269_4?.summaryId);
break L10;
}
const r6 = c6269_18[channelId2];
c6269_5 = r6 == null ? void 0 : r6.findIndex((item) => {
const { topVisibleMessage, bottomVisibleMessage } = c17261_0;
const { startId, endId } = item;
return !(topVisibleMessage == null || topVisibleMessage > endId || bottomVisibleMessage == null || bottomVisibleMessage < startId);
});
}
}
},
SET_SELECTED_SUMMARY: (a0) => {
const channelId2 = a0.channelId;
const r0 = channelId2 == null;
let r1 = null;
if (!r0) {
let r11 = channelId2 !== c6269_6?.channelId || a0.summaryId !== c6269_6?.summaryId;
if (r11) {
let summaryId = a0.summaryId;
if (summaryId == null) {
summaryId = null;
}
c6269_6 = {
channelId: channelId2,
summaryId
};
r11 = void 0;
}
r1 = r11;
}
return r1;
},
SET_SUMMARY_FEEDBACK: ({ summary, rating }) => {
if (rating != null) {
c6269_20[summary.id] = rating;
} else {
delete c6269_20[summary.id];
}
},
REQUEST_CHANNEL_AFFINITIES: () => {
c6269_23 = {
...c6269_23,
status: c6269_17.FETCHING,
lastRequest: Date.now()
};
},
RECEIVE_CHANNEL_AFFINITIES: (a0) => {
const affinities = a0.affinities;
if (a0.error != null) {
c6269_21 = [];
c6269_22 = {};
c6269_23 = {
...c6269_23,
status: c6269_17.ERROR,
lastResponse: Date.now()
};
return void 0;
}
let r12 = affinities;
if (r12 == null) {
r12 = [];
}
c6269_21 = r12;
let r13 = affinities == null ? void 0 : affinities.reduce((acc, affinity) => {
acc[affinity.channel_id] = affinity.affinity;
return acc;
}, {});
if (r13 == null) {
r13 = {};
}
c6269_22 = r13;
c6269_23 = {
...c6269_23,
status: c6269_17.OK,
lastResponse: Date.now()
};
},
REQUEST_CHANNEL_SUMMARIES_BULK: ({ channelIds, requestedAt }) => {
c6269_19 = {
...c6269_19,
...channelIds.reduce((acc, channelId) => {
let r5 = c6269_19[channelId];
if (r5 == null) {
r5 = {};
}
acc[channelId] = {
...r5,
fetching: true,
lastRequestedAt: requestedAt,
error: void 0
};
return acc;
}, {})
};
},
RECEIVE_CHANNEL_SUMMARIES_BULK: (a0) => {
const { summaries: summaries2, receivedAt, error } = a0;
const channelIds = a0.requestArgs.channelIds;
let c17263_2 = Module12.toPairs(summaries2).reduce((acc, a1) => {
const r10 = _slicedToArray(a1, 2);
const r11 = r10[0];
let c26462_0 = r11;
acc[r11] = Module12.chain(r10[1].map((item) => createSummaryFromServer(item, c26462_0))).sortBy((item) => SnowflakeUtils.extractTimestamp(item.startId)).takeRight(75).reverse().filter((item) => Object.keys(item).length > 0).value();
return acc;
}, {});
const r26 = channelIds.reduce((acc, channelId) => {
let r6 = c6269_19[channelId];
if (r6 == null) {
r6 = {};
}
const r3 = c17263_2[channelId];
if (r3 != null) {
acc.summariesByChannel[channelId] = r3;
}
acc.summaryFetchStatusByChannel[channelId] = {
...r6,
fetching: false,
error,
lastReceivedAt: receivedAt
};
return acc;
}, {
summariesByChannel: {},
summaryFetchStatusByChannel: {}
});
c6269_18 = {
...c6269_18,
...r26.summariesByChannel
};
c6269_19 = {
...c6269_19,
...r26.summaryFetchStatusByChannel
};
},
CONVERSATION_SUMMARY_UPDATE: (a0) => {
const { channel_id, summaries: summaries2 } = a0;
a0.guild_id;
const lastReceivedAt = Date.now();
const r34 = Module12.chain(summaries2).sortBy((item) => SnowflakeUtils.extractTimestamp(item.start_id)).filter((item) => Object.keys(item).length > 0).map((item) => createSummaryFromServer(item, channel_id)).reverse().value();
let r36 = c6269_18[channel_id];
if (r36 == null) {
r36 = [];
}
c6269_18[channel_id] = Module12.chain(r34).concat(r36).sortBy((item) => SnowflakeUtils.extractTimestamp(item.startId)).takeRight(75).uniqBy("id").reverse().value();
let fetching = c6269_19[channel_id]?.fetching;
if (fetching == null) {
fetching = false;
}
c6269_19[channel_id] = {
...c6269_19[channel_id],
error: void 0,
fetching,
lastReceivedAt
};
},
CLEAR_CONVERSATION_SUMMARIES: () => {
c6269_18 = {};
c6269_19 = {};
},
DELETE_SUMMARY: (a0) => {
const channelId2 = a0.summary.channelId;
let r6 = c6269_18[channelId2];
if (r6 == null) {
r6 = [];
}
const r10 = r6.indexOf(a0.summary);
if (r10 !== -1) {
c6269_18[channelId2].splice(r10, 1);
}
}
});
export default summaryStore;