Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3189,13 +3189,19 @@ function removeLinksFromHtml(html: string, links: string[]): string {
* @param originalCommentMarkdown original markdown of the comment before editing.
* @param videoAttributeCache cache of video attributes ([videoSource]: videoAttributes)
*/
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string, currentUserLogin: string, videoAttributeCache?: Record<string, string>): string {
function handleUserDeletedLinksInHtml(
newCommentText: string,
originalCommentMarkdown: string,
currentUserLogin: string,
personalDetails: OnyxEntry<PersonalDetailsList>,
Comment thread
lorretheboy marked this conversation as resolved.
videoAttributeCache?: Record<string, string>,
): string {
if (newCommentText.length > CONST.MAX_MARKUP_LENGTH) {
return newCommentText;
}

const userEmailDomain = isEmailPublicDomain(currentUserLogin) ? '' : Str.extractEmailDomain(currentUserLogin);
const allPersonalDetailLogins = Object.values(allPersonalDetails ?? {}).map((personalDetail) => personalDetail?.login ?? '');
const allPersonalDetailLogins = Object.values(personalDetails ?? {}).map((personalDetail) => personalDetail?.login ?? '');

const htmlForNewComment = getParsedMessageWithShortMentions({
text: newCommentText,
Expand All @@ -3219,6 +3225,7 @@ function editReportComment(
isOriginalReportArchived: boolean | undefined,
isOriginalParentReportArchived: boolean | undefined,
currentUserLogin: string,
personalDetails: OnyxEntry<PersonalDetailsList>,
videoAttributeCache?: Record<string, string>,
visibleReportActionsDataParam?: VisibleReportActionsDerivedValue,
) {
Expand All @@ -3239,7 +3246,7 @@ function editReportComment(
if (originalCommentMarkdown === textForNewComment) {
return;
}
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, currentUserLogin, videoAttributeCache);
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, currentUserLogin, personalDetails, videoAttributeCache);

const reportComment = Parser.htmlToText(htmlForNewComment);

Expand Down
2 changes: 1 addition & 1 deletion src/pages/PrivateNotes/PrivateNotesEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function PrivateNotesEditPageInternal({route, report, accountID, privateNoteDraf
const originalNote = report?.privateNotes?.[Number(route.params.accountID)]?.note ?? '';
let editedNote = '';
if (privateNote.trim() !== originalNote.trim()) {
editedNote = handleUserDeletedLinksInHtml(privateNote.trim(), Parser.htmlToMarkdown(originalNote).trim(), login ?? '', undefined);
editedNote = handleUserDeletedLinksInHtml(privateNote.trim(), Parser.htmlToMarkdown(originalNote).trim(), login ?? '', personalDetailsList);
updatePrivateNotes(report.reportID, Number(route.params.accountID), editedNote);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function useEditMessage({reportID, originalReportID, reportAction, shouldScrollT
const actionOwnerReportID = originalReportID ?? reportID;
const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${actionOwnerReportID}`);
const [visibleReportActionsData] = useOnyx(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const originalParentReportID = getOriginalReportID(actionOwnerReportID, reportAction, reportActions);
const [originalReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${actionOwnerReportID}`);
const isOriginalReportArchived = useReportIsArchived(actionOwnerReportID);
Expand Down Expand Up @@ -96,6 +97,7 @@ function useEditMessage({reportID, originalReportID, reportAction, shouldScrollT
isOriginalReportArchived,
isOriginalParentReportArchived,
email ?? '',
personalDetails,
Object.fromEntries(draftMessageVideoAttributeCache),
visibleReportActionsData,
);
Expand Down
47 changes: 26 additions & 21 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,31 +802,31 @@ describe('actions/Report', () => {
// We should generate link
let originalCommentMarkdown = 'Original Comment';
let afterEditCommentText = 'Original Comment www.google.com';
let newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
let newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
let expectedOutput = 'Original Comment <a href="https://www.google.com" target="_blank" rel="noreferrer noopener">www.google.com</a>';
expect(newCommentHTML).toBe(expectedOutput);

// User deletes www.google.com link from comment but keeps link text
// We should not generate link
originalCommentMarkdown = 'Comment [www.google.com](https://www.google.com)';
afterEditCommentText = 'Comment www.google.com';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = 'Comment www.google.com';
expect(newCommentHTML).toBe(expectedOutput);

// User Delete only () part of link but leaves the []
// We should not generate link
originalCommentMarkdown = 'Comment [www.google.com](https://www.google.com)';
afterEditCommentText = 'Comment [www.google.com]';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = 'Comment [www.google.com]';
expect(newCommentHTML).toBe(expectedOutput);

// User Generates multiple links in one edit
// We should generate both links
originalCommentMarkdown = 'Comment';
afterEditCommentText = 'Comment www.google.com www.facebook.com';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput =
'Comment <a href="https://www.google.com" target="_blank" rel="noreferrer noopener">www.google.com</a> ' +
'<a href="https://www.facebook.com" target="_blank" rel="noreferrer noopener">www.facebook.com</a>';
Expand All @@ -836,39 +836,39 @@ describe('actions/Report', () => {
// Should not generate link again for the deleted one
originalCommentMarkdown = 'Comment [www.google.com](https://www.google.com) [www.facebook.com](https://www.facebook.com)';
afterEditCommentText = 'Comment www.google.com [www.facebook.com](https://www.facebook.com)';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = 'Comment www.google.com <a href="https://www.facebook.com" target="_blank" rel="noreferrer noopener">www.facebook.com</a>';
expect(newCommentHTML).toBe(expectedOutput);

// User edits and replaces comment with a link containing underscores
// We should generate link
originalCommentMarkdown = 'Comment';
afterEditCommentText = 'https://www.facebook.com/hashtag/__main/?__eep__=6';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = '<a href="https://www.facebook.com/hashtag/__main/?__eep__=6" target="_blank" rel="noreferrer noopener">https://www.facebook.com/hashtag/__main/?__eep__=6</a>';
expect(newCommentHTML).toBe(expectedOutput);

// User edits and deletes the link containing underscores
// We should not generate link
originalCommentMarkdown = '[https://www.facebook.com/hashtag/__main/?__eep__=6](https://www.facebook.com/hashtag/__main/?__eep__=6)';
afterEditCommentText = 'https://www.facebook.com/hashtag/__main/?__eep__=6';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = 'https://www.facebook.com/hashtag/__main/?__eep__=6';
expect(newCommentHTML).toBe(expectedOutput);

// User edits and replaces comment with a link containing asterisks
// We should generate link
originalCommentMarkdown = 'Comment';
afterEditCommentText = 'http://example.com/foo/*/bar/*/test.txt';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = '<a href="http://example.com/foo/*/bar/*/test.txt" target="_blank" rel="noreferrer noopener">http://example.com/foo/*/bar/*/test.txt</a>';
expect(newCommentHTML).toBe(expectedOutput);

// User edits and deletes the link containing asterisks
// We should not generate link
originalCommentMarkdown = '[http://example.com/foo/*/bar/*/test.txt](http://example.com/foo/*/bar/*/test.txt)';
afterEditCommentText = 'http://example.com/foo/*/bar/*/test.txt';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {});
expectedOutput = 'http://example.com/foo/*/bar/*/test.txt';
expect(newCommentHTML).toBe(expectedOutput);

Expand All @@ -884,7 +884,9 @@ describe('actions/Report', () => {
});
originalCommentMarkdown = 'Comment';
afterEditCommentText = 'Comment @user';
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN);
newCommentHTML = Report.handleUserDeletedLinksInHtml(afterEditCommentText, originalCommentMarkdown, TEST_USER_LOGIN, {
[privateDomainAccount.accountID]: privateDomainAccount,
});
expectedOutput = 'Comment <mention-user>@user@expensify.com</mention-user>';
expect(newCommentHTML).toBe(expectedOutput);
});
Expand Down Expand Up @@ -1362,7 +1364,7 @@ describe('actions/Report', () => {
};

const {result: ancestors, rerender} = renderHook(() => useAncestors(originalReport));
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, '');
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, '', {});

await waitForBatchedUpdates();

Expand Down Expand Up @@ -1444,7 +1446,7 @@ describe('actions/Report', () => {
const {result: ancestors, rerender} = renderHook(() => useAncestors(originalReport));

const currentUserEmail = 'test@test.com';
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, currentUserEmail);
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, currentUserEmail, {});
await waitForBatchedUpdates();

const persistedRequests = await getOnyxValue(ONYXKEYS.PERSISTED_REQUESTS);
Expand Down Expand Up @@ -1504,7 +1506,7 @@ describe('actions/Report', () => {
};
const {result: ancestors, rerender} = renderHook(() => useAncestors(originalReport));

Report.editReportComment(originalReport, reportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, '');
Report.editReportComment(originalReport, reportAction, ancestors.current, 'Testing an edited comment', undefined, undefined, '', {});

await waitForBatchedUpdates();

Expand Down Expand Up @@ -2323,7 +2325,7 @@ describe('actions/Report', () => {
const originalReport = {
reportID: REPORT_ID,
};
Report.editReportComment(originalReport, reportAction, [], 'Testing an edited comment', undefined, undefined, '');
Report.editReportComment(originalReport, reportAction, [], 'Testing an edited comment', undefined, undefined, '', {});

await waitForBatchedUpdates();

Expand Down Expand Up @@ -2362,9 +2364,9 @@ describe('actions/Report', () => {

const {result: ancestors} = renderHook(() => useAncestors(originalReport));

Report.editReportComment(originalReport, action, ancestors.current, 'value1', undefined, undefined, '');
Report.editReportComment(originalReport, action, ancestors.current, 'value2', undefined, undefined, '');
Report.editReportComment(originalReport, action, ancestors.current, 'value3', undefined, undefined, '');
Report.editReportComment(originalReport, action, ancestors.current, 'value1', undefined, undefined, '', {});
Report.editReportComment(originalReport, action, ancestors.current, 'value2', undefined, undefined, '', {});
Report.editReportComment(originalReport, action, ancestors.current, 'value3', undefined, undefined, '', {});

const requests = PersistedRequests?.getAll();

Expand Down Expand Up @@ -2426,7 +2428,10 @@ describe('actions/Report', () => {
const {result: ancestors} = renderHook(() => useAncestors(originalReport));

// Edit the comment to add a short mention
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Initial comment with @bob', undefined, undefined, TEST_USER_LOGIN);
Report.editReportComment(originalReport, newReportAction, ancestors.current, 'Initial comment with @bob', undefined, undefined, TEST_USER_LOGIN, {
[TEST_USER_ACCOUNT_ID]: {accountID: TEST_USER_ACCOUNT_ID, login: TEST_USER_LOGIN},
[MENTIONED_USER_ACCOUNT_ID]: {accountID: MENTIONED_USER_ACCOUNT_ID, login: MENTIONED_USER_LOGIN, displayName: 'Bob'},
});

await waitForBatchedUpdates();

Expand Down Expand Up @@ -2467,9 +2472,9 @@ describe('actions/Report', () => {
const {result: ancestors} = renderHook(() => useAncestors(originalReport));
const currentUserEmail = 'user@test.com';

Report.editReportComment(originalReport, action, ancestors.current, 'value1', undefined, undefined, currentUserEmail);
Report.editReportComment(originalReport, action, ancestors.current, 'value2', undefined, undefined, currentUserEmail);
Report.editReportComment(originalReport, action, ancestors.current, 'value3', undefined, undefined, currentUserEmail);
Report.editReportComment(originalReport, action, ancestors.current, 'value1', undefined, undefined, currentUserEmail, {});
Report.editReportComment(originalReport, action, ancestors.current, 'value2', undefined, undefined, currentUserEmail, {});
Report.editReportComment(originalReport, action, ancestors.current, 'value3', undefined, undefined, currentUserEmail, {});

const requests = PersistedRequests?.getAll();
expect(requests.length).toBe(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/ReportActionItemMessageEditTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('ReportActionCompose Integration Tests', () => {
expect(mockEditReportComment).toHaveBeenCalledTimes(1);

const editReportCommentArgs = mockEditReportComment.mock.calls.at(0);
const videoAttributeCache = editReportCommentArgs?.[7];
const videoAttributeCache = editReportCommentArgs?.[8];

expect(videoAttributeCache).toEqual(expect.any(Object));
expect(videoAttributeCache?.[videoSource]).toEqual(expect.any(String));
Expand Down
Loading