Global Search: Use server order (#4752)

This commit is contained in:
zubiden 2024-07-15 15:52:10 +02:00 committed by Alexander Zinchuk
parent 49ccbe3ee9
commit dd3c6bc70f
6 changed files with 57 additions and 80 deletions

View File

@ -360,16 +360,19 @@ export async function searchChats({ query }: { query: string }) {
updateLocalDb(result);
const accountPeerIds = result.myResults.map(getApiChatIdFromMtpPeer);
const allChats = result.chats.concat(result.users)
const globalPeerIds = result.results.map(getApiChatIdFromMtpPeer)
.filter((id) => !accountPeerIds.includes(id));
const chats = result.chats.concat(result.users)
.map((user) => buildApiChatFromPreview(user))
.filter(Boolean);
const allUsers = result.users.map(buildApiUser).filter((user) => Boolean(user) && !user.isSelf) as ApiUser[];
const users = result.users.map(buildApiUser).filter(Boolean);
return {
accountChats: allChats.filter((r) => accountPeerIds.includes(r.id)),
accountUsers: allUsers.filter((u) => accountPeerIds.includes(u.id)),
globalChats: allChats.filter((r) => !accountPeerIds.includes(r.id)),
globalUsers: allUsers.filter((u) => !accountPeerIds.includes(u.id)),
accountResultIds: accountPeerIds,
globalResultIds: globalPeerIds,
chats,
users,
};
}

View File

@ -27,22 +27,22 @@ type StateProps = {
localContactIds?: string[];
searchQuery?: string;
isSearching?: boolean;
localUserIds?: string[];
globalUserIds?: string[];
localPeerIds?: string[];
globalPeerIds?: string[];
};
const NewChatStep1: FC<OwnProps & StateProps> = ({
isChannel,
isActive,
selectedMemberIds,
onSelectedMemberIdsChange,
onNextStep,
onReset,
localContactIds,
searchQuery,
isSearching,
localUserIds,
globalUserIds,
localPeerIds,
globalPeerIds,
onSelectedMemberIdsChange,
onNextStep,
onReset,
}) => {
const {
setGlobalSearchQuery,
@ -67,20 +67,17 @@ const NewChatStep1: FC<OwnProps & StateProps> = ({
return sortChatIds(
unique([
...foundContactIds,
...(localUserIds || []),
...(globalUserIds || []),
...(localPeerIds || []),
...(globalPeerIds || []),
]).filter((contactId) => {
const user = usersById[contactId];
if (!user) {
return true;
}
return !user.isSelf && (user.canBeInvitedToGroup || !isUserBot(user));
return user && !user.isSelf && (user.canBeInvitedToGroup || !isUserBot(user));
}),
false,
selectedMemberIds,
);
}, [localContactIds, searchQuery, localUserIds, globalUserIds, selectedMemberIds]);
}, [localContactIds, searchQuery, localPeerIds, globalPeerIds, selectedMemberIds]);
const handleNextStep = useCallback(() => {
setGlobalSearchQuery({ query: '' });
@ -136,15 +133,15 @@ export default memo(withGlobal<OwnProps>(
globalResults,
localResults,
} = selectTabState(global).globalSearch;
const { userIds: globalUserIds } = globalResults || {};
const { userIds: localUserIds } = localResults || {};
const { peerIds: globalPeerIds } = globalResults || {};
const { peerIds: localPeerIds } = localResults || {};
return {
localContactIds,
searchQuery,
isSearching: fetchingStatus?.chats,
globalUserIds,
localUserIds,
globalPeerIds,
localPeerIds,
};
},
)(NewChatStep1));

View File

@ -47,10 +47,8 @@ export type OwnProps = {
type StateProps = {
currentUserId?: string;
contactIds?: string[];
accountChatIds?: string[];
accountUserIds?: string[];
globalChatIds?: string[];
globalUserIds?: string[];
accountPeerIds?: string[];
globalPeerIds?: string[];
foundIds?: string[];
globalMessagesByChatId?: Record<string, { byId: Record<number, ApiMessage> }>;
fetchingStatus?: { chats?: boolean; messages?: boolean };
@ -69,10 +67,8 @@ const ChatResults: FC<OwnProps & StateProps> = ({
dateSearchQuery,
currentUserId,
contactIds,
accountChatIds,
accountUserIds,
globalChatIds,
globalUserIds,
accountPeerIds,
globalPeerIds,
foundIds,
globalMessagesByChatId,
fetchingStatus,
@ -155,41 +151,35 @@ const ChatResults: FC<OwnProps & StateProps> = ({
contactIdsWithMe, usersById, searchQuery, currentUserId, lang('SavedMessages'),
);
const localPeerIds = unique([
const localPeerIds = [
...localContactIds,
...localChatIds,
]);
const accountPeerIds = unique([
...(accountChatIds ?? []),
...(accountUserIds ?? []),
].filter((accountPeerId) => !localPeerIds.includes(accountPeerId)));
return [
...sortChatIds(localPeerIds, undefined, currentUserId ? [currentUserId] : undefined),
...sortChatIds(accountPeerIds),
];
}, [searchQuery, lang, currentUserId, contactIds, accountChatIds, accountUserIds, isChannelList]);
return unique([
...sortChatIds(localPeerIds, undefined, currentUserId ? [currentUserId] : undefined),
...sortChatIds(accountPeerIds || []),
]);
}, [searchQuery, lang, currentUserId, contactIds, accountPeerIds, isChannelList]);
useHorizontalScroll(chatSelectionRef, !localResults.length || isChannelList, true);
const globalResults = useMemo(() => {
if (!searchQuery || searchQuery.length < MIN_QUERY_LENGTH_FOR_GLOBAL_SEARCH || !globalChatIds || !globalUserIds) {
if (!searchQuery || searchQuery.length < MIN_QUERY_LENGTH_FOR_GLOBAL_SEARCH || !globalPeerIds) {
return MEMO_EMPTY_ARRAY;
}
// No need for expensive global updates, so we avoid them
const chatsById = getGlobal().chats.byId;
const ids = unique([...globalChatIds, ...globalUserIds]);
const filteredIds = ids.filter((id) => {
const filteredIds = globalPeerIds.filter((id) => {
if (!isChannelList) return true;
const chat = chatsById[id];
return chat && isChatChannel(chat);
});
return sortChatIds(filteredIds, true);
}, [globalChatIds, globalUserIds, isChannelList, searchQuery]);
}, [globalPeerIds, isChannelList, searchQuery]);
const foundMessages = useMemo(() => {
if ((!searchQuery && !searchDate) || !foundIds || foundIds.length === 0) {
@ -372,8 +362,8 @@ export default memo(withGlobal<OwnProps>(
const {
fetchingStatus, globalResults, localResults, resultsByType,
} = selectTabState(global).globalSearch;
const { chatIds: globalChatIds, userIds: globalUserIds } = globalResults || {};
const { chatIds: accountChatIds, userIds: accountUserIds } = localResults || {};
const { peerIds: globalPeerIds } = globalResults || {};
const { peerIds: accountPeerIds } = localResults || {};
const { byChatId: globalMessagesByChatId } = messages;
const foundIds = resultsByType?.[isChannelList ? 'channels' : 'text']?.foundIds;
const { similarChannelIds } = selectSimilarChannelIds(global, GLOBAL_SUGGESTED_CHANNELS_ID) || {};
@ -381,10 +371,8 @@ export default memo(withGlobal<OwnProps>(
return {
currentUserId,
contactIds,
accountChatIds,
accountUserIds,
globalChatIds,
globalUserIds,
accountPeerIds,
globalPeerIds,
foundIds,
globalMessagesByChatId,
fetchingStatus,

View File

@ -44,27 +44,21 @@ addActionHandler('setGlobalSearchQuery', (global, actions, payload): ActionRetur
}
const {
accountChats, accountUsers, globalChats, globalUsers,
accountResultIds, globalResultIds, users, chats,
} = result;
if (accountChats.length || globalChats.length) {
global = addChats(global, buildCollectionByKey([...accountChats, ...globalChats], 'id'));
}
global = addChats(global, buildCollectionByKey(chats, 'id'));
if (accountUsers.length || globalUsers.length) {
global = addUsers(global, buildCollectionByKey([...accountUsers, ...globalUsers], 'id'));
}
global = addUsers(global, buildCollectionByKey(users, 'id'));
global = updateGlobalSearchFetchingStatus(global, { chats: false }, tabId);
global = updateGlobalSearch(global, {
localResults: {
chatIds: accountChats.map(({ id }) => id),
userIds: accountChats.map(({ id }) => id),
peerIds: accountResultIds,
},
globalResults: {
...selectTabState(global, tabId).globalSearch.globalResults,
chatIds: globalChats.map(({ id }) => id),
userIds: globalUsers.map(({ id }) => id),
peerIds: globalResultIds,
},
}, tabId);

View File

@ -318,18 +318,15 @@ addActionHandler('setUserSearchQuery', (global, actions, payload): ActionReturnT
return;
}
const { accountUsers, globalUsers } = result;
const {
users, chats, accountResultIds, globalResultIds,
} = result;
let localUserIds;
let globalUserIds;
if (accountUsers.length) {
global = addUsers(global, buildCollectionByKey(accountUsers, 'id'));
localUserIds = accountUsers.map(({ id }) => id);
}
if (globalUsers.length) {
global = addUsers(global, buildCollectionByKey(globalUsers, 'id'));
globalUserIds = globalUsers.map(({ id }) => id);
}
global = addUsers(global, buildCollectionByKey(users, 'id'));
global = addChats(global, buildCollectionByKey(chats, 'id'));
const localUserIds = accountResultIds.filter(isUserId);
const globalUserIds = globalResultIds.filter(isUserId);
global = updateUserSearchFetchingStatus(global, false, tabId);
global = updateUserSearch(global, { localUserIds, globalUserIds }, tabId);

View File

@ -365,12 +365,10 @@ export type TabState = {
};
isClosing?: boolean;
localResults?: {
chatIds?: string[];
userIds?: string[];
peerIds?: string[];
};
globalResults?: {
chatIds?: string[];
userIds?: string[];
peerIds?: string[];
};
resultsByType?: Partial<Record<ApiGlobalMessageSearchType, {
totalCount?: number;