From c5b8c0bd735d27a287fd57be7747b87b793fadae Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Wed, 14 May 2025 19:01:41 +0300 Subject: [PATCH] Message: Potential fix for missing messages after a long sleep (#5869) --- src/api/gramjs/updates/mtpUpdateHandler.ts | 13 +--- src/api/types/updates.ts | 3 +- src/global/actions/apiUpdaters/messages.ts | 87 ++++++++++++---------- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/api/gramjs/updates/mtpUpdateHandler.ts b/src/api/gramjs/updates/mtpUpdateHandler.ts index 809230020..25f3db98a 100644 --- a/src/api/gramjs/updates/mtpUpdateHandler.ts +++ b/src/api/gramjs/updates/mtpUpdateHandler.ts @@ -85,8 +85,6 @@ import { processMessageAndUpdateThreadInfo } from './entityProcessor'; import LocalUpdatePremiumFloodWait from './UpdatePremiumFloodWait'; import { LocalUpdateChannelPts, LocalUpdatePts } from './UpdatePts'; -const sentMessageIds = new Set(); - export function updater(update: Update) { if (update instanceof UpdateServerTimeOffset) { setServerTimeOffset(update.timeOffset); @@ -159,23 +157,22 @@ export function updater(update: Update) { if (update instanceof GramJs.UpdateNewScheduledMessage) { sendApiUpdate({ - '@type': sentMessageIds.has(message.id) ? 'updateScheduledMessage' : 'newScheduledMessage', + '@type': 'updateScheduledMessage', id: message.id, chatId: message.chatId, message, poll, + isFromNew: true, }); } else { - // We don't have preview for action or 'via bot' messages, so `newMessage` update here is required - const hasLocalCopy = sentMessageIds.has(message.id) && !message.viaBotId && !message.content.action; sendApiUpdate({ - '@type': hasLocalCopy ? 'updateMessage' : 'newMessage', + '@type': 'updateMessage', id: message.id, chatId: message.chatId, message, shouldForceReply, poll, - shouldCreateMessageIfNeeded: true, + isFromNew: true, }); } @@ -414,8 +411,6 @@ export function updater(update: Update) { message, }); } - } else if (update instanceof GramJs.UpdateMessageID || update instanceof GramJs.UpdateShortSentMessage) { - sentMessageIds.add(update.id); } else if (update instanceof GramJs.UpdateReadMessagesContents) { sendApiUpdate({ '@type': 'updateCommonBoxMessages', diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index c42e17073..1eef49642 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -234,7 +234,7 @@ export type ApiUpdateMessage = { message: Partial; poll?: ApiPoll; shouldForceReply?: boolean; - shouldCreateMessageIfNeeded?: true; + isFromNew?: true; }; export type ApiUpdateScheduledMessage = { @@ -243,6 +243,7 @@ export type ApiUpdateScheduledMessage = { id: number; message: Partial; poll?: ApiPoll; + isFromNew?: true; }; export type ApiUpdateQuickReplyMessage = { diff --git a/src/global/actions/apiUpdaters/messages.ts b/src/global/actions/apiUpdaters/messages.ts index affc2e99d..f905985ce 100644 --- a/src/global/actions/apiUpdaters/messages.ts +++ b/src/global/actions/apiUpdaters/messages.ts @@ -254,22 +254,61 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { break; } + case 'updateScheduledMessage': { + const { + chatId, id, message, poll, isFromNew, + } = update; + + const currentMessage = selectScheduledMessage(global, chatId, id); + if (!currentMessage) { + if (isFromNew) { + actions.apiUpdate({ + '@type': 'newScheduledMessage', + id: update.id, + chatId: update.chatId, + message: update.message as ApiMessage, + poll: update.poll, + }); + } + return; + } + + global = updateWithLocalMedia(global, chatId, id, message, true); + const ids = Object.keys(selectChatScheduledMessages(global, chatId) || {}).map(Number).sort((a, b) => b - a); + global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'scheduledIds', ids); + + const threadId = selectThreadIdFromMessage(global, currentMessage); + if (threadId !== MAIN_THREAD_ID) { + const threadScheduledIds = selectScheduledIds(global, chatId, threadId) || []; + global = replaceThreadParam(global, chatId, threadId, 'scheduledIds', threadScheduledIds.sort((a, b) => b - a)); + } + if (poll) { + global = updatePoll(global, poll.id, poll); + } + + setGlobal(global); + + break; + } + case 'updateMessage': { const { - chatId, id, message, poll, shouldCreateMessageIfNeeded, shouldForceReply, + chatId, id, message, poll, isFromNew, shouldForceReply, } = update; const currentMessage = selectChatMessage(global, chatId, id); - if (shouldCreateMessageIfNeeded && !currentMessage) { - actions.apiUpdate({ - '@type': 'newMessage', - id: update.id, - chatId: update.chatId, - message: update.message, - poll: update.poll, - shouldForceReply, - }); + if (!currentMessage) { + if (isFromNew) { + actions.apiUpdate({ + '@type': 'newMessage', + id: update.id, + chatId: update.chatId, + message: update.message, + poll: update.poll, + shouldForceReply, + }); + } return; } @@ -298,34 +337,6 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { break; } - case 'updateScheduledMessage': { - const { - chatId, id, message, poll, - } = update; - - const currentMessage = selectScheduledMessage(global, chatId, id); - if (!currentMessage) { - return; - } - - global = updateWithLocalMedia(global, chatId, id, message, true); - const ids = Object.keys(selectChatScheduledMessages(global, chatId) || {}).map(Number).sort((a, b) => b - a); - global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'scheduledIds', ids); - - const threadId = selectThreadIdFromMessage(global, currentMessage); - if (threadId !== MAIN_THREAD_ID) { - const threadScheduledIds = selectScheduledIds(global, chatId, threadId) || []; - global = replaceThreadParam(global, chatId, threadId, 'scheduledIds', threadScheduledIds.sort((a, b) => b - a)); - } - if (poll) { - global = updatePoll(global, poll.id, poll); - } - - setGlobal(global); - - break; - } - case 'updateQuickReplyMessage': { const { id, message, poll } = update;