diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index f276bd294..5143cc832 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -30,7 +30,7 @@ import { selectChat, selectUser, selectChatListType, selectIsChatPinned, selectChatFolder, selectSupportChat, selectChatByUsername, selectThreadTopMessageId, selectCurrentMessageList, selectThreadInfo, selectCurrentChat, selectLastServiceNotification, - selectVisibleUsers, selectUserByPhoneNumber, + selectVisibleUsers, selectUserByPhoneNumber, selectDraft, } from '../../selectors'; import { buildCollectionByKey, omit } from '../../../util/iteratees'; import { debounce, pause, throttle } from '../../../util/schedulers'; @@ -209,7 +209,7 @@ addActionHandler('loadAllChats', async (global, actions, payload) => { .sort((chat1, chat2) => getOrderDate(chat1)! - getOrderDate(chat2)!)[0] : undefined; - await loadChats(listType, oldestChat?.id, oldestChat ? getOrderDate(oldestChat) : undefined, shouldReplace); + await loadChats(listType, oldestChat?.id, oldestChat ? getOrderDate(oldestChat) : undefined, shouldReplace, true); if (shouldReplace) { onReplace?.(); @@ -1174,7 +1174,11 @@ addActionHandler('processAttachBotParameters', async (global, actions, payload) }); async function loadChats( - listType: 'active' | 'archived', offsetId?: string, offsetDate?: number, shouldReplace = false, + listType: 'active' | 'archived', + offsetId?: string, + offsetDate?: number, + shouldReplace = false, + isFullDraftSync?: boolean, ) { let global = getGlobal(); const lastLocalServiceMessage = selectLastServiceNotification(global)?.message; @@ -1245,13 +1249,17 @@ async function loadChats( global = updateChatListSecondaryInfo(global, listType, result); - Object.keys(result.draftsById).forEach((chatId) => { - global = replaceThreadParam( - global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId], - ); + const idsToUpdateDraft = isFullDraftSync ? result.chatIds : Object.keys(result.draftsById); + idsToUpdateDraft.forEach((chatId) => { + if (!selectDraft(global, chatId, MAIN_THREAD_ID)?.isLocal) { + global = replaceThreadParam( + global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId], + ); + } }); - Object.keys(result.replyingToById).forEach((chatId) => { + const idsToUpdateReplyingToId = isFullDraftSync ? result.chatIds : Object.keys(result.replyingToById); + idsToUpdateReplyingToId.forEach((chatId) => { global = replaceThreadParam( global, chatId, MAIN_THREAD_ID, 'replyingToId', result.replyingToById[chatId], ); diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index 3129c85e1..e6b559648 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -326,30 +326,35 @@ addActionHandler('cancelSendingMessage', (global, actions, payload) => { }); }); -addActionHandler('saveDraft', (global, actions, payload) => { +addActionHandler('saveDraft', async (global, actions, payload) => { const { chatId, threadId, draft } = payload!; if (!draft) { - return undefined; + return; } const { text, entities } = draft; const chat = selectChat(global, chatId)!; const user = selectUser(global, chatId)!; - if (user && isDeletedUser(user)) return undefined; + if (user && isDeletedUser(user)) return; if (threadId === MAIN_THREAD_ID) { - void callApi('saveDraft', { + const result = await callApi('saveDraft', { chat, text, entities, replyToMsgId: selectReplyingToId(global, chatId, threadId), }); + + if (!result) { + draft.isLocal = true; + } } + global = getGlobal(); global = replaceThreadParam(global, chatId, threadId, 'draft', draft); global = updateChat(global, chatId, { draftDate: Math.round(Date.now() / 1000) }); - return global; + setGlobal(global); }); addActionHandler('clearDraft', (global, actions, payload) => { diff --git a/src/global/types.ts b/src/global/types.ts index 97bbccf7c..0e8a5d28c 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -113,7 +113,7 @@ export interface Thread { editingScheduledId?: number; editingDraft?: ApiFormattedText; editingScheduledDraft?: ApiFormattedText; - draft?: ApiFormattedText; + draft?: ApiFormattedText & { isLocal?: boolean }; noWebPage?: boolean; threadInfo?: ApiThreadInfo; firstMessageId?: number;