From baf403b829812a6f97bafe0843340c4ba780e50e Mon Sep 17 00:00:00 2001 From: Mitrovic Djordje Date: Tue, 29 Apr 2025 02:23:48 +0200 Subject: [PATCH 1/5] Added chat thread functions to determine if message is deleted --- src/libs/ReportActionsUtils.ts | 13 +++++++++++-- src/libs/ReportUtils.ts | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 008ffad8b9f6..396a185ae315 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -182,7 +182,7 @@ function getReportActionMessage(reportAction: PartialReportAction) { } function isDeletedParentAction(reportAction: OnyxInputOrEntry): boolean { - return (getReportActionMessage(reportAction)?.isDeletedParentAction ?? false) && (reportAction?.childVisibleActionCount ?? 0) > 0; + return isMessageDeleted(reportAction) && (reportAction?.childVisibleActionCount ?? 0) > 0; } function isReversedTransaction(reportAction: OnyxInputOrEntry) { @@ -1124,7 +1124,13 @@ function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry): boolean { - return getReportActionMessage(reportAction)?.isDeletedParentAction ?? false; + const message = getReportActionMessage(reportAction); + const originalMessage = getOriginalMessage(reportAction) as Message; + + return (message?.deleted != null && message?.deleted != "") || + (message?.isDeletedParentAction ?? false) || + (originalMessage?.deleted != null && originalMessage?.deleted != "") || + (originalMessage?.isDeletedParentAction ?? false) } /** @@ -1557,6 +1563,9 @@ function getReportActionMessageFragments(action: ReportAction): Message[] { const actionMessage = action.previousMessage ?? action.message; if (Array.isArray(actionMessage)) { + if (actionMessage?.length === 1) { + return [{...actionMessage[0], isDeletedParentAction: isMessageDeleted(action) } as Message] + } return actionMessage.filter((item): item is Message => !!item); } return actionMessage ? [actionMessage] : []; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3aa5a6147df7..fae91c5caaba 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -171,6 +171,7 @@ import { isDeletedParentAction, isExportIntegrationAction, isForwardedAction, + isMessageDeleted, isModifiedExpenseAction, isMoneyRequestAction, isOldDotReportAction, @@ -1294,7 +1295,7 @@ function isIOUReport(reportOrID: OnyxInputOrEntry | SearchReport | strin * Checks if a report is an IOU report using report */ function isIOUReportUsingReport(report: OnyxEntry): report is Report { - return report?.type === CONST.REPORT.TYPE.IOU; + return report?.type === CONST.REPORT.TYPE.IOU; } /** * Checks if a report is a task report. @@ -4620,6 +4621,19 @@ function getSearchReportName(props: GetReportNameParams): string { } return getReportNameInternal(props); } +function isChatThreadDeleted(report: OnyxInputOrEntry, reportActionParam: OnyxInputOrEntry): boolean + { + if (!isChatThread(report)) { + return false + } + + let reportAction = reportActionParam as OnyxEntry + if (!reportAction) { + reportAction = getReportAction(report?.parentReportID, report?.parentReportActionID); + } + + return isMessageDeleted(reportAction) + } function getInvoiceReportName(report: OnyxEntry, policy?: OnyxEntry, invoiceReceiverPolicy?: OnyxEntry): string { const moneyRequestReportName = getMoneyRequestReportName({report, policy, invoiceReceiverPolicy}); @@ -4747,7 +4761,7 @@ function getReportNameInternal({ return getRenamedAction(parentReportAction); } - if (parentReportActionMessage?.isDeletedParentAction) { + if (isChatThreadDeleted(report, parentReportActionParam)) { return translateLocal('parentReportAction.deletedMessage'); } @@ -10640,6 +10654,7 @@ export { isChatRoom, isTripRoom, isChatThread, + isChatThreadDeleted, isChildReport, isClosedExpenseReportWithNoExpenses, isCompletedTaskReport, From d8f917e4d4afa019fadc24b1872bba2e6eeffc97 Mon Sep 17 00:00:00 2001 From: Mitrovic Djordje Date: Wed, 7 May 2025 22:33:48 +0200 Subject: [PATCH 2/5] Finished feature --- src/libs/ReportActionsUtils.ts | 13 +++++++++++-- src/libs/ReportUtils.ts | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 476ddc9343c4..005c1170a5c9 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -182,7 +182,7 @@ function getReportActionMessage(reportAction: PartialReportAction) { } function isDeletedParentAction(reportAction: OnyxInputOrEntry): boolean { - return (getReportActionMessage(reportAction)?.isDeletedParentAction ?? false) && (reportAction?.childVisibleActionCount ?? 0) > 0; + return isMessageDeleted(reportAction) && (reportAction?.childVisibleActionCount ?? 0) > 0; } function isReversedTransaction(reportAction: OnyxInputOrEntry) { @@ -1132,7 +1132,13 @@ function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry): boolean { - return getReportActionMessage(reportAction)?.isDeletedParentAction ?? false; + const message = getReportActionMessage(reportAction); + const originalMessage = getOriginalMessage(reportAction) as Message; + + return (message?.deleted != null && message?.deleted != "") || + (message?.isDeletedParentAction ?? false) || + (originalMessage?.deleted != null && originalMessage?.deleted != "") || + (originalMessage?.isDeletedParentAction ?? false) } /** @@ -1570,6 +1576,9 @@ function getReportActionMessageFragments(action: ReportAction): Message[] { const actionMessage = action.previousMessage ?? action.message; if (Array.isArray(actionMessage)) { + if (actionMessage?.length === 1) { + return [{...actionMessage[0], isDeletedParentAction: isMessageDeleted(action) } as Message] + } return actionMessage.filter((item): item is Message => !!item); } return actionMessage ? [actionMessage] : []; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 175dc1221f83..b5020917c3eb 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -175,6 +175,7 @@ import { isForwardedAction, isIntegrationMessageAction, isMarkAsClosedAction, + isMessageDeleted, isModifiedExpenseAction, isMoneyRequestAction, isOldDotReportAction, @@ -1301,7 +1302,7 @@ function isIOUReport(reportOrID: OnyxInputOrEntry | SearchReport | strin * Checks if a report is an IOU report using report */ function isIOUReportUsingReport(report: OnyxEntry): report is Report { - return report?.type === CONST.REPORT.TYPE.IOU; + return report?.type === CONST.REPORT.TYPE.IOU; } /** * Checks if a report is a task report. @@ -4649,6 +4650,19 @@ function getSearchReportName(props: GetReportNameParams): string { } return getReportNameInternal(props); } +function isChatThreadDeleted(report: OnyxInputOrEntry, reportActionParam: OnyxInputOrEntry): boolean + { + if (!isChatThread(report)) { + return false + } + + let reportAction = reportActionParam as OnyxEntry + if (!reportAction) { + reportAction = getReportAction(report?.parentReportID, report?.parentReportActionID); + } + + return isMessageDeleted(reportAction) + } function getInvoiceReportName(report: OnyxEntry, policy?: OnyxEntry, invoiceReceiverPolicy?: OnyxEntry): string { const moneyRequestReportName = getMoneyRequestReportName({report, policy, invoiceReceiverPolicy}); @@ -4780,7 +4794,7 @@ function getReportNameInternal({ return getRenamedAction(parentReportAction, isExpenseReport(getReport(report.parentReportID, allReports))); } - if (parentReportActionMessage?.isDeletedParentAction) { + if (isChatThreadDeleted(report, parentReportActionParam)) { return translateLocal('parentReportAction.deletedMessage'); } @@ -10795,6 +10809,7 @@ export { isChatRoom, isTripRoom, isChatThread, + isChatThreadDeleted, isChildReport, isClosedExpenseReportWithNoExpenses, isCompletedTaskReport, From 496d0a61e81d34e04c3ca264c2267d526d8b429a Mon Sep 17 00:00:00 2001 From: mitrovicdjordje00 Date: Thu, 8 May 2025 00:36:20 +0000 Subject: [PATCH 3/5] Added tests --- tests/unit/ReportActionsUtilsTest.ts | 97 ++++++++++++++++++++++++++++ tests/unit/ReportUtilsTest.ts | 27 ++++++++ 2 files changed, 124 insertions(+) diff --git a/tests/unit/ReportActionsUtilsTest.ts b/tests/unit/ReportActionsUtilsTest.ts index 8452950ed521..de63a0fe1da1 100644 --- a/tests/unit/ReportActionsUtilsTest.ts +++ b/tests/unit/ReportActionsUtilsTest.ts @@ -9,6 +9,7 @@ import createRandomReport from '../utils/collections/reports'; import * as LHNTestUtils from '../utils/LHNTestUtils'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; +import { isMessageDeleted } from '../../src/libs/ReportActionsUtils'; jest.mock('@components/ConfirmedRoute.tsx'); @@ -696,6 +697,86 @@ describe('ReportActionsUtils', () => { }); }); + describe('isMessageDeleted', () => { + it('should return true if there is property deleted inside message', () => { + expect( + isMessageDeleted( + { + message: [ + { + deleted: "someDate", + type: "COMMENT", + text: "" + } + ] + } as ReportAction + ) + ).toBeTruthy(); + }) + it('should return true if there is property isDeletedParentAction inside message', () => { + expect( + isMessageDeleted( + { + message: [ + { + isDeletedParentAction: true, + type: "COMMENT", + text: "" + } + ] + } as ReportAction + ) + ).toBeTruthy(); + }) + it('should return true if there is property deleted inside original message', () => { + expect( + isMessageDeleted( + { + originalMessage: { + deleted: "someDate", + type: "COMMENT", + text: "", + }, + reportActionID: "reportActionID", + actionName: "ACTIONABLEADDPAYMENTCARD", + created: "someDate" + } as ReportAction + ) + ).toBeTruthy(); + }) + it('should return true if there is property isDeletedParentAction inside original message', () => { + expect( + isMessageDeleted( + { + originalMessage: { + isDeletedParentAction: true, + type: "COMMENT", + text: "", + }, + reportActionID: "reportActionID", + actionName: "ACTIONABLEADDPAYMENTCARD", + created: "someDate" + } as ReportAction + ) + ).toBeTruthy(); + }) + it('should return false if there are no deletion properties in message and originalMessage', () => { + expect( + isMessageDeleted( + { + originalMessage: { + type: "COMMENT", + text: "", + }, + reportActionID: "reportActionID", + actionName: "ACTIONABLEADDPAYMENTCARD", + created: "someDate" + } as ReportAction + ) + ).toBeFalsy(); + }) + }) + describe('getReportActionMessageFragments', () => { it('should return the correct fragment for the REIMBURSED action', () => { const action = { @@ -734,6 +815,22 @@ describe('ReportActionsUtils', () => { const expectedFragments = ReportActionsUtils.getReportActionMessageFragments(action); expect(expectedFragments).toEqual([{text: expectedMessage, html: `${expectedMessage}`, type: 'COMMENT'}]); }); + it('should return the correct fragment for the deleted message', () => { + const action = { + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + reportActionID: '1', + created: '1', + message: [ + { + type: 'COMMENT', + text: '', + deleted: 'someDate' + } + ], + }; + const expectedFragments = ReportActionsUtils.getReportActionMessageFragments(action); + expect(expectedFragments).toEqual([{text: '', type: 'COMMENT', isDeletedParentAction: true, deleted: 'someDate'}]); + }); }); describe('shouldShowAddMissingDetails', () => { diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 0516bd60f7fc..bedc7e9ee13f 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -444,6 +444,33 @@ describe('ReportUtils', () => { ).toBe('floki@vikings.net'); }); + test('with deleted message and provided parent action param', () => { + expect( + getReportName({ + type: CONST.REPORT.TYPE.CHAT, + parentReportActionID: "id", + parentReportID: "id", + reportID: "id" + }, + undefined, + { message: [{ deleted: "someDate", type: "COMMENT", text: "" }] } as ReportAction), + ).toEqual(translateLocal('parentReportAction.deletedMessage')); + }); + + test('with deleted message and not provided parent action param', async () => { + const reportAction = { reportActionID: "reportActionID", parentReportID: "parentID", message: [{ deleted: "someDate", type: "COMMENT", text: "" }] } + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportAction.parentReportID}`, { [reportAction.reportActionID]: reportAction as any} ) + expect( + getReportName({ + type: CONST.REPORT.TYPE.CHAT, + parentReportActionID: "reportActionID", + parentReportID: "parentID", + reportID: "id" + })).toEqual(translateLocal('parentReportAction.deletedMessage')); + + + }); + test('SMS', () => { expect( getReportName({ From 49aa56bcfb890a44f2c691fcad46c2426964b12e Mon Sep 17 00:00:00 2001 From: Mitrovic Djordje Date: Thu, 8 May 2025 22:25:38 +0200 Subject: [PATCH 4/5] Sidebar fix --- src/libs/ReportUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index b5020917c3eb..3304458c5543 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4637,7 +4637,8 @@ function getReportName( const canUseDerivedValue = report && policy === undefined && parentReportActionParam === undefined && personalDetails === undefined && invoiceReceiverPolicy === undefined; const attributes = reportAttributesParam ?? reportAttributes; const derivedNameExists = report && !!attributes?.[report.reportID]?.reportName; - if (canUseDerivedValue && derivedNameExists) { + // This doesn't apply to chat reports because last message (report name) can be changed and/or edited + if (canUseDerivedValue && derivedNameExists && !isChatReport(report)) { return attributes[report.reportID].reportName; } return getReportNameInternal({report, policy, parentReportActionParam, personalDetails, invoiceReceiverPolicy}); From bec9c8eb6b2a8698361db2cef2f7494dbafe3090 Mon Sep 17 00:00:00 2001 From: Mitrovic Djordje Date: Mon, 12 May 2025 18:11:27 +0200 Subject: [PATCH 5/5] Fixed tests --- tests/unit/ReportActionsUtilsTest.ts | 152 +++++++++++++++------------ tests/unit/ReportUtilsTest.ts | 61 +++++++---- 2 files changed, 127 insertions(+), 86 deletions(-) diff --git a/tests/unit/ReportActionsUtilsTest.ts b/tests/unit/ReportActionsUtilsTest.ts index de63a0fe1da1..443f5e9eee14 100644 --- a/tests/unit/ReportActionsUtilsTest.ts +++ b/tests/unit/ReportActionsUtilsTest.ts @@ -699,81 +699,103 @@ describe('ReportActionsUtils', () => { describe('isMessageDeleted', () => { it('should return true if there is property deleted inside message', () => { - expect( - isMessageDeleted( + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [] + }, + message: [ { - message: [ - { - deleted: "someDate", - type: "COMMENT", - text: "" - } - ] - } as ReportAction - ) - ).toBeTruthy(); + html: 'Hello world', + deleted: '2025-05-12 17:37:01.825', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + }, + ], + }; + expect(isMessageDeleted(reportAction)).toBeTruthy(); }) it('should return true if there is property isDeletedParentAction inside message', () => { - expect( - isMessageDeleted( + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [] + }, + message: [ { - message: [ - { - isDeletedParentAction: true, - type: "COMMENT", - text: "" - } - ] - } as ReportAction - ) - ).toBeTruthy(); + html: 'Hello world', + isDeletedParentAction: true, + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + }, + ], + }; + expect(isMessageDeleted(reportAction)).toBeTruthy(); }) it('should return true if there is property deleted inside original message', () => { - expect( - isMessageDeleted( + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + deleted: '2025-05-12 17:37:01.825' + }, + message: [ { - originalMessage: { - deleted: "someDate", - type: "COMMENT", - text: "", - }, - reportActionID: "reportActionID", - actionName: "ACTIONABLEADDPAYMENTCARD", - created: "someDate" - } as ReportAction - ) - ).toBeTruthy(); + html: 'Hello world', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + }, + ], + }; + expect(isMessageDeleted(reportAction)).toBeTruthy(); }) it('should return true if there is property isDeletedParentAction inside original message', () => { - expect( - isMessageDeleted( + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + isDeletedParentAction: true + }, + message: [ { - originalMessage: { - isDeletedParentAction: true, - type: "COMMENT", - text: "", - }, - reportActionID: "reportActionID", - actionName: "ACTIONABLEADDPAYMENTCARD", - created: "someDate" - } as ReportAction - ) - ).toBeTruthy(); + html: 'Hello world', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + }, + ], + }; + expect(isMessageDeleted(reportAction)).toBeTruthy(); }) it('should return false if there are no deletion properties in message and originalMessage', () => { - expect( - isMessageDeleted( + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [] + }, + message: [ { - originalMessage: { - type: "COMMENT", - text: "", - }, - reportActionID: "reportActionID", - actionName: "ACTIONABLEADDPAYMENTCARD", - created: "someDate" - } as ReportAction - ) - ).toBeFalsy(); + html: 'Hello world', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + }, + ], + }; + expect(isMessageDeleted(reportAction)).toBeFalsy(); }) }) @@ -822,14 +844,14 @@ describe('ReportActionsUtils', () => { created: '1', message: [ { - type: 'COMMENT', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, text: '', - deleted: 'someDate' + deleted: '2025-05-12 17:27:01.825' } ], }; const expectedFragments = ReportActionsUtils.getReportActionMessageFragments(action); - expect(expectedFragments).toEqual([{text: '', type: 'COMMENT', isDeletedParentAction: true, deleted: 'someDate'}]); + expect(expectedFragments).toEqual([{text: '', type: CONST.REPORT.MESSAGE.TYPE.COMMENT, isDeletedParentAction: true, deleted: '2025-05-12 17:27:01.825'}]); }); }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index bedc7e9ee13f..c65d63a205e9 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -445,30 +445,49 @@ describe('ReportUtils', () => { }); test('with deleted message and provided parent action param', () => { - expect( - getReportName({ - type: CONST.REPORT.TYPE.CHAT, - parentReportActionID: "id", - parentReportID: "id", - reportID: "id" - }, - undefined, - { message: [{ deleted: "someDate", type: "COMMENT", text: "" }] } as ReportAction), - ).toEqual(translateLocal('parentReportAction.deletedMessage')); + const report = { + type: CONST.REPORT.TYPE.CHAT, + reportID: '4401445780099175', + parentReportActionID: '8401445780099176', + parentReportID: '4401445780099175' + } + const reportAction = { + created: '2025-05-12 17:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + message: [ + { + deleted: '2025-05-12 17:27:01.825', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + } + ] + } + expect(getReportName(report, undefined, reportAction)).toEqual(translateLocal('parentReportAction.deletedMessage')); }); test('with deleted message and not provided parent action param', async () => { - const reportAction = { reportActionID: "reportActionID", parentReportID: "parentID", message: [{ deleted: "someDate", type: "COMMENT", text: "" }] } - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportAction.parentReportID}`, { [reportAction.reportActionID]: reportAction as any} ) - expect( - getReportName({ - type: CONST.REPORT.TYPE.CHAT, - parentReportActionID: "reportActionID", - parentReportID: "parentID", - reportID: "id" - })).toEqual(translateLocal('parentReportAction.deletedMessage')); - - + const report = { + type: CONST.REPORT.TYPE.CHAT, + parentReportActionID: '8401445780099176', + parentReportID: '4401445780099175', + reportID: '2401445780099174' + } + const reportAction = { + reportActionID: '8401445780099176', + parentReportID: '', + message: [ + { + deleted: '2025-05-12 17:27:01.825', + type: CONST.REPORT.MESSAGE.TYPE.COMMENT, + text: '' + } + ] + } + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, { + [reportAction.reportActionID]: reportAction + }) + expect(getReportName(report)).toEqual(translateLocal('parentReportAction.deletedMessage')); }); test('SMS', () => {