diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -441,6 +441,39 @@ }; } +// Returns map from lowercase username to AccountUserInfo +function memberLowercaseUsernameMap( + members: $ReadOnlyArray, +): Map { + const memberMap = new Map(); + for (const member of members) { + const { id, role, username } = member; + if (!role || !username) { + continue; + } + memberMap.set(username.toLowerCase(), { id, username }); + } + return memberMap; +} + +// Returns map from user ID to AccountUserInfo +function extractMentionedMembers( + text: string, + threadInfo: ThreadInfo, +): Map { + const memberMap = memberLowercaseUsernameMap(threadInfo.members); + const mentions = extractMentionsFromText(text); + + const mentionedMembers = new Map(); + for (const mention of mentions) { + const userInfo = memberMap.get(mention.toLowerCase()); + if (userInfo) { + mentionedMembers.set(userInfo.id, userInfo); + } + } + return mentionedMembers; +} + type SharedCreatePendingSidebarInput = { +sourceMessageInfo: ComposableMessageInfo | RobotextMessageInfo, +parentThreadInfo: ThreadInfo, @@ -499,20 +532,12 @@ } if (sourceMessageInfo.type === messageTypes.TEXT) { - const memberMap = new Map(); - for (const member of parentThreadInfo.members) { - const { id, role, username } = member; - if (!role || !username) { - continue; - } - memberMap.set(username.toLowerCase(), { id, username }); - } - const mentions = extractMentionsFromText(sourceMessageInfo.text); - for (const mention of mentions) { - const userInfo = memberMap.get(mention.toLowerCase()); - if (userInfo) { - initialMembers.set(userInfo.id, userInfo); - } + const mentionedMembersOfParent = extractMentionedMembers( + sourceMessageInfo.text, + parentThreadInfo, + ); + for (const [memberID, member] of mentionedMembersOfParent) { + initialMembers.set(memberID, member); } } @@ -1524,6 +1549,7 @@ parsePendingThreadID, createPendingThread, createUnresolvedPendingSidebar, + extractMentionedMembers, createPendingSidebar, pendingThreadType, createRealThreadFromPendingThread,