diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index add379d462b0..96681d373b4a 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1,4 +1,4 @@ -import {fastMerge} from 'expensify-common'; +import {fastMerge, Str} from 'expensify-common'; import _ from 'lodash'; import lodashFindLast from 'lodash/findLast'; import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; @@ -68,6 +68,7 @@ Onyx.connect({ }); let currentUserAccountID: number | undefined; +let currentEmail = ''; Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { @@ -77,6 +78,7 @@ Onyx.connect({ } currentUserAccountID = value.accountID; + currentEmail = value?.email ?? ''; }, }); @@ -1419,6 +1421,23 @@ function isLinkedTransactionHeld(reportActionID: string, reportID: string): bool return TransactionUtils.isOnHoldByTransactionID(getLinkedTransactionID(reportActionID, reportID) ?? '-1'); } +function getMentionedAccountIDsFromAction(reportAction: OnyxInputOrEntry) { + return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) ? getOriginalMessage(reportAction)?.mentionedAccountIDs ?? [] : []; +} + +function getMentionedEmailsFromMessage(message: string) { + const mentionEmailRegex = /(.*?)<\/mention-user>/g; + const matches = [...message.matchAll(mentionEmailRegex)]; + return matches.map((match) => Str.removeSMSDomain(match[1].substring(1))); +} + +function didMessageMentionCurrentUser(reportAction: OnyxInputOrEntry) { + const accountIDsFromMessage = getMentionedAccountIDsFromAction(reportAction); + const message = getReportActionMessage(reportAction)?.html ?? ''; + const emailsFromMessage = getMentionedEmailsFromMessage(message); + return accountIDsFromMessage.includes(currentUserAccountID ?? -1) || emailsFromMessage.includes(currentEmail) || message.includes(''); +} + /** * Check if the current user is the requestor of the action */ @@ -1626,6 +1645,7 @@ export { getExportIntegrationActionFragments, getExportIntegrationLastMessageText, getExportIntegrationMessageHTML, + didMessageMentionCurrentUser, }; export type {LastVisibleMessage}; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index eaa54c7e08db..0fecf16bc293 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1416,7 +1416,15 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { lastActorAccountID, }; } - + const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; + const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(reportAction); + if (didCommentMentionCurrentUser && reportAction.created === report?.lastMentionedTime) { + const reportActionsForReport = allReportActions?.[reportID]; + const latestMentioneReportAction = Object.values(reportActionsForReport ?? {}).find( + (action) => action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(action), + ); + optimisticReport.lastMentionedTime = latestMentioneReportAction?.created ?? null; + } // If the API call fails we must show the original message again, so we revert the message content back to how it was // and and remove the pendingAction so the strike-through clears const failureData: OnyxUpdate[] = [ diff --git a/src/types/onyx/OriginalMessage.ts b/src/types/onyx/OriginalMessage.ts index 2841b0a7d45c..06b31f242989 100644 --- a/src/types/onyx/OriginalMessage.ts +++ b/src/types/onyx/OriginalMessage.ts @@ -121,6 +121,9 @@ type OriginalMessageAddComment = { /** Collection of accountIDs of users mentioned in message */ whisperedTo: number[]; + + /** List accountIDs are mentioned in message */ + mentionedAccountIDs?: number[]; }; /** Model of `actionable mention whisper` report action */