From 5ed545aa0b5850a1b6bd8a56b094b801781f4a82 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 24 Sep 2021 17:05:41 +0300 Subject: [PATCH] Chat List: Fix missing archived chats in folders --- src/components/left/main/ChatList.tsx | 9 +++++++-- src/global/types.ts | 3 ++- src/modules/actions/api/chats.ts | 29 ++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/components/left/main/ChatList.tsx b/src/components/left/main/ChatList.tsx index 0ee8296d..7f153aef 100644 --- a/src/components/left/main/ChatList.tsx +++ b/src/components/left/main/ChatList.tsx @@ -45,7 +45,9 @@ type StateProps = { notifyExceptions?: Record; }; -type DispatchProps = Pick; +type DispatchProps = Pick; enum FolderTypeToListType { 'all' = 'active', @@ -68,6 +70,7 @@ const ChatList: FC = ({ onScreenSelect, loadMoreChats, preloadTopChatMessages, + preloadArchivedChats, openChat, openNextChat, }) => { @@ -122,8 +125,9 @@ const ChatList: FC = ({ useEffect(() => { if (lastSyncTime && folderType === 'all') { preloadTopChatMessages(); + preloadArchivedChats(); } - }, [lastSyncTime, folderType, preloadTopChatMessages]); + }, [lastSyncTime, folderType, preloadTopChatMessages, preloadArchivedChats]); const getAnimationType = useChatAnimationType(orderDiffById); @@ -255,6 +259,7 @@ export default memo(withGlobal( (setGlobal, actions): DispatchProps => pick(actions, [ 'loadMoreChats', 'preloadTopChatMessages', + 'preloadArchivedChats', 'openChat', 'openNextChat', ]), diff --git a/src/global/types.ts b/src/global/types.ts index dd9e0394..947de12f 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -452,7 +452,8 @@ export type ActionTypes = ( 'setAuthPhoneNumber' | 'setAuthCode' | 'setAuthPassword' | 'signUp' | 'returnToAuthPhoneNumber' | 'signOut' | 'setAuthRememberMe' | 'clearAuthError' | 'uploadProfilePhoto' | 'goToAuthQrCode' | 'clearCache' | // chats - 'preloadTopChatMessages' | 'loadChats' | 'loadMoreChats' | 'openChat' | 'openChatWithInfo' | 'openLinkedChat' | + 'preloadTopChatMessages' | 'preloadArchivedChats' | 'loadChats' | 'loadMoreChats' | 'openChat' | + 'openChatWithInfo' | 'openLinkedChat' | 'openSupportChat' | 'openTipsChat' | 'focusMessageInComments' | 'loadFullChat' | 'loadTopChats' | 'requestChatUpdate' | 'updateChatMutedState' | 'joinChannel' | 'leaveChannel' | 'deleteChannel' | 'toggleChatPinned' | 'toggleChatArchived' | 'toggleChatUnread' | diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index db9adb79..56c93272 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -47,11 +47,12 @@ import { } from '../../helpers'; import { processDeepLink } from '../../../util/deeplink'; -const TOP_CHATS_PRELOAD_PAUSE = 100; +const TOP_CHAT_MESSAGES_PRELOAD_INTERVAL = 100; +const CHATS_PRELOAD_INTERVAL = 300; // We expect this ID does not exist const TMP_CHAT_ID = -1; -const runThrottledForLoadChats = throttle((cb) => cb(), 1000, true); +const runThrottledForLoadChats = throttle((cb) => cb(), CHATS_PRELOAD_INTERVAL, true); const runThrottledForLoadTopChats = throttle((cb) => cb(), 3000, true); const runDebouncedForLoadFullChat = debounce((cb) => cb(), 500, false, true); @@ -60,7 +61,7 @@ addReducer('preloadTopChatMessages', (global, actions) => { const preloadedChatIds: number[] = []; for (let i = 0; i < TOP_CHAT_MESSAGES_PRELOAD_LIMIT; i++) { - await pause(TOP_CHATS_PRELOAD_PAUSE); + await pause(TOP_CHAT_MESSAGES_PRELOAD_INTERVAL); const { byId, @@ -207,6 +208,24 @@ addReducer('loadMoreChats', (global, actions, payload) => { } }); +addReducer('preloadArchivedChats', () => { + (async () => { + while (!getGlobal().chats.isFullyLoaded.archived) { + const currentGlobal = getGlobal(); + const listIds = currentGlobal.chats.listIds.archived; + const oldestChat = listIds + ? listIds + .map((id) => currentGlobal.chats.byId[id]) + .filter((chat) => Boolean(chat?.lastMessage) && !selectIsChatPinned(currentGlobal, chat.id)) + .sort((chat1, chat2) => (chat1.lastMessage!.date - chat2.lastMessage!.date))[0] + : undefined; + + await loadChats('archived', oldestChat?.id, oldestChat?.lastMessage!.date); + await pause(CHATS_PRELOAD_INTERVAL); + } + })(); +}); + addReducer('loadFullChat', (global, actions, payload) => { const { chatId, force } = payload!; const chat = selectChat(global, chatId); @@ -279,7 +298,7 @@ addReducer('joinChannel', (global, actions, payload) => { addReducer('deleteChatUser', (global, actions, payload) => { (async () => { - const { chatId, userId } : { chatId: number; userId: number } = payload!; + const { chatId, userId }: { chatId: number; userId: number } = payload!; const chat = selectChat(global, chatId); const user = selectUser(global, userId); if (!chat || !user) { @@ -296,7 +315,7 @@ addReducer('deleteChatUser', (global, actions, payload) => { addReducer('deleteChat', (global, actions, payload) => { (async () => { - const { chatId } : { chatId: number } = payload!; + const { chatId }: { chatId: number } = payload!; const chat = selectChat(global, chatId); if (!chat) { return;