diff --git a/lib/shared/mention-utils.js b/lib/shared/mention-utils.js new file mode 100644 index 000000000..cd4d5feb0 --- /dev/null +++ b/lib/shared/mention-utils.js @@ -0,0 +1,29 @@ +// @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) => + 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 index c97e9c404..3239f37da 100644 --- a/lib/shared/user-utils.js +++ b/lib/shared/user-utils.js @@ -1,52 +1,60 @@ // @flow import bots from '../facts/bots'; import staff from '../facts/staff'; import type { ServerThreadInfo, RawThreadInfo, RelativeMemberInfo, ThreadInfo, } from '../types/thread-types'; import type { RelativeUserInfo, UserInfo, UserInfos, } from '../types/user-types'; import { memberHasAdminPowers } from './thread-utils'; 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'; } } function isStaff(userID: string): boolean { if (staff.includes(userID)) { return true; } for (const key in bots) { const bot = bots[key]; if (userID === bot.userID) { return true; } } return false; } function getKeyserverAdmin( community: ThreadInfo | RawThreadInfo | ServerThreadInfo, userInfos: UserInfos, ): ?UserInfo { // This hack only works as long as there is only one admin // Linear task to revert this: // https://linear.app/comm/issue/ENG-1707/revert-fix-getting-the-keyserver-admin-info const admin = community.members.find(memberHasAdminPowers); return admin ? userInfos[admin.id] : undefined; } -export { stringForUser, isStaff, getKeyserverAdmin }; +export { stringForUser, stringForUserExplicit, isStaff, getKeyserverAdmin };