diff --git a/lib/shared/mention-utils.js b/lib/shared/mention-utils.js new file mode 100644 --- /dev/null +++ b/lib/shared/mention-utils.js @@ -0,0 +1,31 @@ +// @flow + +import type { RelativeMemberInfo } from '../types/thread-types'; +import { oldValidUsernameRegexString } from './account-utils'; +import SearchIndex from './search-index'; +import { stringForUserExplicit } from './user-utils'; + +function getTypeaheadUserSuggestions( + userSearchIndex: SearchIndex, + usersInThread: $ReadOnlyArray, + typedPrefix: string, +): $ReadOnlyArray { + const userIDs = userSearchIndex.getSearchResults(typedPrefix); + + return usersInThread + .filter( + (user: RelativeMemberInfo) => + typedPrefix.length === 0 || userIDs.includes(user.id), + ) + .sort((userA, userB) => { + return stringForUserExplicit(userA).localeCompare( + stringForUserExplicit(userB), + ); + }); +} + +const mentionRegex: RegExp = new RegExp( + `(^|.* )@(${oldValidUsernameRegexString})?$`, +); + +export { getTypeaheadUserSuggestions, mentionRegex }; diff --git a/lib/shared/user-utils.js b/lib/shared/user-utils.js --- a/lib/shared/user-utils.js +++ b/lib/shared/user-utils.js @@ -18,7 +18,15 @@ function stringForUser(user: RelativeUserInfo | RelativeMemberInfo): string { if (user.isViewer) { return 'you'; - } else if (user.username) { + } + + return stringForUserExplicit(user); +} + +function stringForUserExplicit( + user: RelativeUserInfo | RelativeMemberInfo, +): string { + if (user.username) { return user.username; } else { return 'anonymous'; @@ -49,4 +57,4 @@ return admin ? userInfos[admin.id] : undefined; } -export { stringForUser, isStaff, getKeyserverAdmin }; +export { stringForUser, stringForUserExplicit, isStaff, getKeyserverAdmin };