From 3590cf6f5c6b6f2ec6a44e7e1e620351c5ecf4b0 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 30 Jun 2021 00:22:54 +0300 Subject: [PATCH] Chat List: Prioritize chats with drafts when ordering --- src/api/gramjs/apiBuilders/chats.ts | 3 ++- src/api/types/chats.ts | 1 + src/modules/actions/api/messages.ts | 11 +++++++++-- src/modules/actions/api/sync.ts | 2 ++ src/modules/actions/apiUpdaters/chats.ts | 1 + src/modules/helpers/chats.ts | 8 +++++++- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/api/gramjs/apiBuilders/chats.ts b/src/api/gramjs/apiBuilders/chats.ts index 3a214b54..f7693b8d 100644 --- a/src/api/gramjs/apiBuilders/chats.ts +++ b/src/api/gramjs/apiBuilders/chats.ts @@ -61,7 +61,7 @@ export function buildApiChatFromDialog( ): ApiChat { const { peer, folderId, unreadMark, unreadCount, unreadMentionsCount, notifySettings: { silent, muteUntil }, - readOutboxMaxId, readInboxMaxId, + readOutboxMaxId, readInboxMaxId, draft, } = dialog; const isMuted = silent || (typeof muteUntil === 'number' && Date.now() + serverTimeOffset * 1000 < muteUntil * 1000); @@ -76,6 +76,7 @@ export function buildApiChatFromDialog( unreadMentionsCount, isMuted, ...(unreadMark && { hasUnreadMark: true }), + ...(draft instanceof GramJs.DraftMessage && { hasDraft: true }), ...buildApiChatFieldsFromPeerEntity(peerEntity), }; } diff --git a/src/api/types/chats.ts b/src/api/types/chats.ts index 534b8267..932ff04f 100644 --- a/src/api/types/chats.ts +++ b/src/api/types/chats.ts @@ -29,6 +29,7 @@ export interface ApiChat { joinDate?: number; isSupport?: boolean; photos?: ApiPhoto[]; + hasDraft?: boolean; // Calls isCallActive?: boolean; diff --git a/src/modules/actions/api/messages.ts b/src/modules/actions/api/messages.ts index d4542a1a..7cf445ae 100644 --- a/src/modules/actions/api/messages.ts +++ b/src/modules/actions/api/messages.ts @@ -29,6 +29,7 @@ import { updateOutlyingIds, replaceScheduledMessages, updateThreadInfos, + updateChat, } from '../../reducers'; import { selectChat, @@ -298,7 +299,10 @@ addReducer('saveDraft', (global, actions, payload) => { }); } - return replaceThreadParam(global, chatId, threadId, 'draft', draft); + global = replaceThreadParam(global, chatId, threadId, 'draft', draft); + global = updateChat(global, chatId, { hasDraft: true }); + + return global; }); addReducer('clearDraft', (global, actions, payload) => { @@ -313,7 +317,10 @@ addReducer('clearDraft', (global, actions, payload) => { void callApi('clearDraft', chat); } - return replaceThreadParam(global, chatId, threadId, 'draft', undefined); + global = replaceThreadParam(global, chatId, threadId, 'draft', undefined); + global = updateChat(global, chatId, { hasDraft: false }); + + return global; }); addReducer('toggleMessageWebPage', (global, actions, payload) => { diff --git a/src/modules/actions/api/sync.ts b/src/modules/actions/api/sync.ts index 1341cf2c..19dc02d3 100644 --- a/src/modules/actions/api/sync.ts +++ b/src/modules/actions/api/sync.ts @@ -21,6 +21,7 @@ import { updateChatListSecondaryInfo, updateThreadInfos, replaceThreadParam, + updateChat, } from '../../reducers'; import { selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectThreadInfo, @@ -145,6 +146,7 @@ async function loadAndReplaceChats() { global = replaceThreadParam( global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId], ); + global = updateChat(global, chatId, { hasDraft: Boolean(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 dc51d23a..a1d6dde4 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -369,6 +369,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { 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) }); setGlobal(global); } diff --git a/src/modules/helpers/chats.ts b/src/modules/helpers/chats.ts index a67b7b50..a3b6ce99 100644 --- a/src/modules/helpers/chats.ts +++ b/src/modules/helpers/chats.ts @@ -192,8 +192,14 @@ export function getChatSlowModeOptions(chat?: ApiChat) { return chat.fullInfo.slowMode; } + export function getChatOrder(chat: ApiChat) { - return Math.max(chat.joinDate || 0, chat.lastMessage ? chat.lastMessage.date : 0); + return Math.max( + chat.joinDate || 0, + chat.lastMessage ? chat.lastMessage.date : 0, + ) + ( + chat.hasDraft ? Date.now() / 1000 : 0 + ); } export function isChatArchived(chat: ApiChat) {