Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix message after deleting self mention displays green dot in LHN #41888

Merged
merged 14 commits into from
Jul 23, 2024
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 @@ -1419,6 +1421,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 @@ -1626,6 +1645,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 @@ -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[] = [
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
Loading