// Metro module 9899 (f9900). import ChannelRTCStore from "../calls/ChannelRTCStore"; import GatewayConnectionStore from "../gateway/GatewayConnectionStore"; import { isTextChannel } from "../../records/ChannelRecord"; import ChannelSectionStore from "../../stores/ChannelSectionStore"; import ChannelStore from "../../stores/ChannelStore"; import GuildStore from "../../stores/GuildStore"; import ReadStateStore from "../../stores/ReadStateStore"; import SelectedChannelStore from "../../stores/SelectedChannelStore"; import SelectedGuildStore from "../../stores/SelectedGuildStore"; import { AbortCodes, CURRENT_APP_CONTEXT, ChannelTypes, ChannelTypesSets, MAX_MESSAGES_PER_CHANNEL, Routes } from "../../Constants"; import { isStaticChannelRoute } from "../channel/ChannelConstants"; import Durations from "../../utils/Durations"; import Logger from "../debug/Logger"; import ChannelMessages from "../../lib/ChannelMessages"; import { messageHasExpiredAttachmentUrl } from "./SignedAttachmentLinkUtils"; import { JumpType } from "../../flow/Client"; import isChangelogChannel from "../changelog/utils/isChangelogChannel"; import MessageActionCreators from "../../actions/MessageActionCreators"; import { getMessageLimit } from "./getAdaptiveMessageLimit.native"; import { Storage } from "../../_bundle/Module595"; import { matchPath } from "../routing/matchPathCompat"; import { getHistory } from "../routing/router_utils"; import { SidebarType } from "../sidebar/SidebarActionTypes"; import AlertActionCreators from "../../actions/AlertActionCreators"; import { intl, t } from "../../intl/index.native"; import Dispatcher from "../../Dispatcher"; import AutomaticLifecycleManager from "../../lib/AutomaticLifecycleManager"; let c6569_20, c6569_21, c6569_23, c6569_24, c6569_25, handleConnectionOpen, loadSelectedChannelIfNecessary, handleChannelSelect, handleVoiceChannelSelect, handleJumpToVoiceChannelMessage, handleChannelSectionStoreChange, handleChannelPreload, handleChannelCreate, handleMessageEditEnd, c6569_35, handleLoadMessagesSuccess, handleUploadFail, handleAppWillBecomeActive; const fetchMessages = function fetchMessages(a0) { let r86; L61: { const { guildId, channelId, messageId, forceFetch, isPreload, jumpType, skipLocalFetch, avoidInitialScroll, fetchKey } = a0; if (channelId != null && !isStaticChannelRoute(channelId)) { const channel = ChannelStore.getChannel(channelId); if (channel?.type !== ChannelTypes.GUILD_STORE) { if (channel?.type != null && ChannelTypesSets.GUILD_THREADS_ONLY.has(channel.type)) { break L61; } const orCreate = ChannelMessages.getOrCreate(channelId); const r47 = orCreate.some(messageHasExpiredAttachmentUrl); let r48 = orCreate; if (r47) { c6569_21.log("Found expired attachment link, clearing messages"); ChannelMessages.clear(channelId); r48 = ChannelMessages.getOrCreate(channelId); } const r60 = r48.jumpTargetId != null && messageId == null; let r61 = r48; if (r60) { const r67 = r48.mutate({ jumpTargetId: null, jumped: false, jumpType: JumpType.ANIMATED }); ChannelMessages.commit(r67); r61 = r67; } const r73 = r61.focusTargetId != null && messageId == null; let r74 = r61; if (r73) { const r77 = r61.mutate({ focusTargetId: null }); ChannelMessages.commit(r77); r74 = r77; } L31: { if (isPreload && !GatewayConnectionStore.isConnected()) { const loadingMore = r74.loadingMore; r86 = true; if (!loadingMore) { break L31; } } L29: { if (!r74.loadingMore) { if (r74.ready && !r74.cached) { break L29; } const r90 = guildId != null && GuildStore.getGuild(guildId) == null; r86 = forceFetch; if (r90) { break L31; } r86 = true; break L31; } } r86 = messageId != null ? true : forceFetch; } if (isChangelogChannel(channelId) && ReadStateStore.hasUnread(channelId)) { r86 = true; } if (r86) { ChannelMessages.commit(r74.mutate({ loadingMore: true })); if (messageId == null) { if (channel == null ? void 0 : channel.isThread()) { const hasOpenedThread = ReadStateStore.hasOpenedThread(channelId); let r117 = false; if (!hasOpenedThread) { if (c6569_23 == null) { let r125 = Storage.get(c6569_25, {}); if (r125 == null) { r125 = {}; } c6569_23 = r125; } const r5 = channelId in c6569_23; r117 = false; if (!r5) { c6569_23[channelId] = Date.now(); const r0 = Date.now() - c6569_24; const r136 = c6569_23; for (const key in r136) { if (c6569_23[key] < r0) { delete c6569_23[key]; } } Storage.set(c6569_25, c6569_23); r117 = true; } } if (r117) { c6569_21.log(`Jumping to start of thread ${channel.id}`); return MessageActionCreators.fetchMessages({ channelId, limit: getMessageLimit("MessageManager.threadStart"), jump: { messageId: channelId, flash: false }, isPreload, skipLocalFetch, avoidInitialScroll, fetchKey }); } } if ((channel == null ? void 0 : channel.isThread()) && ReadStateStore.hasTrackedUnread(channel.id) && !r74.ready) { const trackedAckMessageId = ReadStateStore.getTrackedAckMessageId(channel.id); c6569_21.log(`Jumping to most recent message in thread ${channel.id} - ${trackedAckMessageId}`); return MessageActionCreators.fetchMessages({ channelId, limit: getMessageLimit("MessageManager.threadUnread"), jump: { messageId: trackedAckMessageId, flash: false, offset: 1 }, isPreload, skipLocalFetch, avoidInitialScroll, fetchKey }); } return MessageActionCreators.fetchMessages({ channelId, limit: getMessageLimit("MessageManager.initialFetch"), isPreload, skipLocalFetch, jump: { jumpType: JumpType.ANIMATED }, avoidInitialScroll, fetchKey }); } MessageActionCreators.jumpToMessage({ channelId, messageId, flash: true, isPreload, skipLocalFetch, jumpType, avoidInitialScroll }); } } } } }; handleConnectionOpen = function handleConnectionOpen() { const channelId = SelectedChannelStore.getChannelId(); if (channelId != null) { const [r8] = ChannelRTCStore.getOpenChatChannelIds(); if (r8 != null && r8 !== channelId) { const channel = ChannelStore.getChannel(r8); if (channel != null) { fetchMessages({ guildId: channel.getGuildId(), channelId: channel.id }); } } const channel2 = ChannelStore.getChannel(channelId); if (channel2 != null) { channel2.id; const r45 = matchPath(getHistory().location.pathname, { path: Routes.CHANNEL(":guild", ":channel", ":message"), exact: true }); const messageId = r45 == null ? void 0 : r45.params?.message; fetchMessages({ guildId: channel2.getGuildId(), channelId: channel2.id, messageId, avoidInitialScroll: messageId != null }); const guildId = channel2.getGuildId(); const id = channel2.id; const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(id); if (currentSidebarChannelId != null) { fetchMessages({ guildId, channelId: currentSidebarChannelId, messageId: ChannelSectionStore.getCurrentSidebarMessageId(id) }); } } } }; loadSelectedChannelIfNecessary = function loadSelectedChannelIfNecessary() { const channelId = SelectedChannelStore.getChannelId(); if (channelId != null) { const channel = ChannelStore.getChannel(channelId); if (channel != null) { if (isTextChannel(channel.type)) { const orCreate = ChannelMessages.getOrCreate(channelId); if (!(orCreate.ready && orCreate.hasFetched)) { fetchMessages({ guildId: channel.getGuildId(), channelId: channel.id }); } const guildId2 = channel.getGuildId(); const id2 = channel.id; const currentSidebarChannelId2 = ChannelSectionStore.getCurrentSidebarChannelId(id2); if (currentSidebarChannelId2 != null) { fetchMessages({ guildId: guildId2, channelId: currentSidebarChannelId2, messageId: ChannelSectionStore.getCurrentSidebarMessageId(id2) }); } } else { const guildId = channel.getGuildId(); const id = channel.id; const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(id); if (currentSidebarChannelId != null) { fetchMessages({ guildId, channelId: currentSidebarChannelId, messageId: ChannelSectionStore.getCurrentSidebarMessageId(id) }); } } } } }; handleChannelSelect = function handleChannelSelect(a0) { const { guildId, channelId, messageId, jumpType } = a0; if (a0.skipMessageFetch) { return false; } fetchMessages({ guildId, channelId, messageId, jumpType }); const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(channelId); if (currentSidebarChannelId != null) { fetchMessages({ guildId, channelId: currentSidebarChannelId, messageId: ChannelSectionStore.getCurrentSidebarMessageId(channelId) }); } }; handleVoiceChannelSelect = function handleVoiceChannelSelect(a0) { fetchMessages({ guildId: a0.guildId, channelId: a0.channelId }); }; handleJumpToVoiceChannelMessage = function handleJumpToVoiceChannelMessage(a0) { fetchMessages({ guildId: a0.guildId, channelId: a0.channelId, messageId: a0.messageId, jumpType: a0.jumpType }); }; handleChannelSectionStoreChange = function handleChannelSectionStoreChange() { const channelId = SelectedChannelStore.getChannelId(); const guildId = SelectedGuildStore.getGuildId(); if (guildId != null && channelId != null) { if (ChannelSectionStore.getSidebarState(channelId)?.type !== SidebarType.VIEW_CHANNEL) { const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(channelId); if (currentSidebarChannelId != null) { fetchMessages({ guildId, channelId: currentSidebarChannelId, messageId: ChannelSectionStore.getCurrentSidebarMessageId(channelId) }); } } } }; handleChannelPreload = function handleChannelPreload(a0) { const { guildId, channelId } = a0; if (a0.context === CURRENT_APP_CONTEXT) { fetchMessages({ guildId, channelId }); const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(channelId); if (currentSidebarChannelId != null) { fetchMessages({ guildId, channelId: currentSidebarChannelId, messageId: ChannelSectionStore.getCurrentSidebarMessageId(channelId) }); } } }; handleChannelCreate = function handleChannelCreate({ channel, messageId }) { const guild_id = channel.guild_id; if (guild_id != null && SelectedChannelStore.getChannelId(guild_id) === channel.id) { fetchMessages({ guildId: guild_id, channelId: channel.id, messageId }); } }; handleMessageEditEnd = function handleMessageEditEnd({ response }) { if (response != null && response.body != null) { if (response.body.code === AbortCodes.CHANNEL_FOLLOWING_EDIT_RATE_LIMITED) { const retry_after = response.body.retry_after; if (retry_after != null) { AlertActionCreators.show({ title: intl.string(t.Whhv4w), body: intl.formatToPlainString(t.qoxdQB, { retryAfterMinutes: Math.ceil(retry_after / 60) }) }); } } return void 0; } return null; }; handleLoadMessagesSuccess = function handleLoadMessagesSuccess(a0) { const { channelId, jump } = a0; let isStale = a0.isStale; let isPreview = a0.isPreview; if (isPreview === void 0) { isPreview = false; } if (!isPreview) { let r11 = c6569_35[channelId]; if (r11 == null) { r11 = 0; } if (!(Date.now() - r11 < c6569_20)) { c6569_35[channelId] = Date.now(); const channelId2 = SelectedChannelStore.getChannelId(); const currentSidebarChannelId = ChannelSectionStore.getCurrentSidebarChannelId(channelId2); if (isStale) { isStale = GatewayConnectionStore.isConnected(); } if (isStale) { isStale = channelId === channelId2 || channelId === currentSidebarChannelId; } if (isStale) { MessageActionCreators.fetchMessages({ channelId, limit: getMessageLimit("MessageManager.staleFetch"), jump }); } } } }; handleUploadFail = function handleUploadFail(a0) { const { channelId, messageId } = a0; let reason = a0.reason; const { noSendFailed, shouldSendNotification } = a0; if (messageId != null && noSendFailed !== true) { if (reason == null) { reason = null; } Dispatcher.dispatch({ type: "MESSAGE_SEND_FAILED", channelId, messageId, reason, shouldNotify: shouldSendNotification !== false }); } }; handleAppWillBecomeActive = function handleAppWillBecomeActive() { const channelId = SelectedChannelStore.getChannelId(); if (channelId == null) { return false; } MessageActionCreators.fetchNewLocalMessages(channelId, MAX_MESSAGES_PER_CHANNEL); }; c6569_20 = 10 * Durations.Millis.SECOND; c6569_21 = new Logger("MessageManager"); c6569_24 = 90 * Durations.Millis.DAY; c6569_25 = "viewedThreadIds"; c6569_35 = {}; class MessageManager extends AutomaticLifecycleManager { fetchMessages = fetchMessages; loadSelectedChannelIfNecessary = loadSelectedChannelIfNecessary; stores = new Map().set(ChannelSectionStore, handleChannelSectionStoreChange); actions = { APP_STATE_UPDATE_WILL_BECOME_ACTIVE: handleAppWillBecomeActive, OVERLAY_INITIALIZE: handleConnectionOpen, CHANNEL_SELECT: handleChannelSelect, VOICE_CHANNEL_SELECT: handleVoiceChannelSelect, THREAD_CREATE: handleChannelCreate, THREAD_LIST_SYNC: () => { loadSelectedChannelIfNecessary(); }, CHANNEL_CREATE: handleChannelCreate, CHANNEL_PRELOAD: handleChannelPreload, GUILD_CREATE: () => { loadSelectedChannelIfNecessary(); }, MESSAGE_END_EDIT: handleMessageEditEnd, LOAD_MESSAGES_SUCCESS: handleLoadMessagesSuccess, UPLOAD_FAIL: handleUploadFail, CHANNEL_DELETE: () => { loadSelectedChannelIfNecessary(); }, THREAD_DELETE: () => { loadSelectedChannelIfNecessary(); }, CHANNEL_RTC_JUMP_TO_VOICE_CHANNEL_MESSAGE: handleJumpToVoiceChannelMessage }; _initialize() { Dispatcher.subscribe("CONNECTION_OPEN", handleConnectionOpen); } _terminate() { Dispatcher.unsubscribe("CONNECTION_OPEN", handleConnectionOpen); } } const messageManager = new MessageManager(); export default messageManager;