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 @@ -10,7 +10,11 @@ } from '../types/thread-types'; import type { AccountUserInfo, UserListItem } from '../types/user-types'; import SearchIndex from './search-index'; -import { userIsMember, threadMemberHasPermission } from './thread-utils'; +import { + userIsMember, + threadMemberHasPermission, + getContainingThreadID, +} from './thread-utils'; const notFriendNotice = 'not friend'; @@ -32,6 +36,17 @@ ? inputParentThreadInfo : null; + const containgThreadID = threadType + ? getContainingThreadID(parentThreadInfo, threadType) + : null; + + let containingThreadInfo = null; + if (containgThreadID === parentThreadInfo?.id) { + containingThreadInfo = parentThreadInfo; + } else if (containgThreadID === communityThreadInfo?.id) { + containingThreadInfo = communityThreadInfo; + } + let results = []; const appendUserInfo = (userInfo: AccountUserInfo) => { const { id } = userInfo; @@ -51,6 +66,7 @@ results.push({ ...userInfo, isMemberOfParentThread: userIsMember(parentThreadInfo, id), + isMemberOfContainingThread: userIsMember(containingThreadInfo, id), }); }; if (text === '') { @@ -64,77 +80,91 @@ } } + const blockedRelationshipsStatuses = new Set([ + userRelationshipStatus.BLOCKED_BY_VIEWER, + userRelationshipStatus.BLOCKED_VIEWER, + userRelationshipStatus.BOTH_BLOCKED, + ]); + if (text === '') { results = results.filter(userInfo => - parentThreadInfo - ? userInfo.isMemberOfParentThread && - userInfo.relationshipStatus !== - userRelationshipStatus.BLOCKED_BY_VIEWER + containingThreadInfo + ? userInfo.isMemberOfContainingThread && + !blockedRelationshipsStatuses.has(userInfo.relationshipStatus) : userInfo.relationshipStatus === userRelationshipStatus.FRIEND, ); } const nonFriends = []; const blockedUsers = []; - const friendsAndParentMembers = []; + const friendsAndContainingThreadMembers = []; for (const userResult of results) { const relationshipStatus = userResult.relationshipStatus; if ( - userResult.isMemberOfParentThread && - relationshipStatus !== userRelationshipStatus.BLOCKED_BY_VIEWER + userResult.isMemberOfContainingThread && + !blockedRelationshipsStatuses.has(relationshipStatus) ) { - friendsAndParentMembers.unshift(userResult); + friendsAndContainingThreadMembers.unshift(userResult); } else if (relationshipStatus === userRelationshipStatus.FRIEND) { - friendsAndParentMembers.push(userResult); - } else if ( - relationshipStatus === userRelationshipStatus.BLOCKED_BY_VIEWER - ) { + friendsAndContainingThreadMembers.push(userResult); + } else if (blockedRelationshipsStatuses.has(relationshipStatus)) { blockedUsers.push(userResult); } else { nonFriends.push(userResult); } } - const sortedResults = friendsAndParentMembers + const sortedResults = friendsAndContainingThreadMembers .concat(nonFriends) .concat(blockedUsers); return sortedResults.map( - ({ isMemberOfParentThread, relationshipStatus, ...result }) => { - if ( - isMemberOfParentThread && - relationshipStatus !== userRelationshipStatus.BLOCKED_BY_VIEWER - ) { - return { ...result }; - } + ({ + isMemberOfContainingThread, + isMemberOfParentThread, + relationshipStatus, + ...result + }) => { let notice, alertText, alertTitle; - const userText = result.username; - if (!isMemberOfParentThread && threadType === threadTypes.SIDEBAR) { - notice = 'not in parent chat'; - alertTitle = 'Not in parent chat'; - alertText = 'You can only add members of the parent chat to a thread'; - } else if ( - relationshipStatus === userRelationshipStatus.BLOCKED_BY_VIEWER - ) { - notice = 'you’ve blocked this user'; - alertTitle = 'Not a friend'; + const username = result.username; + if (blockedRelationshipsStatuses.has(relationshipStatus)) { + notice = 'user is blocked'; + alertTitle = 'User is blocked'; alertText = - `Before you add ${userText} to this chat, ` + - 'you’ll need to unblock them and send a friend request. ' + - 'You can do this from the Block List and Friend List ' + + `Before you add ${username} to this chat, ` + + 'you’ll need to unblock them. You can do this from the Block List ' + 'in the Profile tab.'; - } else if (relationshipStatus !== userRelationshipStatus.FRIEND) { + } else if (!isMemberOfContainingThread && containingThreadInfo) { + if (threadType !== threadTypes.SIDEBAR) { + notice = 'not in community'; + alertTitle = 'Not in community'; + alertText = 'You can only add members of the community to this chat'; + } else { + notice = 'not in parent chat'; + alertTitle = 'Not in parent chat'; + alertText = 'You can only add members of the parent chat to a thread'; + } + } else if ( + !containingThreadInfo && + relationshipStatus !== userRelationshipStatus.FRIEND + ) { notice = notFriendNotice; alertTitle = 'Not a friend'; alertText = - `Before you add ${userText} to this chat, ` + + `Before you add ${username} to this chat, ` + 'you’ll need to send them a friend request. ' + 'You can do this from the Friend List in the Profile tab.'; - } else if (parentThreadInfo) { + } else if (!!parentThreadInfo && !isMemberOfParentThread) { notice = 'not in parent chat'; } - return { ...result, notice, alertText, alertTitle }; + if (notice) { + result = { ...result, notice }; + } + if (alertTitle) { + result = { ...result, alertTitle, alertText }; + } + return result; }, ); }