diff --git a/src/api/gramjs/apiBuilders/chats.ts b/src/api/gramjs/apiBuilders/chats.ts index f7693b8d9..bf3372898 100644 --- a/src/api/gramjs/apiBuilders/chats.ts +++ b/src/api/gramjs/apiBuilders/chats.ts @@ -76,7 +76,7 @@ export function buildApiChatFromDialog( unreadMentionsCount, isMuted, ...(unreadMark && { hasUnreadMark: true }), - ...(draft instanceof GramJs.DraftMessage && { hasDraft: true }), + ...(draft instanceof GramJs.DraftMessage && { draftDate: draft.date }), ...buildApiChatFieldsFromPeerEntity(peerEntity), }; } diff --git a/src/api/gramjs/apiBuilders/messages.ts b/src/api/gramjs/apiBuilders/messages.ts index 3b119c223..f0ea2376c 100644 --- a/src/api/gramjs/apiBuilders/messages.ts +++ b/src/api/gramjs/apiBuilders/messages.ts @@ -188,9 +188,14 @@ export function buildMessageDraft(draft: GramJs.TypeDraftMessage) { return undefined; } + const { + message, entities, replyToMsgId, date, + } = draft; + return { - formattedText: draft.message ? buildMessageTextContent(draft.message, draft.entities) : undefined, - replyingToId: draft.replyToMsgId, + formattedText: message ? buildMessageTextContent(message, entities) : undefined, + replyingToId: replyToMsgId, + date, }; } diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index cc69ec2ba..67e365990 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -982,7 +982,7 @@ function updateLocalDb(result: ( } } -export async function importChatInvite({ hash }: {hash: string}) { +export async function importChatInvite({ hash }: { hash: string }) { const updates = await invokeRequest(new GramJs.messages.ImportChatInvite({ hash }), true); if (!(updates instanceof GramJs.Updates) || !updates.chats.length) { return undefined; diff --git a/src/api/gramjs/updater.ts b/src/api/gramjs/updater.ts index 2de9769be..cc77b75ee 100644 --- a/src/api/gramjs/updater.ts +++ b/src/api/gramjs/updater.ts @@ -771,12 +771,10 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) { // Misc } else if (update instanceof GramJs.UpdateDraftMessage) { - const { replyingToId, formattedText } = buildMessageDraft(update.draft) || {}; onUpdate({ '@type': 'draftMessage', chatId: getApiChatIdFromMtpPeer(update.peer), - formattedText, - replyingToId, + ...buildMessageDraft(update.draft), }); } else if (update instanceof GramJs.UpdateContactsReset) { onUpdate({ '@type': 'updateResetContactList' }); diff --git a/src/api/types/chats.ts b/src/api/types/chats.ts index 932ff04f5..b70f696dc 100644 --- a/src/api/types/chats.ts +++ b/src/api/types/chats.ts @@ -29,7 +29,7 @@ export interface ApiChat { joinDate?: number; isSupport?: boolean; photos?: ApiPhoto[]; - hasDraft?: boolean; + draftDate?: number; // Calls isCallActive?: boolean; diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index 182faeb08..a06889b2e 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -268,6 +268,7 @@ export type ApiUpdateDraftMessage = { '@type': 'draftMessage'; chatId: number; formattedText?: ApiFormattedText; + date?: number; replyingToId?: number; }; diff --git a/src/modules/actions/api/messages.ts b/src/modules/actions/api/messages.ts index 7cf445ae6..0ae38016e 100644 --- a/src/modules/actions/api/messages.ts +++ b/src/modules/actions/api/messages.ts @@ -300,7 +300,7 @@ addReducer('saveDraft', (global, actions, payload) => { } global = replaceThreadParam(global, chatId, threadId, 'draft', draft); - global = updateChat(global, chatId, { hasDraft: true }); + global = updateChat(global, chatId, { draftDate: Math.round(Date.now() / 1000) }); return global; }); @@ -318,7 +318,7 @@ addReducer('clearDraft', (global, actions, payload) => { } global = replaceThreadParam(global, chatId, threadId, 'draft', undefined); - global = updateChat(global, chatId, { hasDraft: false }); + global = updateChat(global, chatId, { draftDate: undefined }); return global; }); diff --git a/src/modules/actions/api/sync.ts b/src/modules/actions/api/sync.ts index 19dc02d38..fbde9a2fe 100644 --- a/src/modules/actions/api/sync.ts +++ b/src/modules/actions/api/sync.ts @@ -21,7 +21,6 @@ import { updateChatListSecondaryInfo, updateThreadInfos, replaceThreadParam, - updateChat, } from '../../reducers'; import { selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectThreadInfo, @@ -143,10 +142,7 @@ async function loadAndReplaceChats() { global = updateChatListSecondaryInfo(global, 'active', result); Object.keys(result.draftsById).map(Number).forEach((chatId) => { - global = replaceThreadParam( - global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId], - ); - global = updateChat(global, chatId, { hasDraft: Boolean(result.draftsById[chatId]) }); + global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId]); }); Object.keys(result.replyingToById).map(Number).forEach((chatId) => { diff --git a/src/modules/actions/apiUpdaters/chats.ts b/src/modules/actions/apiUpdaters/chats.ts index 3d099ef9b..7a613a70c 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -363,13 +363,15 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { } case 'draftMessage': { - const { chatId, formattedText, replyingToId } = update; + const { + chatId, formattedText, date, replyingToId, + } = update; const chat = global.chats.byId[chatId]; if (chat) { global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'draft', formattedText); global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'replyingToId', replyingToId); - global = updateChat(global, chatId, { hasDraft: Boolean(formattedText) }); + global = updateChat(global, chatId, { draftDate: date }); setGlobal(global); } diff --git a/src/modules/helpers/chats.ts b/src/modules/helpers/chats.ts index a3b6ce99a..c2a8ec993 100644 --- a/src/modules/helpers/chats.ts +++ b/src/modules/helpers/chats.ts @@ -196,9 +196,8 @@ export function getChatSlowModeOptions(chat?: ApiChat) { export function getChatOrder(chat: ApiChat) { return Math.max( chat.joinDate || 0, + chat.draftDate || 0, chat.lastMessage ? chat.lastMessage.date : 0, - ) + ( - chat.hasDraft ? Date.now() / 1000 : 0 ); }