From 339c747784eba24c14d20bc991a0f39e2e187e76 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 7 Jul 2021 15:37:50 +0300 Subject: [PATCH] [Refactoring] Get rid of server offset in chat sorting (#1243) --- src/components/left/newChat/NewChatStep1.tsx | 11 +++-------- src/components/left/search/ChatResults.tsx | 14 +++++--------- src/components/main/ForwardPicker.tsx | 8 ++------ src/modules/helpers/chats.ts | 11 +++++------ 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/components/left/newChat/NewChatStep1.tsx b/src/components/left/newChat/NewChatStep1.tsx index 8a957a694..5dd1a0379 100644 --- a/src/components/left/newChat/NewChatStep1.tsx +++ b/src/components/left/newChat/NewChatStep1.tsx @@ -33,7 +33,6 @@ type StateProps = { isSearching?: boolean; localUserIds?: number[]; globalUserIds?: number[]; - serverTimeOffset?: number; }; type DispatchProps = Pick; @@ -54,7 +53,6 @@ const NewChatStep1: FC = ({ isSearching, localUserIds, globalUserIds, - serverTimeOffset, loadContactList, setGlobalSearchQuery, }) => { @@ -72,8 +70,7 @@ const NewChatStep1: FC = ({ const displayedIds = useMemo(() => { const contactIds = localContactIds - ? sortChatIds(localContactIds.filter((id) => id !== currentUserId), chatsById, - undefined, undefined, serverTimeOffset) + ? sortChatIds(localContactIds.filter((id) => id !== currentUserId), chatsById) : []; if (!searchQuery) { @@ -98,10 +95,9 @@ const NewChatStep1: FC = ({ chatsById, false, selectedMemberIds, - serverTimeOffset, ); }, [ - localContactIds, chatsById, serverTimeOffset, searchQuery, localUserIds, globalUserIds, selectedMemberIds, + localContactIds, chatsById, searchQuery, localUserIds, globalUserIds, selectedMemberIds, currentUserId, usersById, ]); @@ -157,7 +153,7 @@ export default memo(withGlobal( const { userIds: localContactIds } = global.contactList || {}; const { byId: usersById } = global.users; const { byId: chatsById } = global.chats; - const { currentUserId, serverTimeOffset } = global; + const { currentUserId } = global; const { query: searchQuery, @@ -177,7 +173,6 @@ export default memo(withGlobal( isSearching: fetchingStatus && fetchingStatus.chats, globalUserIds, localUserIds, - serverTimeOffset, }; }, (setGlobal, actions): DispatchProps => pick(actions, ['loadContactList', 'setGlobalSearchQuery']), diff --git a/src/components/left/search/ChatResults.tsx b/src/components/left/search/ChatResults.tsx index ce4b6abf5..3e1cc89fd 100644 --- a/src/components/left/search/ChatResults.tsx +++ b/src/components/left/search/ChatResults.tsx @@ -45,7 +45,6 @@ type StateProps = { usersById: Record; fetchingStatus?: { chats?: boolean; messages?: boolean }; lastSyncTime?: number; - serverTimeOffset?: number; }; type DispatchProps = Pick = ({ localContactIds, localChatIds, localUserIds, globalChatIds, globalUserIds, foundIds, globalMessagesByChatId, chatsById, usersById, fetchingStatus, lastSyncTime, onReset, onSearchDateSelect, openChat, addRecentlyFoundChatId, searchMessagesGlobal, setGlobalSearchChatId, - serverTimeOffset, }) => { const lang = useLang(); @@ -122,11 +120,10 @@ const ChatResults: FC = ({ ...foundContactIds, ...(localChatIds || []), ...(localUserIds || []), - ]), chatsById, undefined, undefined, serverTimeOffset), + ]), chatsById), ]; }, [ - searchQuery, localContactIds, currentUserId, lang, localChatIds, localUserIds, chatsById, - serverTimeOffset, usersById, + searchQuery, localContactIds, currentUserId, lang, localChatIds, localUserIds, chatsById, usersById, ]); const globalResults = useMemo(() => { @@ -135,8 +132,8 @@ const ChatResults: FC = ({ } return sortChatIds(unique([...globalChatIds, ...globalUserIds]), - chatsById, true, undefined, serverTimeOffset); - }, [chatsById, globalChatIds, globalUserIds, searchQuery, serverTimeOffset]); + chatsById, true); + }, [chatsById, globalChatIds, globalUserIds, searchQuery]); const foundMessages = useMemo(() => { if ((!searchQuery && !searchDate) || !foundIds || foundIds.length === 0) { @@ -295,7 +292,7 @@ export default memo(withGlobal( } const { - currentUserId, messages, lastSyncTime, serverTimeOffset, + currentUserId, messages, lastSyncTime, } = global; const { fetchingStatus, globalResults, localResults, resultsByType, @@ -318,7 +315,6 @@ export default memo(withGlobal( usersById, fetchingStatus, lastSyncTime, - serverTimeOffset, }; }, (setGlobal, actions): DispatchProps => pick(actions, [ diff --git a/src/components/main/ForwardPicker.tsx b/src/components/main/ForwardPicker.tsx index ea4f13957..20b545d84 100644 --- a/src/components/main/ForwardPicker.tsx +++ b/src/components/main/ForwardPicker.tsx @@ -38,7 +38,6 @@ type StateProps = { archivedListIds?: number[]; orderedPinnedIds?: number[]; currentUserId?: number; - serverTimeOffset: number; }; type DispatchProps = Pick; @@ -53,7 +52,6 @@ const ForwardPicker: FC = ({ activeListIds, archivedListIds, currentUserId, - serverTimeOffset, isOpen, setForwardChatId, exitForwardMode, @@ -115,8 +113,8 @@ const ForwardPicker: FC = ({ return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); }), - ], chatsById, undefined, priorityIds, serverTimeOffset); - }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds, serverTimeOffset]); + ], chatsById, undefined, priorityIds); + }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]); const [viewportIds, getMore] = useInfiniteScroll(loadMoreChats, chatIds, Boolean(filter)); @@ -195,7 +193,6 @@ const ForwardPicker: FC = ({ export default memo(withGlobal( (global): StateProps => { const { - serverTimeOffset, chats: { byId: chatsById, listIds, @@ -210,7 +207,6 @@ export default memo(withGlobal( activeListIds: listIds.active, archivedListIds: listIds.archived, currentUserId, - serverTimeOffset, }; }, (setGlobal, actions): DispatchProps => pick(actions, ['setForwardChatId', 'exitForwardMode', 'loadMoreChats']), diff --git a/src/modules/helpers/chats.ts b/src/modules/helpers/chats.ts index e7151042e..67f40c8f9 100644 --- a/src/modules/helpers/chats.ts +++ b/src/modules/helpers/chats.ts @@ -13,6 +13,9 @@ import { orderBy } from '../../util/iteratees'; import { getUserFirstOrLastName } from './users'; import { LangFn } from '../../hooks/useLang'; +const VEIFIED_PRIORITY_BASE = 3e9; +const PINNED_PRIORITY_BASE = 3e8; + export function isChatPrivate(chatId: number) { return chatId > 0; } @@ -474,7 +477,6 @@ export function sortChatIds( chatsById: Record, shouldPrioritizeVerified = false, priorityIds?: number[], - serverTimeOffset = 0, ) { return orderBy(chatIds, (id) => { const chat = chatsById[id]; @@ -489,14 +491,11 @@ export function sortChatIds( } if (shouldPrioritizeVerified && chat.isVerified) { - priority += 3e9; // ~100 years in seconds + priority += VEIFIED_PRIORITY_BASE; // ~100 years in seconds } if (priorityIds && priorityIds.includes(id)) { - // Assuming that last message date can't be less than now, - // this should place prioritized on top of the list. - // Then we subtract index of `id` in `priorityIds` to preserve selected order - priority += Date.now() + serverTimeOffset * 1000 + (priorityIds.length - priorityIds.indexOf(id)) * 3e8; + priority = Date.now() + PINNED_PRIORITY_BASE + (priorityIds.length - priorityIds.indexOf(id)); } return priority;