[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;
localUserIds?: number[];
globalUserIds?: number[];
serverTimeOffset?: number;
};
type DispatchProps = Pick<GlobalActions, 'loadContactList' | 'setGlobalSearchQuery'>;
@ -54,7 +53,6 @@ const NewChatStep1: FC<OwnProps & StateProps & DispatchProps> = ({
isSearching,
localUserIds,
globalUserIds,
serverTimeOffset,
loadContactList,
setGlobalSearchQuery,
}) => {
@ -72,8 +70,7 @@ const NewChatStep1: FC<OwnProps & StateProps & DispatchProps> = ({
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<OwnProps & StateProps & DispatchProps> = ({
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<OwnProps>(
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<OwnProps>(
isSearching: fetchingStatus && fetchingStatus.chats,
globalUserIds,
localUserIds,
serverTimeOffset,
};
},
(setGlobal, actions): DispatchProps => pick(actions, ['loadContactList', 'setGlobalSearchQuery']),

View File

@ -45,7 +45,6 @@ type StateProps = {
usersById: Record<number, ApiUser>;
fetchingStatus?: { chats?: boolean; messages?: boolean };
lastSyncTime?: number;
serverTimeOffset?: number;
};
type DispatchProps = Pick<GlobalActions, (
@ -62,7 +61,6 @@ const ChatResults: FC<OwnProps & StateProps & DispatchProps> = ({
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<OwnProps & StateProps & DispatchProps> = ({
...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<OwnProps & StateProps & DispatchProps> = ({
}
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<OwnProps>(
}
const {
currentUserId, messages, lastSyncTime, serverTimeOffset,
currentUserId, messages, lastSyncTime,
} = global;
const {
fetchingStatus, globalResults, localResults, resultsByType,
@ -318,7 +315,6 @@ export default memo(withGlobal<OwnProps>(
usersById,
fetchingStatus,
lastSyncTime,
serverTimeOffset,
};
},
(setGlobal, actions): DispatchProps => pick(actions, [

View File

@ -38,7 +38,6 @@ type StateProps = {
archivedListIds?: number[];
orderedPinnedIds?: number[];
currentUserId?: number;
serverTimeOffset: number;
};
type DispatchProps = Pick<GlobalActions, 'setForwardChatId' | 'exitForwardMode' | 'loadMoreChats'>;
@ -53,7 +52,6 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
activeListIds,
archivedListIds,
currentUserId,
serverTimeOffset,
isOpen,
setForwardChatId,
exitForwardMode,
@ -115,8 +113,8 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
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<OwnProps & StateProps & DispatchProps> = ({
export default memo(withGlobal<OwnProps>(
(global): StateProps => {
const {
serverTimeOffset,
chats: {
byId: chatsById,
listIds,
@ -210,7 +207,6 @@ export default memo(withGlobal<OwnProps>(
activeListIds: listIds.active,
archivedListIds: listIds.archived,
currentUserId,
serverTimeOffset,
};
},
(setGlobal, actions): DispatchProps => pick(actions, ['setForwardChatId', 'exitForwardMode', 'loadMoreChats']),

View File

@ -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<number, ApiChat>,
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;