[Refactoring] Get rid of server offset in chat sorting (#1243)

This commit is contained in:
Alexander Zinchuk 2021-07-07 15:37:50 +03:00
parent f639ca00b5
commit 339c747784
4 changed files with 15 additions and 29 deletions

View File

@ -33,7 +33,6 @@ type StateProps = {
isSearching?: boolean; isSearching?: boolean;
localUserIds?: number[]; localUserIds?: number[];
globalUserIds?: number[]; globalUserIds?: number[];
serverTimeOffset?: number;
}; };
type DispatchProps = Pick<GlobalActions, 'loadContactList' | 'setGlobalSearchQuery'>; type DispatchProps = Pick<GlobalActions, 'loadContactList' | 'setGlobalSearchQuery'>;
@ -54,7 +53,6 @@ const NewChatStep1: FC<OwnProps & StateProps & DispatchProps> = ({
isSearching, isSearching,
localUserIds, localUserIds,
globalUserIds, globalUserIds,
serverTimeOffset,
loadContactList, loadContactList,
setGlobalSearchQuery, setGlobalSearchQuery,
}) => { }) => {
@ -72,8 +70,7 @@ const NewChatStep1: FC<OwnProps & StateProps & DispatchProps> = ({
const displayedIds = useMemo(() => { const displayedIds = useMemo(() => {
const contactIds = localContactIds const contactIds = localContactIds
? sortChatIds(localContactIds.filter((id) => id !== currentUserId), chatsById, ? sortChatIds(localContactIds.filter((id) => id !== currentUserId), chatsById)
undefined, undefined, serverTimeOffset)
: []; : [];
if (!searchQuery) { if (!searchQuery) {
@ -98,10 +95,9 @@ const NewChatStep1: FC<OwnProps & StateProps & DispatchProps> = ({
chatsById, chatsById,
false, false,
selectedMemberIds, selectedMemberIds,
serverTimeOffset,
); );
}, [ }, [
localContactIds, chatsById, serverTimeOffset, searchQuery, localUserIds, globalUserIds, selectedMemberIds, localContactIds, chatsById, searchQuery, localUserIds, globalUserIds, selectedMemberIds,
currentUserId, usersById, currentUserId, usersById,
]); ]);
@ -157,7 +153,7 @@ export default memo(withGlobal<OwnProps>(
const { userIds: localContactIds } = global.contactList || {}; const { userIds: localContactIds } = global.contactList || {};
const { byId: usersById } = global.users; const { byId: usersById } = global.users;
const { byId: chatsById } = global.chats; const { byId: chatsById } = global.chats;
const { currentUserId, serverTimeOffset } = global; const { currentUserId } = global;
const { const {
query: searchQuery, query: searchQuery,
@ -177,7 +173,6 @@ export default memo(withGlobal<OwnProps>(
isSearching: fetchingStatus && fetchingStatus.chats, isSearching: fetchingStatus && fetchingStatus.chats,
globalUserIds, globalUserIds,
localUserIds, localUserIds,
serverTimeOffset,
}; };
}, },
(setGlobal, actions): DispatchProps => pick(actions, ['loadContactList', 'setGlobalSearchQuery']), (setGlobal, actions): DispatchProps => pick(actions, ['loadContactList', 'setGlobalSearchQuery']),

View File

@ -45,7 +45,6 @@ type StateProps = {
usersById: Record<number, ApiUser>; usersById: Record<number, ApiUser>;
fetchingStatus?: { chats?: boolean; messages?: boolean }; fetchingStatus?: { chats?: boolean; messages?: boolean };
lastSyncTime?: number; lastSyncTime?: number;
serverTimeOffset?: number;
}; };
type DispatchProps = Pick<GlobalActions, ( type DispatchProps = Pick<GlobalActions, (
@ -62,7 +61,6 @@ const ChatResults: FC<OwnProps & StateProps & DispatchProps> = ({
localContactIds, localChatIds, localUserIds, globalChatIds, globalUserIds, localContactIds, localChatIds, localUserIds, globalChatIds, globalUserIds,
foundIds, globalMessagesByChatId, chatsById, usersById, fetchingStatus, lastSyncTime, foundIds, globalMessagesByChatId, chatsById, usersById, fetchingStatus, lastSyncTime,
onReset, onSearchDateSelect, openChat, addRecentlyFoundChatId, searchMessagesGlobal, setGlobalSearchChatId, onReset, onSearchDateSelect, openChat, addRecentlyFoundChatId, searchMessagesGlobal, setGlobalSearchChatId,
serverTimeOffset,
}) => { }) => {
const lang = useLang(); const lang = useLang();
@ -122,11 +120,10 @@ const ChatResults: FC<OwnProps & StateProps & DispatchProps> = ({
...foundContactIds, ...foundContactIds,
...(localChatIds || []), ...(localChatIds || []),
...(localUserIds || []), ...(localUserIds || []),
]), chatsById, undefined, undefined, serverTimeOffset), ]), chatsById),
]; ];
}, [ }, [
searchQuery, localContactIds, currentUserId, lang, localChatIds, localUserIds, chatsById, searchQuery, localContactIds, currentUserId, lang, localChatIds, localUserIds, chatsById, usersById,
serverTimeOffset, usersById,
]); ]);
const globalResults = useMemo(() => { const globalResults = useMemo(() => {
@ -135,8 +132,8 @@ const ChatResults: FC<OwnProps & StateProps & DispatchProps> = ({
} }
return sortChatIds(unique([...globalChatIds, ...globalUserIds]), return sortChatIds(unique([...globalChatIds, ...globalUserIds]),
chatsById, true, undefined, serverTimeOffset); chatsById, true);
}, [chatsById, globalChatIds, globalUserIds, searchQuery, serverTimeOffset]); }, [chatsById, globalChatIds, globalUserIds, searchQuery]);
const foundMessages = useMemo(() => { const foundMessages = useMemo(() => {
if ((!searchQuery && !searchDate) || !foundIds || foundIds.length === 0) { if ((!searchQuery && !searchDate) || !foundIds || foundIds.length === 0) {
@ -295,7 +292,7 @@ export default memo(withGlobal<OwnProps>(
} }
const { const {
currentUserId, messages, lastSyncTime, serverTimeOffset, currentUserId, messages, lastSyncTime,
} = global; } = global;
const { const {
fetchingStatus, globalResults, localResults, resultsByType, fetchingStatus, globalResults, localResults, resultsByType,
@ -318,7 +315,6 @@ export default memo(withGlobal<OwnProps>(
usersById, usersById,
fetchingStatus, fetchingStatus,
lastSyncTime, lastSyncTime,
serverTimeOffset,
}; };
}, },
(setGlobal, actions): DispatchProps => pick(actions, [ (setGlobal, actions): DispatchProps => pick(actions, [

View File

@ -38,7 +38,6 @@ type StateProps = {
archivedListIds?: number[]; archivedListIds?: number[];
orderedPinnedIds?: number[]; orderedPinnedIds?: number[];
currentUserId?: number; currentUserId?: number;
serverTimeOffset: number;
}; };
type DispatchProps = Pick<GlobalActions, 'setForwardChatId' | 'exitForwardMode' | 'loadMoreChats'>; type DispatchProps = Pick<GlobalActions, 'setForwardChatId' | 'exitForwardMode' | 'loadMoreChats'>;
@ -53,7 +52,6 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
activeListIds, activeListIds,
archivedListIds, archivedListIds,
currentUserId, currentUserId,
serverTimeOffset,
isOpen, isOpen,
setForwardChatId, setForwardChatId,
exitForwardMode, exitForwardMode,
@ -115,8 +113,8 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter);
}), }),
], chatsById, undefined, priorityIds, serverTimeOffset); ], chatsById, undefined, priorityIds);
}, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds, serverTimeOffset]); }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]);
const [viewportIds, getMore] = useInfiniteScroll(loadMoreChats, chatIds, Boolean(filter)); const [viewportIds, getMore] = useInfiniteScroll(loadMoreChats, chatIds, Boolean(filter));
@ -195,7 +193,6 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
export default memo(withGlobal<OwnProps>( export default memo(withGlobal<OwnProps>(
(global): StateProps => { (global): StateProps => {
const { const {
serverTimeOffset,
chats: { chats: {
byId: chatsById, byId: chatsById,
listIds, listIds,
@ -210,7 +207,6 @@ export default memo(withGlobal<OwnProps>(
activeListIds: listIds.active, activeListIds: listIds.active,
archivedListIds: listIds.archived, archivedListIds: listIds.archived,
currentUserId, currentUserId,
serverTimeOffset,
}; };
}, },
(setGlobal, actions): DispatchProps => pick(actions, ['setForwardChatId', 'exitForwardMode', 'loadMoreChats']), (setGlobal, actions): DispatchProps => pick(actions, ['setForwardChatId', 'exitForwardMode', 'loadMoreChats']),

View File

@ -13,6 +13,9 @@ import { orderBy } from '../../util/iteratees';
import { getUserFirstOrLastName } from './users'; import { getUserFirstOrLastName } from './users';
import { LangFn } from '../../hooks/useLang'; import { LangFn } from '../../hooks/useLang';
const VEIFIED_PRIORITY_BASE = 3e9;
const PINNED_PRIORITY_BASE = 3e8;
export function isChatPrivate(chatId: number) { export function isChatPrivate(chatId: number) {
return chatId > 0; return chatId > 0;
} }
@ -474,7 +477,6 @@ export function sortChatIds(
chatsById: Record<number, ApiChat>, chatsById: Record<number, ApiChat>,
shouldPrioritizeVerified = false, shouldPrioritizeVerified = false,
priorityIds?: number[], priorityIds?: number[],
serverTimeOffset = 0,
) { ) {
return orderBy(chatIds, (id) => { return orderBy(chatIds, (id) => {
const chat = chatsById[id]; const chat = chatsById[id];
@ -489,14 +491,11 @@ export function sortChatIds(
} }
if (shouldPrioritizeVerified && chat.isVerified) { if (shouldPrioritizeVerified && chat.isVerified) {
priority += 3e9; // ~100 years in seconds priority += VEIFIED_PRIORITY_BASE; // ~100 years in seconds
} }
if (priorityIds && priorityIds.includes(id)) { if (priorityIds && priorityIds.includes(id)) {
// Assuming that last message date can't be less than now, priority = Date.now() + PINNED_PRIORITY_BASE + (priorityIds.length - priorityIds.indexOf(id));
// 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;
} }
return priority; return priority;