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
17 changes: 10 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2934,19 +2934,22 @@ function isOneOnOneChat(report: OnyxEntry<Report>, currentUserAccountID?: number
* Returns the other participant of a cached 1:1 DM as OpenReport participant info, so the server can
* resolve a stale/optimistic reportID to the real chat (returned as preexistingReportID) instead of
* failing with "Report not found". Returns an empty list for any report that is not a 1:1 DM.
*/
function getOneOnOneChatParticipants(
report: OnyxEntry<Report>,
personalDetails: OnyxEntry<PersonalDetailsList>,
currentUserAccountID: number | undefined,
): Array<{login: string; accountID: number}> {
*
* Only the login is returned, never the accountID. The other participant may be an invited user whose
* accountID was generated locally (see generateAccountID) and does not exist on the server yet, and sending
* that ID in accountIDList makes OpenReport fail with "Email not found". This cannot be gated on
* isOptimisticPersonalDetail because not every flow sets it (sendMoney writes the recipient's optimistic
* detail without it). The login alone is enough for the server to resolve a 1:1 DM, the same way
* navigateToAndOpenReport creates one, so a real accountID adds nothing here.
*/
function getOneOnOneChatParticipants(report: OnyxEntry<Report>, personalDetails: OnyxEntry<PersonalDetailsList>, currentUserAccountID: number | undefined): Array<{login: string}> {
if (!currentUserAccountID || !isOneOnOneChat(report, currentUserAccountID)) {
return [];
}
return Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID)
.map((accountID) => ({login: personalDetails?.[accountID]?.login ?? '', accountID}))
.map((accountID) => ({login: personalDetails?.[accountID]?.login ?? ''}))
.filter((participant) => !!participant.login);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import * as SequentialQueue from '@src/libs/Network/SequentialQueue';
import {setHasRadio} from '@src/libs/NetworkState';
import * as ReportUtils from '@src/libs/ReportUtils';
import type * as SearchQueryUtilsType from '@src/libs/SearchQueryUtils';
import {generateAccountID} from '@src/libs/UserUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
Expand Down Expand Up @@ -5280,6 +5281,38 @@ describe('actions/Report', () => {
accountIDList: '2',
});
});

it('should send an empty accountIDList for a DM with an invited user who has no account yet', async () => {
global.fetch = TestHelper.createGlobalFetchMock();
const REPORT_ID = 'dm2';
const optimisticAccountID = generateAccountID('new@user.com');
const optimisticPersonalDetails: OnyxTypes.PersonalDetailsList = {
[optimisticAccountID]: {accountID: optimisticAccountID, login: 'new@user.com', isOptimisticPersonalDetail: true},
};
const dmReport: OnyxTypes.Report = {
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.CHAT,
policyID: CONST.POLICY.ID_FAKE,
participants: ReportUtils.buildParticipantsFromAccountIDs([1, optimisticAccountID]),
};

Report.openReport({
conciergeChat: undefined,
reportID: REPORT_ID,
introSelected: undefined,
betas: undefined,
hasReportActions: true,
currentUserAccountID: 1,
participants: ReportUtils.getOneOnOneChatParticipants(dmReport, optimisticPersonalDetails, 1),
Comment thread
ahmdshrif marked this conversation as resolved.
Resolved
});
await waitForBatchedUpdates();

TestHelper.expectAPICommandToHaveBeenCalledWith(WRITE_COMMANDS.OPEN_REPORT, 0, {
reportID: REPORT_ID,
emailList: 'new@user.com',
accountIDList: '',
});
});
});

describe('setOptimisticTransactionThread', () => {
Expand Down
33 changes: 31 additions & 2 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ import {
} from '@libs/ReportUtils';
import {buildTransactionsByReportID} from '@libs/TodosUtils';
import {buildOptimisticTransaction} from '@libs/TransactionUtils';
import {generateAccountID} from '@libs/UserUtils';
import ViolationsUtils from '@libs/Violations/ViolationsUtils';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -12101,8 +12102,36 @@ describe('ReportUtils', () => {
participants: buildParticipantsFromAccountIDs([currentUserAccountID, OTHER_ACCOUNT_ID]),
};

it('should return the other participant of a 1:1 DM with their login and accountID', () => {
expect(getOneOnOneChatParticipants(dmReport, personalDetailsList, currentUserAccountID)).toEqual([{login: 'other@test.com', accountID: OTHER_ACCOUNT_ID}]);
it('should return the other participant of a 1:1 DM with their login only, even when they have a real accountID', () => {
expect(getOneOnOneChatParticipants(dmReport, personalDetailsList, currentUserAccountID)).toStrictEqual([{login: 'other@test.com'}]);
});

it('should not send a locally generated accountID for an invited user who has no account yet', () => {
Comment thread
ahmdshrif marked this conversation as resolved.
Resolved
// An invited (brand-new) email gets a client-generated accountID and an optimistic personal detail.
// That accountID does not exist on the server, so only the login may be passed to OpenReport.
const optimisticAccountID = generateAccountID('new@user.com');
const optimisticPersonalDetails: PersonalDetailsList = {
[optimisticAccountID]: {accountID: optimisticAccountID, login: 'new@user.com', isOptimisticPersonalDetail: true},
};
const optimisticDMReport: Report = {
...dmReport,
participants: buildParticipantsFromAccountIDs([currentUserAccountID, optimisticAccountID]),
};
expect(getOneOnOneChatParticipants(optimisticDMReport, optimisticPersonalDetails, currentUserAccountID)).toStrictEqual([{login: 'new@user.com'}]);
});

it('should not send a locally generated accountID when the optimistic detail carries no flag', () => {
Comment thread
ahmdshrif marked this conversation as resolved.
Resolved
// sendMoney (Pay someone) writes the recipient's optimistic detail without
// isOptimisticPersonalDetail - see src/libs/actions/IOU/SendMoney.ts
const optimisticAccountID = generateAccountID('new@user.com');
const unflaggedPersonalDetails: PersonalDetailsList = {
[optimisticAccountID]: {accountID: optimisticAccountID, login: 'new@user.com'},
};
const optimisticDMReport: Report = {
...dmReport,
participants: buildParticipantsFromAccountIDs([currentUserAccountID, optimisticAccountID]),
};
expect(getOneOnOneChatParticipants(optimisticDMReport, unflaggedPersonalDetails, currentUserAccountID)).toStrictEqual([{login: 'new@user.com'}]);
});

it('should return an empty list for reports that are not 1:1 DMs', () => {
Expand Down
Loading