From bd0d326ddafc7a858d1bfc862bf29cf0417200a6 Mon Sep 17 00:00:00 2001 From: Eric Han <117511920+eh2077@users.noreply.github.com> Date: Sat, 6 Dec 2025 12:04:12 +0800 Subject: [PATCH 1/4] fix: selfDM appears briefly when open a new chat --- .../Middleware/HandleUnusedOptimisticID.ts | 44 +++++++++++++++++++ src/libs/actions/Report.ts | 28 +++++++----- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 47cd7c844e9b..312dfd42e016 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -4,6 +4,14 @@ import type {Middleware} from '@libs/Request'; import * as PersistedRequests from '@userActions/PersistedRequests'; import ONYXKEYS from '@src/ONYXKEYS'; import type Report from '@src/types/onyx/Report'; +import { prepareOnyxDataForCleanUpOptimisticParticipants } from '@libs/actions/Report'; +import Onyx, { OnyxEntry } from 'react-native-onyx'; +import { PersonalDetailsList } from '@src/types/onyx'; +import { WRITE_COMMANDS } from '@libs/API/types'; +import { isEmptyObject } from '@src/types/utils/EmptyObject'; + +// Local cache of reportID to optimistic Onyx data +const reportOptimisticData = new Map; redundantParticipants: Record } | undefined>(); /** * This middleware checks for the presence of a field called preexistingReportID in the response. @@ -17,6 +25,15 @@ import type Report from '@src/types/onyx/Report'; */ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSequentialQueue) => requestResponse.then((response) => { + const currentRequestReportID = request?.data?.reportID as string; + const isCreatingNewReport = request?.command === WRITE_COMMANDS.OPEN_REPORT && !!request?.data?.createdReportActionID && !!currentRequestReportID; + if (isCreatingNewReport) { + // We're opening a new report, which can be a new or preexisting report + // For new report, clean up optimistic data after this request returned successfully + // For report redirect a preexisting repoort, clean up optimistic data after the request of preexisting report returned successfully + reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID)); + } + const responseOnyxData = response?.onyxData ?? []; for (const onyxData of responseOnyxData) { const key = onyxData.key; @@ -35,6 +52,12 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe } const oldReportID = key.split(ONYXKEYS.COLLECTION.REPORT).at(-1) ?? request.data?.reportID ?? request.data?.optimisticReportID; + if (isCreatingNewReport && currentRequestReportID === oldReportID) { + // move optimistic data to preexisting reportID, so it'll be cleaned up after preexisting report is returned successfully + reportOptimisticData.set(preexistingReportID, reportOptimisticData.get(currentRequestReportID)); + reportOptimisticData.delete(currentRequestReportID); + } + if (isFromSequentialQueue) { const ongoingRequest = PersistedRequests.getOngoingRequest(); const ongoingRequestReportIDParam = ongoingRequest?.data?.reportID ?? ongoingRequest?.data?.optimisticReportID; @@ -51,6 +74,27 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe PersistedRequests.update(index, persistedRequestClone); } } + + if (!!currentRequestReportID && request?.command === WRITE_COMMANDS.OPEN_REPORT && !!response?.onyxData && reportOptimisticData.has(currentRequestReportID)) { + const { settledPersonalDetails, redundantParticipants } = reportOptimisticData.get(currentRequestReportID) ?? {}; + reportOptimisticData.delete(currentRequestReportID); + if (!isEmptyObject(settledPersonalDetails) && !isEmptyObject(redundantParticipants)) { + response.onyxData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${currentRequestReportID}`, + value: { + participants: redundantParticipants, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: redundantParticipants, + } + ); + } + } return response; }); diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 679b03b0e849..4e0c8d19f8e3 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1289,7 +1289,6 @@ function openReport( // Add optimistic personal details for new participants const optimisticPersonalDetails: OnyxEntry = {}; const settledPersonalDetails: OnyxEntry = {}; - const redundantParticipants: Record = {}; const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins(participantLoginList); for (const [index, login] of participantLoginList.entries()) { const accountID = participantAccountIDs.at(index) ?? -1; @@ -1306,9 +1305,6 @@ function openReport( isOptimisticPersonalDetail: true, }; settledPersonalDetails[accountID] = null; - - // BE will send different participants. We clear the optimistic ones to avoid duplicated entries - redundantParticipants[accountID] = null; } successData.push( @@ -1316,7 +1312,6 @@ function openReport( onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - participants: redundantParticipants, pendingFields: { createChat: null, reportName: null, @@ -1340,11 +1335,6 @@ function openReport( key: ONYXKEYS.PERSONAL_DETAILS_LIST, value: optimisticPersonalDetails, }); - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: settledPersonalDetails, - }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, @@ -1392,6 +1382,23 @@ function openReport( } } +function prepareOnyxDataForCleanUpOptimisticParticipants(preexistingReportID: string): { settledPersonalDetails: OnyxEntry; redundantParticipants: Record } | undefined { + const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`]; + if (!existingReport?.participants) { + return undefined; + } + const settledPersonalDetails: OnyxEntry = {}; + const redundantParticipants: Record = {}; + for (const accountID in existingReport.participants) { + if (!allPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) { + continue; + } + settledPersonalDetails[accountID] = null; + redundantParticipants[accountID] = null; + } + return { settledPersonalDetails, redundantParticipants }; +} + /** * This will return an optimistic report object for a given user we want to create a chat with without saving it, when the only thing we know about recipient is his accountID. * * @param accountID accountID of the user that the optimistic chat report is created with. @@ -6348,4 +6355,5 @@ export { openUnreportedExpense, optimisticReportLastData, setOptimisticTransactionThread, + prepareOnyxDataForCleanUpOptimisticParticipants, }; From 1269a70920007e4919b9cbf2f35037ba44086de8 Mon Sep 17 00:00:00 2001 From: Eric Han <117511920+eh2077@users.noreply.github.com> Date: Fri, 19 Dec 2025 22:48:36 +0800 Subject: [PATCH 2/4] fix ci and add unit tests --- .../Middleware/HandleUnusedOptimisticID.ts | 18 +- src/libs/actions/Report.ts | 8 +- tests/unit/MiddlewareTest.ts | 214 +++++++++++++++++- 3 files changed, 228 insertions(+), 12 deletions(-) diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 312dfd42e016..46f0ea82642b 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -1,17 +1,18 @@ import clone from 'lodash/clone'; +import type {OnyxEntry} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; +import {prepareOnyxDataForCleanUpOptimisticParticipants} from '@libs/actions/Report'; +import {WRITE_COMMANDS} from '@libs/API/types'; import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues'; import type {Middleware} from '@libs/Request'; import * as PersistedRequests from '@userActions/PersistedRequests'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {PersonalDetailsList} from '@src/types/onyx'; import type Report from '@src/types/onyx/Report'; -import { prepareOnyxDataForCleanUpOptimisticParticipants } from '@libs/actions/Report'; -import Onyx, { OnyxEntry } from 'react-native-onyx'; -import { PersonalDetailsList } from '@src/types/onyx'; -import { WRITE_COMMANDS } from '@libs/API/types'; -import { isEmptyObject } from '@src/types/utils/EmptyObject'; +import {isEmptyObject} from '@src/types/utils/EmptyObject'; // Local cache of reportID to optimistic Onyx data -const reportOptimisticData = new Map; redundantParticipants: Record } | undefined>(); +const reportOptimisticData = new Map; redundantParticipants: Record} | undefined>(); /** * This middleware checks for the presence of a field called preexistingReportID in the response. @@ -76,7 +77,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe } if (!!currentRequestReportID && request?.command === WRITE_COMMANDS.OPEN_REPORT && !!response?.onyxData && reportOptimisticData.has(currentRequestReportID)) { - const { settledPersonalDetails, redundantParticipants } = reportOptimisticData.get(currentRequestReportID) ?? {}; + const {settledPersonalDetails, redundantParticipants} = reportOptimisticData.get(currentRequestReportID) ?? {}; reportOptimisticData.delete(currentRequestReportID); if (!isEmptyObject(settledPersonalDetails) && !isEmptyObject(redundantParticipants)) { response.onyxData.push( @@ -91,8 +92,9 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PERSONAL_DETAILS_LIST, value: redundantParticipants, - } + }, ); + return Promise.resolve(response); } } return response; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index cbd55cbabf8b..e504d0399649 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1374,8 +1374,10 @@ function openReport( } } -function prepareOnyxDataForCleanUpOptimisticParticipants(preexistingReportID: string): { settledPersonalDetails: OnyxEntry; redundantParticipants: Record } | undefined { - const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`]; +function prepareOnyxDataForCleanUpOptimisticParticipants( + reportID: string, +): {settledPersonalDetails: OnyxEntry; redundantParticipants: Record} | undefined { + const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; if (!existingReport?.participants) { return undefined; } @@ -1388,7 +1390,7 @@ function prepareOnyxDataForCleanUpOptimisticParticipants(preexistingReportID: st settledPersonalDetails[accountID] = null; redundantParticipants[accountID] = null; } - return { settledPersonalDetails, redundantParticipants }; + return {settledPersonalDetails, redundantParticipants}; } /** diff --git a/tests/unit/MiddlewareTest.ts b/tests/unit/MiddlewareTest.ts index 5f957b1a2253..f535f43fb412 100644 --- a/tests/unit/MiddlewareTest.ts +++ b/tests/unit/MiddlewareTest.ts @@ -1,4 +1,6 @@ import Onyx from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; +import SaveResponseInOnyx from '@libs/Middleware/SaveResponseInOnyx'; import HttpUtils from '@src/libs/HttpUtils'; import handleUnusedOptimisticID from '@src/libs/Middleware/HandleUnusedOptimisticID'; import * as MainQueue from '@src/libs/Network/MainQueue'; @@ -6,8 +8,9 @@ import * as NetworkStore from '@src/libs/Network/NetworkStore'; import * as SequentialQueue from '@src/libs/Network/SequentialQueue'; import * as Request from '@src/libs/Request'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report as OnyxReport} from '@src/types/onyx'; +import type {Report as OnyxReport, PersonalDetailsList} from '@src/types/onyx'; import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import waitForNetworkPromises from '../utils/waitForNetworkPromises'; type FormDataObject = {body: TestHelper.FormData}; @@ -217,5 +220,214 @@ describe('Middleware', () => { expect(global.fetch).toHaveBeenNthCalledWith(1, 'https://www.expensify.com.dev/api/MoveIOUReportToExistingPolicy?', expect.anything()); TestHelper.assertFormDataMatchesObject({optimisticReportID: '1234'} as unknown as OnyxReport, ((global.fetch as jest.Mock).mock.calls.at(0) as FormDataObject[]).at(1)?.body); }); + + test('OpenReport to a chat with preexistingReportID and clean up optimistic participant data', async () => { + const optimisticReportID = '1234'; + const preexistingReportID = '5555'; + const optimisticAccountID = 999; + const preexistingAccountID = 333; + await Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}` as const]: { + reportID: optimisticReportID, + participants: {[optimisticAccountID]: {notificationPreference: 'always'}}, + }, + [ONYXKEYS.PERSONAL_DETAILS_LIST]: { + [optimisticAccountID]: { + accountID: optimisticAccountID, + isOptimisticPersonalDetail: true, + }, + }, + }); + + Request.addMiddleware(handleUnusedOptimisticID); + Request.addMiddleware(SaveResponseInOnyx); + + const requests = [ + { + command: 'OpenReport', + data: {authToken: 'testToken', reportID: optimisticReportID, createdReportActionID: '5678'}, + }, + { + command: 'OpenReport', + data: {authToken: 'testToken', reportID: preexistingReportID}, + }, + ]; + for (const request of requests) { + SequentialQueue.push(request); + } + + // eslint-disable-next-line @typescript-eslint/require-await + (global.fetch as jest.Mock) + .mockImplementationOnce(async () => ({ + ok: true, + // eslint-disable-next-line @typescript-eslint/require-await + json: async () => ({ + jsonCode: 200, + onyxData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`, + value: { + preexistingReportID, + }, + }, + ], + }), + })) + .mockImplementationOnce(async () => ({ + ok: true, + // eslint-disable-next-line @typescript-eslint/require-await + json: async () => ({ + jsonCode: 200, + onyxData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`, + value: { + reportID: preexistingReportID, + participants: {[preexistingAccountID]: {notificationPreference: 'always'}}, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [preexistingAccountID]: { + accountID: preexistingAccountID, + }, + }, + }, + ], + }), + })); + + SequentialQueue.unpause(); + await waitForBatchedUpdates(); + + expect(global.fetch).toHaveBeenCalledTimes(2); + + const optimisticReportUpdated = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`, + callback: (report) => { + Onyx.disconnect(connection); + resolve(report); + }, + }); + }); + expect(optimisticReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined(); + + const preexistingReportUpdated = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`, + callback: (report) => { + Onyx.disconnect(connection); + resolve(report); + }, + }); + }); + expect(preexistingReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined(); + expect(preexistingReportUpdated?.participants?.[preexistingAccountID]).not.toBeUndefined(); + + const personalDetails = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + callback: (data) => { + Onyx.disconnect(connection); + resolve(data); + }, + }); + }); + expect(personalDetails?.[optimisticAccountID]).toBeUndefined(); + expect(personalDetails?.[preexistingAccountID]).not.toBeUndefined(); + }); + + test('OpenReport to a new chat without preexistingReportID and clean up optimistic participant data', async () => { + const optimisticReportID = '1234'; + const optimisticAccountID = 999; + const preexistingAccountID = 333; + await Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}` as const]: { + reportID: optimisticReportID, + participants: {[optimisticAccountID]: {notificationPreference: 'always'}}, + }, + [ONYXKEYS.PERSONAL_DETAILS_LIST]: { + [optimisticAccountID]: { + accountID: optimisticAccountID, + isOptimisticPersonalDetail: true, + }, + }, + }); + + Request.addMiddleware(handleUnusedOptimisticID); + Request.addMiddleware(SaveResponseInOnyx); + + const requests = [ + { + command: 'OpenReport', + data: {authToken: 'testToken', reportID: optimisticReportID, createdReportActionID: '5678'}, + }, + ]; + for (const request of requests) { + SequentialQueue.push(request); + } + + // eslint-disable-next-line @typescript-eslint/require-await + (global.fetch as jest.Mock).mockImplementationOnce(async () => ({ + ok: true, + // eslint-disable-next-line @typescript-eslint/require-await + json: async () => ({ + jsonCode: 200, + onyxData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`, + value: { + reportID: optimisticReportID, + participants: {[preexistingAccountID]: {notificationPreference: 'always'}}, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [preexistingAccountID]: { + accountID: preexistingAccountID, + }, + }, + }, + ], + }), + })); + + SequentialQueue.unpause(); + await waitForBatchedUpdates(); + + expect(global.fetch).toHaveBeenCalledTimes(1); + + const optimisticReportUpdated = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`, + callback: (report) => { + Onyx.disconnect(connection); + resolve(report); + }, + }); + }); + expect(optimisticReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined(); + expect(optimisticReportUpdated?.participants?.[preexistingAccountID]).not.toBeUndefined(); + + const personalDetails = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + callback: (data) => { + Onyx.disconnect(connection); + resolve(data); + }, + }); + }); + expect(personalDetails?.[optimisticAccountID]).toBeUndefined(); + expect(personalDetails?.[preexistingAccountID]).not.toBeUndefined(); + }); }); }); From 97ce6bb13a286825c6fb15cee58449fea680cde8 Mon Sep 17 00:00:00 2001 From: Eric Han <117511920+eh2077@users.noreply.github.com> Date: Fri, 19 Dec 2025 22:58:28 +0800 Subject: [PATCH 3/4] fix spellcheck --- src/libs/Middleware/HandleUnusedOptimisticID.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 46f0ea82642b..32f83ac0811b 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -31,7 +31,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe if (isCreatingNewReport) { // We're opening a new report, which can be a new or preexisting report // For new report, clean up optimistic data after this request returned successfully - // For report redirect a preexisting repoort, clean up optimistic data after the request of preexisting report returned successfully + // For report redirect a preexisting report, clean up optimistic data after the request of preexisting report returned successfully reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID)); } From 605fbeab5bcc95113d026602ef28dfdade0333ef Mon Sep 17 00:00:00 2001 From: Eric Han <117511920+eh2077@users.noreply.github.com> Date: Mon, 22 Dec 2025 23:12:09 +0800 Subject: [PATCH 4/4] fix review comment --- src/libs/Middleware/HandleUnusedOptimisticID.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 32f83ac0811b..0f30c98c8cf4 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -94,7 +94,6 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe value: redundantParticipants, }, ); - return Promise.resolve(response); } } return response;