Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO QA] Add random reportID generation and method for creating optimistic reports #9991

Merged
merged 9 commits into from
Jul 20, 2022
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)
stitesExpensify marked this conversation as resolved.
Show resolved Hide 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,
};