diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 058c90d1aa61..515101b5665f 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -510,6 +510,19 @@ function navigateToDetailsPage(report) { Navigation.navigate(ROUTES.getReportParticipantsRoute(report.reportID)); } +/** + * Generate a random reportID between 98000000 (the number of reports before the switch from sequential to random) + * and the maximum safe integer of js (53 bits aka 9,007,199,254,740,991) + * + * In a test of 500M reports (28 years of reports at our current max rate) we got 20-40 collisions meaning that + * this is more than random enough for our needs. + * + * @returns {Number} + */ +function generateReportID() { + return Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - 98000000)) + 98000000; +} + export { getReportParticipantsTitle, isReportMessageAttachment, @@ -538,4 +551,5 @@ export { getDisplayNamesWithTooltips, getReportName, navigateToDetailsPage, + generateReportID, }; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c14279085fbf..5645e24dd2cf 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -820,6 +820,39 @@ function fetchAllReports( }); } +/** + * Creates an optimistic report with a randomly generated reportID and as much information as we currently have + * + * @param {Array} participantList + * @returns {Object} + */ +function createOptimisticReport(participantList) { + return { + chatType: '', + hasOutstandingIOU: false, + isOwnPolicyExpenseChat: false, + isPinned: false, + lastActorEmail: '', + lastMessageHtml: '', + lastMessageText: null, + lastReadSequenceNumber: undefined, + lastMessageTimestamp: 0, + lastVisitedTimestamp: 0, + maxSequenceNumber: 0, + notificationPreference: '', + oldPolicyName: '', + ownerEmail: '__FAKE__', + participants: participantList, + policyID: '_FAKE_', + reportID: ReportUtils.generateReportID(), + reportName: 'Chat Report', + stateNum: 0, + statusNum: 0, + unreadActionCount: 0, + visibility: undefined, + }; +} + /** * @param {Number} reportID * @param {String} [text] @@ -1663,4 +1696,5 @@ export { readNewestAction, openReport, openPaymentDetailsPage, + createOptimisticReport, };