Chat List: Fix a case causing infinite loop when loading chats

This commit is contained in:
Alexander Zinchuk 2022-09-16 18:28:49 +02:00
parent 8979f83542
commit 4b4b9281a7
2 changed files with 7 additions and 7 deletions

View File

@ -170,6 +170,10 @@ addActionHandler('loadAllChats', async (global, actions, payload) => {
let { shouldReplace } = payload;
let i = 0;
const getOrderDate = (chat: ApiChat) => {
return chat.lastMessage?.date || chat.joinDate;
};
while (shouldReplace || !getGlobal().chats.isFullyLoaded[listType]) {
if (i++ >= INFINITE_LOOP_MARKER) {
if (DEBUG) {
@ -192,15 +196,15 @@ addActionHandler('loadAllChats', async (global, actions, payload) => {
/* eslint-disable @typescript-eslint/no-loop-func */
.map((id) => global.chats.byId[id])
.filter((chat) => (
Boolean(chat?.lastMessage)
Boolean(chat && getOrderDate(chat))
&& chat.id !== SERVICE_NOTIFICATIONS_USER_ID
&& !selectIsChatPinned(global, chat.id)
))
/* eslint-enable @typescript-eslint/no-loop-func */
.sort((chat1, chat2) => (chat1.lastMessage!.date - chat2.lastMessage!.date))[0]
.sort((chat1, chat2) => getOrderDate(chat1)! - getOrderDate(chat2)!)[0]
: undefined;
await loadChats(listType, oldestChat?.id, oldestChat?.lastMessage!.date, shouldReplace);
await loadChats(listType, oldestChat?.id, oldestChat ? getOrderDate(oldestChat) : undefined, shouldReplace);
if (shouldReplace) {
onReplace?.();

View File

@ -235,10 +235,6 @@ export function getChatSlowModeOptions(chat?: ApiChat) {
return chat.fullInfo.slowMode;
}
export function getChatOrder(chat: ApiChat) {
return Math.max(chat.joinDate || 0, chat.draftDate || 0, chat.lastMessage?.date || 0);
}
export function isChatArchived(chat: ApiChat) {
return chat.folderId === ARCHIVED_FOLDER_ID;
}