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); updateLocalDb(result);
const accountPeerIds = result.myResults.map(getApiChatIdFromMtpPeer); 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)) .map((user) => buildApiChatFromPreview(user))
.filter(Boolean); .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 { return {
accountChats: allChats.filter((r) => accountPeerIds.includes(r.id)), accountResultIds: accountPeerIds,
accountUsers: allUsers.filter((u) => accountPeerIds.includes(u.id)), globalResultIds: globalPeerIds,
globalChats: allChats.filter((r) => !accountPeerIds.includes(r.id)), chats,
globalUsers: allUsers.filter((u) => !accountPeerIds.includes(u.id)), users,
}; };
} }

View File

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

View File

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

View File

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

View File

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

View File

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