From 871980e56f969729ae839b1aaf473bb9d298a95e Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 9 May 2024 16:43:29 +0700 Subject: [PATCH 1/6] fix message after deleting self mention displays green dot in LHN --- src/libs/ReportActionsUtils.ts | 30 ++++++++++++++++++++++++++++++ src/libs/actions/Report.ts | 10 +++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 906a2963baa8..7c27bd446674 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1,4 +1,5 @@ import fastMerge from 'expensify-common/lib/fastMerge'; +import Str from 'expensify-common/lib/str'; import _ from 'lodash'; import lodashFindLast from 'lodash/findLast'; import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; @@ -84,6 +85,7 @@ Onyx.connect({ }); let currentUserAccountID: number | undefined; +let currentEmail = ''; Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { @@ -93,6 +95,7 @@ Onyx.connect({ } currentUserAccountID = value.accountID; + currentEmail = value?.email ?? ''; }, }); @@ -1199,6 +1202,32 @@ function isLinkedTransactionHeld(reportActionID: string, reportID: string): bool return TransactionUtils.isOnHoldByTransactionID(getLinkedTransactionID(reportActionID, reportID) ?? ''); } +function getAccountIDsFromMessage(message: string) { + const regex = //g; + const matches = []; + let match; + while ((match = regex.exec(message)) !== null) { + matches.push(match[1]); + } + return matches; +} + +function getMentionedEmailsFromMessage(message: string) { + const regex = /(.*?)<\/mention-user>/g; + const matches = []; + let match; + while ((match = regex.exec(message)) !== null) { + matches.push(Str.removeSMSDomain(match[1].substring(1))); + } + return matches; +} + +function didMessageMentionCurrentUser(message: string) { + const accountIDsFromMessage = getAccountIDsFromMessage(message); + const emailsFromMessage = getMentionedEmailsFromMessage(message); + return accountIDsFromMessage.includes(String(currentUserAccountID)) || emailsFromMessage.includes(currentEmail); +} + export { extractLinksFromMessageHtml, getDismissedViolationMessageText, @@ -1265,6 +1294,7 @@ export { isActionableJoinRequestPending, isActionableTrackExpense, isLinkedTransactionHeld, + didMessageMentionCurrentUser, }; export type {LastVisibleMessage}; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 3154ae218d72..7ba42e24aaab 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1324,7 +1324,15 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { lastActorAccountID, }; } - + const report = ReportUtils.getReport(reportID); + const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(reportAction?.message?.[0]?.html ?? ''); + if (didCommentMentionCurrentUser && reportAction.created === report?.lastMentionedTime) { + const reportActionsForReport = allReportActions?.[reportID]; + const latestMentioneReportAction = Object.values(reportActionsForReport ?? {}).find( + (action) => action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(action?.message?.[0]?.html ?? ''), + ); + 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[] = [ From 37806923f2682f4ffcf442efd732affb5606a7b7 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 9 May 2024 17:28:15 +0700 Subject: [PATCH 2/6] fix lint --- src/libs/ReportActionsUtils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 7c27bd446674..e01277733415 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1206,6 +1206,7 @@ function getAccountIDsFromMessage(message: string) { const regex = //g; const matches = []; let match; + // eslint-disable-next-line no-cond-assign while ((match = regex.exec(message)) !== null) { matches.push(match[1]); } @@ -1216,6 +1217,7 @@ function getMentionedEmailsFromMessage(message: string) { const regex = /(.*?)<\/mention-user>/g; const matches = []; let match; + // eslint-disable-next-line no-cond-assign while ((match = regex.exec(message)) !== null) { matches.push(Str.removeSMSDomain(match[1].substring(1))); } From 6aaa6417e7ce75e92b6cb2fe445c6ec9cfc71dd9 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 26 Jun 2024 16:48:34 +0700 Subject: [PATCH 3/6] fix lint --- src/libs/actions/Report.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index f72810917472..b309285bc5dc 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -222,6 +222,13 @@ Onyx.connect({ }, }); +let reports: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => (reports = value), +}); + const draftNoteMap: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT, @@ -1370,12 +1377,13 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { lastActorAccountID, }; } - const report = ReportUtils.getReport(reportID); - const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(reportAction?.message?.[0]?.html ?? ''); + const report = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; + const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(ReportActionsUtils.getReportActionMessage(reportAction)?.html ?? ''); if (didCommentMentionCurrentUser && reportAction.created === report?.lastMentionedTime) { const reportActionsForReport = allReportActions?.[reportID]; const latestMentioneReportAction = Object.values(reportActionsForReport ?? {}).find( - (action) => action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(action?.message?.[0]?.html ?? ''), + (action) => + action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(ReportActionsUtils.getReportActionMessage(action)?.html ?? ''), ); optimisticReport.lastMentionedTime = latestMentioneReportAction?.created ?? null; } From 390d5b6151297becf3b0938c2e148abc6a15c029 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 11 Jul 2024 10:49:33 +0700 Subject: [PATCH 4/6] fix comments --- src/libs/ReportActionsUtils.ts | 29 +++++++++-------------------- src/libs/actions/Report.ts | 11 ++--------- src/types/onyx/OriginalMessage.ts | 3 +++ 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 9dfd16c0768d..7f75051bb79c 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1382,32 +1382,21 @@ function isLinkedTransactionHeld(reportActionID: string, reportID: string): bool return TransactionUtils.isOnHoldByTransactionID(getLinkedTransactionID(reportActionID, reportID) ?? '-1'); } -function getAccountIDsFromMessage(message: string) { - const regex = //g; - const matches = []; - let match; - // eslint-disable-next-line no-cond-assign - while ((match = regex.exec(message)) !== null) { - matches.push(match[1]); - } - return matches; +function getMentionedAccountIDsFromAction(reportAction: OnyxInputOrEntry) { + return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) ? getOriginalMessage(reportAction)?.mentionedAccountIDs : []; } function getMentionedEmailsFromMessage(message: string) { - const regex = /(.*?)<\/mention-user>/g; - const matches = []; - let match; - // eslint-disable-next-line no-cond-assign - while ((match = regex.exec(message)) !== null) { - matches.push(Str.removeSMSDomain(match[1].substring(1))); - } - return matches; + const mentionEmailRegex = /(.*?)<\/mention-user>/g; + const matches = [...message.matchAll(mentionEmailRegex)]; + return matches.map((match) => Str.removeSMSDomain(match[1].substring(1))); } -function didMessageMentionCurrentUser(message: string) { - const accountIDsFromMessage = getAccountIDsFromMessage(message); +function didMessageMentionCurrentUser(reportAction: OnyxInputOrEntry) { + const accountIDsFromMessage = getMentionedAccountIDsFromAction(reportAction); + const message = getReportActionMessage(reportAction)?.html ?? ''; const emailsFromMessage = getMentionedEmailsFromMessage(message); - return accountIDsFromMessage.includes(String(currentUserAccountID)) || emailsFromMessage.includes(currentEmail); + return accountIDsFromMessage?.includes(currentUserAccountID ?? -1) || emailsFromMessage.includes(currentEmail) || message.includes(''); } /** diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index c4a385397f85..6de5813e8fc1 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -211,13 +211,6 @@ Onyx.connect({ }, }); -let reports: OnyxCollection; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (value) => (reports = value), -}); - const draftNoteMap: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT, @@ -1400,8 +1393,8 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { lastActorAccountID, }; } - const report = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(ReportActionsUtils.getReportActionMessage(reportAction)?.html ?? ''); + 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( diff --git a/src/types/onyx/OriginalMessage.ts b/src/types/onyx/OriginalMessage.ts index 3d864523e418..cfa1e5cc78b1 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 */ From 29964ea6b485fd3056e7b5cc866e69a0e8731629 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 11 Jul 2024 10:54:54 +0700 Subject: [PATCH 5/6] fix lint --- src/libs/actions/Report.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 6de5813e8fc1..69ae032c5bd6 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1398,8 +1398,7 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) { if (didCommentMentionCurrentUser && reportAction.created === report?.lastMentionedTime) { const reportActionsForReport = allReportActions?.[reportID]; const latestMentioneReportAction = Object.values(reportActionsForReport ?? {}).find( - (action) => - action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(ReportActionsUtils.getReportActionMessage(action)?.html ?? ''), + (action) => action.reportActionID !== reportAction.reportActionID && ReportActionsUtils.didMessageMentionCurrentUser(action), ); optimisticReport.lastMentionedTime = latestMentioneReportAction?.created ?? null; } From 59b0f3a9db8d023603c4f93cf17bc4b54102d985 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 11 Jul 2024 11:10:21 +0700 Subject: [PATCH 6/6] fix lint --- src/libs/ReportActionsUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 7f75051bb79c..b68a7eb45403 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1383,7 +1383,7 @@ function isLinkedTransactionHeld(reportActionID: string, reportID: string): bool } function getMentionedAccountIDsFromAction(reportAction: OnyxInputOrEntry) { - return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) ? getOriginalMessage(reportAction)?.mentionedAccountIDs : []; + return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) ? getOriginalMessage(reportAction)?.mentionedAccountIDs ?? [] : []; } function getMentionedEmailsFromMessage(message: string) { @@ -1396,7 +1396,7 @@ function didMessageMentionCurrentUser(reportAction: OnyxInputOrEntry'); + return accountIDsFromMessage.includes(currentUserAccountID ?? -1) || emailsFromMessage.includes(currentEmail) || message.includes(''); } /**