diff --git a/lib/shared/search-utils.js b/lib/shared/search-utils.js --- a/lib/shared/search-utils.js +++ b/lib/shared/search-utils.js @@ -2,6 +2,7 @@ import * as React from 'react'; +import { messageID } from './message-utils.js'; import SearchIndex from './search-index.js'; import { userIsMember, @@ -20,10 +21,8 @@ import type { ChatMessageInfoItem, MessageListData, - ChatMessageItem, } from '../selectors/chat-selectors.js'; import type { MessageInfo, RawMessageInfo } from '../types/message-types.js'; -import { isComposableMessageType } from '../types/message-types.js'; import type { MinimallyEncodedThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; import { userRelationshipStatus } from '../types/relationship-types.js'; import { threadPermissions } from '../types/thread-permission-types.js'; @@ -137,12 +136,19 @@ let userResults = values(results); if (text === '') { - userResults = userResults.filter(userInfo => - containingThreadInfo - ? userInfo.isMemberOfContainingThread && - !blockedRelationshipsStatuses.has(userInfo.relationshipStatus) - : userInfo?.relationshipStatus === userRelationshipStatus.FRIEND, - ); + userResults = userResults.filter(userInfo => { + if (!containingThreadInfo) { + return userInfo.relationshipStatus === userRelationshipStatus.FRIEND; + } + if (!userInfo.isMemberOfContainingThread) { + return false; + } + const { relationshipStatus } = userInfo; + if (!relationshipStatus) { + return true; + } + return !blockedRelationshipsStatuses.has(relationshipStatus); + }); } const nonFriends = []; @@ -152,8 +158,11 @@ const parentThreadMembers = []; for (const userResult of userResults) { - const relationshipStatus = userResult.relationshipStatus; - if (blockedRelationshipsStatuses.has(relationshipStatus)) { + const { relationshipStatus } = userResult; + if ( + relationshipStatus && + blockedRelationshipsStatuses.has(relationshipStatus) + ) { blockedUsers.push(userResult); } else if (userResult.isMemberOfParentThread) { parentThreadMembers.push(userResult); @@ -181,7 +190,10 @@ }) => { let notice, alert; const username = result.username; - if (blockedRelationshipsStatuses.has(relationshipStatus)) { + if ( + relationshipStatus && + blockedRelationshipsStatuses.has(relationshipStatus) + ) { notice = 'user is blocked'; alert = { title: 'User is blocked', @@ -312,31 +324,28 @@ return null; } - const idSet = new Set(translatedSearchResults.map(item => item.id)); - - const chatMessageInfoItems = chatMessageInfos.filter( - item => - item.messageInfo && - idSet.has(item.messageInfo.id) && - isComposableMessageType(item.messageInfo.type), - ); + const idSet = new Set(translatedSearchResults.map(messageID)); - const uniqueChatMessageInfoItemsMap = new Map(); - chatMessageInfoItems.forEach( - item => - item.messageInfo && - item.messageInfo.id && - uniqueChatMessageInfoItemsMap.set(item.messageInfo.id, item), - ); + const uniqueChatMessageInfoItemsMap = new Map(); + for (const item of chatMessageInfos) { + if (item.itemType !== 'message' || item.messageInfoType !== 'composable') { + continue; + } + const id = messageID(item.messageInfo); + if (idSet.has(id)) { + uniqueChatMessageInfoItemsMap.set(id, item); + } + } - const sortedChatMessageInfoItems = []; + const sortedChatMessageInfoItems: ChatMessageInfoItem[] = []; for (let i = 0; i < translatedSearchResults.length; i++) { - sortedChatMessageInfoItems.push( - uniqueChatMessageInfoItemsMap.get(translatedSearchResults[i].id), - ); + const id = messageID(translatedSearchResults[i]); + const match = uniqueChatMessageInfoItemsMap.get(id); + if (match) { + sortedChatMessageInfoItems.push(match); + } } - - return sortedChatMessageInfoItems.filter(Boolean); + return sortedChatMessageInfoItems; } export {