Skip to content
14 changes: 14 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
stitesExpensify marked this conversation as resolved.
*
* 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,
Expand Down Expand Up @@ -538,4 +551,5 @@ export {
getDisplayNamesWithTooltips,
getReportName,
navigateToDetailsPage,
generateReportID,
};
34 changes: 34 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -1663,4 +1696,5 @@ export {
readNewestAction,
openReport,
openPaymentDetailsPage,
createOptimisticReport,
};