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

415 lines
11 KiB
TypeScript

// Metro module 4526 (f4527).
import _slicedToArray from "../_bundle/_slicedToArray";
import AuthenticationStore from "./AuthenticationStore";
import ChannelStore from "./ChannelStore";
import GuildAvailabilityStore from "./GuildAvailabilityStore";
import { MAX_MESSAGE_LENGTH_PREMIUM } from "../Constants";
import Module12 from "../_bundle/Module12";
import SnowflakeUtils from "../utils/SnowflakeUtils";
import Module589 from "../_bundle/Module589";
import { isNotNullish } from "../utils/GlobalUtils";
import Dispatcher from "../Dispatcher";
let c2733_7, c2733_8, c2733_9, deleteDraft;
const handleChanged = function handleChanged(a0) {
const { type, channelId } = a0;
let draft = a0.draft;
const draftType = a0.draftType;
if (draft === ChannelStore.getChannel(channelId)?.template) {
draft = "";
}
L23: {
const id = AuthenticationStore.getId();
if (id != null && (draft != null && draft !== "")) {
let r34 = c2733_9[id];
if (r34 == null) {
const r36 = {};
c2733_9[id] = r36;
r34 = r36;
}
let r37 = r34[channelId];
if (r37 == null) {
const r38 = {};
r34[channelId] = r38;
r37 = r38;
}
const draft2 = draft.length > c2733_7 ? draft.substr(0, c2733_7) : draft;
if (draft2 === r37[draftType]?.draft) {
break L23;
}
r37[draftType] = {
timestamp: Date.now(),
draft: draft2
};
} else {
const id2 = AuthenticationStore.getId();
if (id2 != null) {
let r22 = c2733_9[id2];
if (r22 == null) {
const r23 = {};
c2733_9[id2] = r23;
r22 = r23;
}
const r24 = r22[channelId];
if (r24 != null) {
delete r24[draftType];
if (Module12.isEmpty(r24)) {
delete r22[channelId];
}
}
}
}
}
return type === "DRAFT_SAVE";
};
deleteDraft = function deleteDraft(a0, a1, r6 = AuthenticationStore.getId()) {
if (r6 == null) {
return false;
}
let r8 = c2733_9[r6];
if (r8 == null) {
const r9 = {};
c2733_9[r6] = r9;
r8 = r9;
}
const r11 = r8[a0];
if (r11 == null) {
return false;
}
delete r11[a1];
if (Module12.isEmpty(r11)) {
delete r8[a0];
}
};
const handleChannelDelete = function handleChannelDelete(a0) {
const id = a0.channel.id;
const id2 = AuthenticationStore.getId();
if (id2 != null) {
let r9 = c2733_9[id2];
if (r9 == null) {
const r11 = {};
c2733_9[id2] = r11;
r9 = r11;
}
delete r9[id];
}
return false;
};
c2733_7 = MAX_MESSAGE_LENGTH_PREMIUM + 500;
c2733_8 = {
ChannelMessage: 0,
0: "ChannelMessage",
ThreadSettings: 1,
1: "ThreadSettings",
FirstThreadMessage: 2,
2: "FirstThreadMessage",
ApplicationLauncherCommand: 3,
3: "ApplicationLauncherCommand",
Poll: 4,
4: "Poll",
SlashCommand: 5,
5: "SlashCommand",
ForwardContextMessage: 6,
6: "ForwardContextMessage",
InteractionModal: 7,
7: "InteractionModal"
};
c2733_9 = {};
class DraftStore extends Module589.PersistedStore {
static displayName = "DraftStore";
static persistKey = "DraftStore";
static migrations = [(a0) => {
if (a0 == null) {
return {};
}
for (const key in a0) {
if ("timestamp" in a0[key]) {
const r14 = {};
r14[c2733_8.ChannelMessage] = a0[key];
a0[key] = r14;
}
}
return a0;
}, (a0) => {
const id = AuthenticationStore.getId();
if (a0 != null && id != null) {
const r13 = {};
const r7 = {};
r13[id] = r7;
for (const key in a0) {
r7[key] = a0[key];
}
return r13;
}
return {};
}];
initialize(a0) {
c2733_9 = a0 == null ? {} : a0;
(function pruneEmptyDrafts() {
for (const item of SnowflakeUtils.entries(c2733_9)) {
const [r42, r13] = item;
for (const item2 of SnowflakeUtils.entries(r13)) {
const r55 = _slicedToArray(item2, 2);
const r56 = r55[0];
const r59 = r55[1][c2733_8.ChannelMessage];
if (r59 != null && !(r59.draft !== "" && r59.draft.trim() !== "")) {
deleteDraft(r56, c2733_8.ChannelMessage, r42);
}
}
}
})();
this.waitFor(AuthenticationStore, ChannelStore, GuildAvailabilityStore);
}
getState() {
return c2733_9;
}
getThreadDraftWithParentMessageId(c14301_0) {
const id = AuthenticationStore.getId();
if (id != null) {
let r11 = c2733_9[id];
if (r11 == null) {
const r12 = {};
c2733_9[id] = r12;
r11 = r12;
}
const r22 = SnowflakeUtils.keys(r11).find((item) => this.getThreadSettings(item)?.parentMessageId === c14301_0);
return r22 != null ? this.getThreadSettings(r22) : void 0;
}
}
getRecentlyEditedDrafts(c14302_0) {
const id = AuthenticationStore.getId();
if (id == null) {
return [];
}
let r5 = c2733_9[id];
if (r5 == null) {
const r10 = {};
c2733_9[id] = r10;
r5 = r10;
}
return Module12(r5).mapValues((a0) => a0 == null ? void 0 : a0[c14302_0]).pickBy(isNotNullish).toPairs().map((item) => {
const [channelId, r15] = item;
return {
channelId,
timestamp: r15.timestamp,
draft: r15.draft
};
}).sortBy((item) => -item.timestamp).value();
}
getDraft(a0, a1) {
const id = AuthenticationStore.getId();
if (id == null) {
return "";
}
let r6 = c2733_9[id];
if (r6 == null) {
const r8 = {};
c2733_9[id] = r8;
r6 = r8;
}
const r10 = r6[a0];
if (r10 != null) {
const r12 = r10[a1];
if (r12 != null) {
return r12.draft;
}
}
return "";
}
getThreadSettings(a0) {
const id = AuthenticationStore.getId();
if (id == null) {
return null;
}
let r8 = c2733_9[id];
if (r8 == null) {
const r9 = {};
c2733_9[id] = r9;
r8 = r9;
}
const r11 = r8[a0];
return r11 == null ? null : r11[c2733_8.ThreadSettings];
}
}
const draftStore = new DraftStore(Dispatcher, {
CONNECTION_OPEN: function handleConnectionOpen() {
const id = AuthenticationStore.getId();
if (!(id in c2733_9)) {
c2733_9[id] = {};
}
const id2 = AuthenticationStore.getId();
if (id2 != null && !(GuildAvailabilityStore.totalUnavailableGuilds > 0)) {
let r20 = c2733_9[id2];
if (r20 == null) {
const r22 = {};
c2733_9[id2] = r22;
r20 = r22;
}
for (const key in r20) {
if (ChannelStore.getChannel(key) == null) {
delete r20[key];
}
}
}
return false;
},
LOGOUT: function handleLogout(a0) {
if (!a0.isSwitchingAccount) {
c2733_9 = {};
}
},
MULTI_ACCOUNT_REMOVE_ACCOUNT: function handleMultiAccountRemove(a0) {
if (a0.userId in c2733_9) {
delete c2733_9[a0.userId];
}
},
GUILD_DELETE: function handleGuildDelete() {
const id = AuthenticationStore.getId();
if (id != null && !(GuildAvailabilityStore.totalUnavailableGuilds > 0)) {
let r7 = c2733_9[id];
if (r7 == null) {
const r16 = {};
c2733_9[id] = r16;
r7 = r16;
}
for (const key in r7) {
if (ChannelStore.getChannel(key) == null) {
delete r7[key];
}
}
}
return false;
},
CHANNEL_DELETE: handleChannelDelete,
THREAD_DELETE: handleChannelDelete,
THREAD_CREATE: function handleThreadCreate({ channel }) {
const id = AuthenticationStore.getId();
if (id != null && channel.ownerId !== id) {
let r11 = c2733_9[id];
if (r11 == null) {
const r20 = {};
c2733_9[id] = r20;
r11 = r20;
}
const r22 = r11[channel.parent_id];
if (r22 == null) {
return false;
}
const r25 = r22[c2733_8.ThreadSettings];
if (r25 == null) {
return false;
}
if (r25.parentMessageId !== SnowflakeUtils.castChannelIdAsMessageId(channel.id)) {
return false;
}
const r31 = r11[channel.parent_id];
if (r31 == null) {
return false;
}
let draft = r31[c2733_8.FirstThreadMessage]?.draft;
if (draft == null) {
draft = "";
}
if (draft !== "") {
const id2 = channel.id;
const r36 = {};
r36[c2733_8.ChannelMessage] = {
timestamp: Date.now(),
draft
};
r11[id2] = r36;
}
const parent_id = channel.parent_id;
const ThreadSettings = c2733_8.ThreadSettings;
const id3 = AuthenticationStore.getId();
if (id3 != null) {
let r46 = c2733_9[id3];
if (r46 == null) {
const r48 = {};
c2733_9[id3] = r48;
r46 = r48;
}
const r49 = r46[parent_id];
if (r49 != null) {
delete r49[ThreadSettings];
if (Module12.isEmpty(r49)) {
delete r46[parent_id];
}
}
}
const parent_id2 = channel.parent_id;
const FirstThreadMessage = c2733_8.FirstThreadMessage;
const id4 = AuthenticationStore.getId();
if (id4 != null) {
let r61 = c2733_9[id4];
if (r61 == null) {
const r63 = {};
c2733_9[id4] = r63;
r61 = r63;
}
const r64 = r61[parent_id2];
if (r64 != null) {
delete r64[FirstThreadMessage];
if (Module12.isEmpty(r64)) {
delete r61[parent_id2];
}
}
}
return void 0;
}
return false;
},
DRAFT_SAVE: handleChanged,
DRAFT_CHANGE: handleChanged,
DRAFT_CLEAR: function handleDraftClear({ channelId, draftType }) {
const id = AuthenticationStore.getId();
const r3 = id == null;
let r0 = false;
if (!r3) {
let r13 = c2733_9[id];
if (r13 == null) {
const r14 = {};
c2733_9[id] = r14;
r13 = r14;
}
const r15 = r13[channelId];
const r16 = r15 == null;
r0 = false;
if (!r16) {
delete r15[draftType];
const isEmpty = Module12.isEmpty(r15);
r0 = void 0;
if (isEmpty) {
delete r13[channelId];
r0 = void 0;
}
}
}
return r0;
},
THREAD_SETTINGS_DRAFT_CHANGE: function handleThreadSettingsDraftChanged({ channelId, draft }) {
const id = AuthenticationStore.getId();
if (id != null) {
let r6 = c2733_9[id];
if (r6 == null) {
const r14 = {};
c2733_9[id] = r14;
r6 = r14;
}
let r15 = r6[channelId];
if (r15 == null) {
const r16 = {};
r6[channelId] = r16;
r15 = r16;
}
r15[c2733_8.ThreadSettings] = {
timestamp: Date.now(),
...r15[c2733_8.ThreadSettings],
...draft,
parentChannelId: channelId
};
}
}
});
export default draftStore;
export { c2733_8 as DraftType };