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,29 @@ +// @flow + +import type { RelativeMemberInfo } from '../types/thread-types'; +import { oldValidUsernameRegexString } from './account-utils'; +import { stringForUserExplicit } from './user-utils'; + +function getTypeaheadUserSuggestions( + usersInThread: $ReadOnlyArray, + typedPrefix: string, +): $ReadOnlyArray { + return usersInThread + .filter(user => { + if (stringForUserExplicit(user) === 'anonymous') { + return false; + } + return stringForUserExplicit(user).startsWith(typedPrefix); + }) + .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 @@ -25,6 +25,16 @@ } } +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; @@ -49,4 +59,4 @@ return admin ? userInfos[admin.id] : undefined; } -export { stringForUser, isStaff, getKeyserverAdmin }; +export { stringForUser, stringForUserExplicit, isStaff, getKeyserverAdmin };