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 @@ -32,6 +32,9 @@ ? inputParentThreadInfo : null; + const containingThreadInfo = + threadType === threadTypes.SIDEBAR ? parentThreadInfo : communityThreadInfo; + let results = []; const appendUserInfo = (userInfo: AccountUserInfo) => { const { id } = userInfo; @@ -51,6 +54,7 @@ results.push({ ...userInfo, isMemberOfParentThread: userIsMember(parentThreadInfo, id), + isMemberOfContainingThread: userIsMember(containingThreadInfo, id), }); }; if (text === '') { @@ -64,75 +68,87 @@ } } + 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 + ({ + isMemberOfContainingThread, + isMemberOfParentThread, + relationshipStatus, + ...result + }) => { + let notice, alertText, alertTitle; + const username = result.username; + if (blockedRelationshipsStatuses.has(relationshipStatus)) { + notice = 'user is blocked'; + alertTitle = 'User is blocked'; + alertText = + `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 (!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 ) { return { ...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 + !containingThreadInfo && + relationshipStatus !== userRelationshipStatus.FRIEND ) { - notice = 'you’ve blocked this user'; - alertTitle = 'Not a friend'; - 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 ' + - 'in the Profile tab.'; - } else if (relationshipStatus !== userRelationshipStatus.FRIEND) { notice = notFriendNotice; + alertTitle = 'not friend'; 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) { - notice = 'not in parent chat'; } return { ...result, notice, alertText, alertTitle }; },