Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/search-utils.js
Show All 12 Lines | import { | ||||
searchMessagesActionTypes, | searchMessagesActionTypes, | ||||
} from '../actions/message-actions.js'; | } from '../actions/message-actions.js'; | ||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import type { RawMessageInfo } from '../types/message-types.js'; | import type { RawMessageInfo } from '../types/message-types.js'; | ||||
import { userRelationshipStatus } from '../types/relationship-types.js'; | import { userRelationshipStatus } from '../types/relationship-types.js'; | ||||
import { threadPermissions } from '../types/thread-permission-types.js'; | import { threadPermissions } from '../types/thread-permission-types.js'; | ||||
import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; | import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; | ||||
import { type ThreadInfo } from '../types/thread-types.js'; | import { type ThreadInfo } from '../types/thread-types.js'; | ||||
import type { AccountUserInfo, UserListItem } from '../types/user-types.js'; | import type { | ||||
AccountUserInfo, | |||||
UserListItem, | |||||
GlobalAccountUserInfo, | |||||
} from '../types/user-types.js'; | |||||
import { | import { | ||||
useServerCall, | useServerCall, | ||||
useDispatchActionPromise, | useDispatchActionPromise, | ||||
} from '../utils/action-utils.js'; | } from '../utils/action-utils.js'; | ||||
import { values } from '../utils/objects.js'; | |||||
const notFriendNotice = 'not friend'; | const notFriendNotice = 'not friend'; | ||||
function getPotentialMemberItems( | function getPotentialMemberItems({ | ||||
text: string, | text, | ||||
userInfos: { +[id: string]: AccountUserInfo }, | userInfos, | ||||
searchIndex: SearchIndex, | searchIndex, | ||||
excludeUserIDs: $ReadOnlyArray<string>, | excludeUserIDs, | ||||
inputParentThreadInfo: ?ThreadInfo, | includeServerSearchUsers, | ||||
inputCommunityThreadInfo: ?ThreadInfo, | inputParentThreadInfo, | ||||
threadType: ?ThreadType, | inputCommunityThreadInfo, | ||||
): UserListItem[] { | threadType, | ||||
}: { | |||||
+text: string, | |||||
+userInfos: { +[id: string]: AccountUserInfo }, | |||||
+searchIndex: SearchIndex, | |||||
+excludeUserIDs: $ReadOnlyArray<string>, | |||||
+includeServerSearchUsers?: $ReadOnlyArray<GlobalAccountUserInfo>, | |||||
+inputParentThreadInfo?: ?ThreadInfo, | |||||
+inputCommunityThreadInfo?: ?ThreadInfo, | |||||
+threadType?: ?ThreadType, | |||||
}): UserListItem[] { | |||||
const communityThreadInfo = | const communityThreadInfo = | ||||
inputCommunityThreadInfo && inputCommunityThreadInfo.id !== genesis.id | inputCommunityThreadInfo && inputCommunityThreadInfo.id !== genesis.id | ||||
? inputCommunityThreadInfo | ? inputCommunityThreadInfo | ||||
: null; | : null; | ||||
const parentThreadInfo = | const parentThreadInfo = | ||||
inputParentThreadInfo && inputParentThreadInfo.id !== genesis.id | inputParentThreadInfo && inputParentThreadInfo.id !== genesis.id | ||||
? inputParentThreadInfo | ? inputParentThreadInfo | ||||
: null; | : null; | ||||
const containgThreadID = threadType | const containgThreadID = threadType | ||||
? getContainingThreadID(parentThreadInfo, threadType) | ? getContainingThreadID(parentThreadInfo, threadType) | ||||
: null; | : null; | ||||
let containingThreadInfo = null; | let containingThreadInfo = null; | ||||
if (containgThreadID === parentThreadInfo?.id) { | if (containgThreadID === parentThreadInfo?.id) { | ||||
containingThreadInfo = parentThreadInfo; | containingThreadInfo = parentThreadInfo; | ||||
} else if (containgThreadID === communityThreadInfo?.id) { | } else if (containgThreadID === communityThreadInfo?.id) { | ||||
containingThreadInfo = communityThreadInfo; | containingThreadInfo = communityThreadInfo; | ||||
} | } | ||||
let results = []; | const results: { | ||||
const appendUserInfo = (userInfo: AccountUserInfo) => { | [id: string]: { | ||||
...AccountUserInfo | GlobalAccountUserInfo, | |||||
isMemberOfParentThread: boolean, | |||||
isMemberOfContainingThread: boolean, | |||||
}, | |||||
} = {}; | |||||
const appendUserInfo = ( | |||||
userInfo: AccountUserInfo | GlobalAccountUserInfo, | |||||
) => { | |||||
const { id } = userInfo; | const { id } = userInfo; | ||||
if (excludeUserIDs.includes(id)) { | if (excludeUserIDs.includes(id) || id in results) { | ||||
return; | return; | ||||
} | } | ||||
if ( | if ( | ||||
communityThreadInfo && | communityThreadInfo && | ||||
!threadMemberHasPermission( | !threadMemberHasPermission( | ||||
communityThreadInfo, | communityThreadInfo, | ||||
id, | id, | ||||
threadPermissions.KNOW_OF, | threadPermissions.KNOW_OF, | ||||
) | ) | ||||
) { | ) { | ||||
return; | return; | ||||
} | } | ||||
results.push({ | results[id] = { | ||||
...userInfo, | ...userInfo, | ||||
isMemberOfParentThread: userIsMember(parentThreadInfo, id), | isMemberOfParentThread: userIsMember(parentThreadInfo, id), | ||||
isMemberOfContainingThread: userIsMember(containingThreadInfo, id), | isMemberOfContainingThread: userIsMember(containingThreadInfo, id), | ||||
}); | }; | ||||
}; | }; | ||||
if (text === '') { | if (text === '') { | ||||
for (const id in userInfos) { | for (const id in userInfos) { | ||||
appendUserInfo(userInfos[id]); | appendUserInfo(userInfos[id]); | ||||
} | } | ||||
} else { | } else { | ||||
const ids = searchIndex.getSearchResults(text); | const ids = searchIndex.getSearchResults(text); | ||||
for (const id of ids) { | for (const id of ids) { | ||||
appendUserInfo(userInfos[id]); | appendUserInfo(userInfos[id]); | ||||
} | } | ||||
} | } | ||||
if (includeServerSearchUsers) { | |||||
for (const userInfo of includeServerSearchUsers) { | |||||
appendUserInfo(userInfo); | |||||
} | |||||
} | |||||
const blockedRelationshipsStatuses = new Set([ | const blockedRelationshipsStatuses = new Set([ | ||||
userRelationshipStatus.BLOCKED_BY_VIEWER, | userRelationshipStatus.BLOCKED_BY_VIEWER, | ||||
userRelationshipStatus.BLOCKED_VIEWER, | userRelationshipStatus.BLOCKED_VIEWER, | ||||
userRelationshipStatus.BOTH_BLOCKED, | userRelationshipStatus.BOTH_BLOCKED, | ||||
]); | ]); | ||||
let userResults = values(results); | |||||
if (text === '') { | if (text === '') { | ||||
results = results.filter(userInfo => | userResults = userResults.filter(userInfo => | ||||
containingThreadInfo | containingThreadInfo | ||||
? userInfo.isMemberOfContainingThread && | ? userInfo.isMemberOfContainingThread && | ||||
!blockedRelationshipsStatuses.has(userInfo.relationshipStatus) | !blockedRelationshipsStatuses.has(userInfo.relationshipStatus) | ||||
: userInfo.relationshipStatus === userRelationshipStatus.FRIEND, | : userInfo?.relationshipStatus === userRelationshipStatus.FRIEND, | ||||
); | ); | ||||
} | } | ||||
const nonFriends = []; | const nonFriends = []; | ||||
const blockedUsers = []; | const blockedUsers = []; | ||||
const friends = []; | const friends = []; | ||||
const containingThreadMembers = []; | const containingThreadMembers = []; | ||||
const parentThreadMembers = []; | const parentThreadMembers = []; | ||||
for (const userResult of results) { | for (const userResult of userResults) { | ||||
const relationshipStatus = userResult.relationshipStatus; | const relationshipStatus = userResult.relationshipStatus; | ||||
if (blockedRelationshipsStatuses.has(relationshipStatus)) { | if (blockedRelationshipsStatuses.has(relationshipStatus)) { | ||||
blockedUsers.push(userResult); | blockedUsers.push(userResult); | ||||
} else if (userResult.isMemberOfParentThread) { | } else if (userResult.isMemberOfParentThread) { | ||||
parentThreadMembers.push(userResult); | parentThreadMembers.push(userResult); | ||||
} else if (userResult.isMemberOfContainingThread) { | } else if (userResult.isMemberOfContainingThread) { | ||||
containingThreadMembers.push(userResult); | containingThreadMembers.push(userResult); | ||||
} else if (relationshipStatus === userRelationshipStatus.FRIEND) { | } else if (relationshipStatus === userRelationshipStatus.FRIEND) { | ||||
▲ Show 20 Lines • Show All 96 Lines • Show Last 20 Lines |