Skip to content

Commit

Permalink
Merge pull request #41888 from tienifr/fix/40469
Browse files Browse the repository at this point in the history
fix message after deleting self mention displays green dot in LHN
  • Loading branch information
neil-marcellini authored Jul 23, 2024
2 parents a78d124 + 8f88a88 commit 91a5c69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -68,6 +68,7 @@ Onyx.connect({
});

let currentUserAccountID: number | undefined;
let currentEmail = '';
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (value) => {
Expand All @@ -77,6 +78,7 @@ Onyx.connect({
}

currentUserAccountID = value.accountID;
currentEmail = value?.email ?? '';
},
});

Expand Down Expand Up @@ -1440,6 +1442,23 @@ function isLinkedTransactionHeld(reportActionID: string, reportID: string): bool
return TransactionUtils.isOnHoldByTransactionID(getLinkedTransactionID(reportActionID, reportID) ?? '-1');
}

function getMentionedAccountIDsFromAction(reportAction: OnyxInputOrEntry<ReportAction>) {
return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) ? getOriginalMessage(reportAction)?.mentionedAccountIDs ?? [] : [];
}

function getMentionedEmailsFromMessage(message: string) {
const mentionEmailRegex = /<mention-user>(.*?)<\/mention-user>/g;
const matches = [...message.matchAll(mentionEmailRegex)];
return matches.map((match) => Str.removeSMSDomain(match[1].substring(1)));
}

function didMessageMentionCurrentUser(reportAction: OnyxInputOrEntry<ReportAction>) {
const accountIDsFromMessage = getMentionedAccountIDsFromAction(reportAction);
const message = getReportActionMessage(reportAction)?.html ?? '';
const emailsFromMessage = getMentionedEmailsFromMessage(message);
return accountIDsFromMessage.includes(currentUserAccountID ?? -1) || emailsFromMessage.includes(currentEmail) || message.includes('<mention-here>');
}

/**
* Check if the current user is the requestor of the action
*/
Expand Down Expand Up @@ -1648,6 +1667,7 @@ export {
getExportIntegrationActionFragments,
getExportIntegrationLastMessageText,
getExportIntegrationMessageHTML,
didMessageMentionCurrentUser,
};

export type {LastVisibleMessage};
10 changes: 9 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,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[] = [
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 91a5c69

Please sign in to comment.