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

Refactor Update Last Read #9529

Merged
merged 9 commits into from
Jul 9, 2022
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
100 changes: 94 additions & 6 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ function getUnreadActionCount(report) {
return Math.max(0, unreadActionCount);
}

/**
* Returns the number of unread actions for a reportID
*
* @param {Number} reportID
* @param {Number} sequenceNumber
* @returns {Number}
*/
function getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber) {
const reportMaxSequenceNumber = reportMaxSequenceNumbers[reportID];

// Determine the number of unread actions by deducting the last read sequence from the total. If, for some reason,
// the last read sequence is higher than the actual last sequence, let's just assume all actions are read
return Math.max(reportMaxSequenceNumber - sequenceNumber - ReportActions.getDeletedCommentsCount(reportID, sequenceNumber), 0);
marcaaron marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {Object} report
* @return {String[]}
Expand Down Expand Up @@ -409,15 +424,10 @@ function setLocalIOUReportData(iouReportObject) {
*/
function setLocalLastRead(reportID, lastReadSequenceNumber) {
lastReadSequenceNumbers[reportID] = lastReadSequenceNumber;
const reportMaxSequenceNumber = reportMaxSequenceNumbers[reportID];

// Determine the number of unread actions by deducting the last read sequence from the total. If, for some reason,
// the last read sequence is higher than the actual last sequence, let's just assume all actions are read
const unreadActionCount = Math.max(reportMaxSequenceNumber - lastReadSequenceNumber - ReportActions.getDeletedCommentsCount(reportID, lastReadSequenceNumber), 0);

// Update the report optimistically.
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
unreadActionCount,
unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, lastReadSequenceNumber),
lastVisitedTimestamp: Date.now(),
});
}
Expand Down Expand Up @@ -1084,6 +1094,81 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false
});
}

/**
* Gets the latest page of report actions and updates the last read message
*
* @param {Number} reportID
*/
function openReport(reportID) {
const sequenceNumber = reportMaxSequenceNumbers[reportID];
marcaaron marked this conversation as resolved.
Show resolved Hide resolved
lastReadSequenceNumbers[reportID] = sequenceNumber;
API.write('OpenReport',
{
reportID,
sequenceNumber,
},
{
optimisticData: [{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
lastVisitedTimestamp: Date.now(),
madmax330 marked this conversation as resolved.
Show resolved Hide resolved
unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber),
},
}],
});
}

/**
* Marks the new report actions as read
*
* @param {Number} reportID
*/
function readNewestAction(reportID) {
const sequenceNumber = reportMaxSequenceNumbers[reportID];
lastReadSequenceNumbers[reportID] = sequenceNumber;
API.write('ReadNewestAction',
{
reportID,
sequenceNumber,
},
{
optimisticData: [{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
lastVisitedTimestamp: Date.now(),
unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber),
},
}],
});
}

/**
* Sets the last read comment on a report
*
* @param {Number} reportID
* @param {Number} sequenceNumber
*/
function markCommentAsUnread(reportID, sequenceNumber) {
lastReadSequenceNumbers[reportID] = sequenceNumber;
API.write('MarkCommentAsUnread',
{
reportID,
sequenceNumber,
},
{
optimisticData: [{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
lastVisitedTimestamp: Math.round(Date.now() / 1000),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB and sorry, as mentioned in that other comment I was wrong about this and Date.now() is totally fine to use as it returns milliseconds and the lastVisitedTimestamp should be in ms.

unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber),
},
}],
});
}

/**
* Toggles the pinned state of the report.
*
Expand Down Expand Up @@ -1535,4 +1620,7 @@ export {
renameReport,
getLastReadSequenceNumber,
setIsComposerFullSize,
markCommentAsUnread,
readNewestAction,
openReport,
};
2 changes: 1 addition & 1 deletion src/pages/home/report/ContextMenu/ContextMenuActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default [
successIcon: Expensicons.Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
onPress: (closePopover, {reportAction, reportID}) => {
Report.updateLastReadActionID(reportID, reportAction.sequenceNumber, true);
madmax330 marked this conversation as resolved.
Show resolved Hide resolved
Report.markCommentAsUnread(reportID, reportAction.sequenceNumber);
Report.setNewMarkerPosition(reportID, reportAction.sequenceNumber);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
Expand Down
10 changes: 5 additions & 5 deletions src/pages/home/report/ReportActionsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class ReportActionsView extends React.Component {
return;
}

Report.updateLastReadActionID(this.props.reportID);
Report.openReport(this.props.reportID);
});

// If the reportID is not found then we have either not loaded this chat or the user is unable to access it.
Expand Down Expand Up @@ -201,6 +201,7 @@ class ReportActionsView extends React.Component {

componentDidUpdate(prevProps) {
if (prevProps.network.isOffline && !this.props.network.isOffline) {
Report.openReport(this.props.reportID);
this.fetchData();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting: Once the report actions fetching stuff is refactored this call should not be here anymore. cc @sketchydroide

}

Expand Down Expand Up @@ -242,14 +243,14 @@ class ReportActionsView extends React.Component {
// When the last action changes, record the max action
// This will make the NEW marker line go away if you receive comments in the same chat you're looking at
if (shouldRecordMaxAction) {
Report.updateLastReadActionID(this.props.reportID);
Report.readNewestAction(this.props.reportID);
}
}

// Update the new marker position and last read action when we are closing the sidebar or moving from a small to large screen size
if (shouldRecordMaxAction && reportBecomeVisible) {
this.updateNewMarkerPosition(this.props.report.unreadActionCount);
Report.updateLastReadActionID(this.props.reportID);
Report.openReport(this.props.reportID);
}
}

Expand Down Expand Up @@ -301,7 +302,6 @@ class ReportActionsView extends React.Component {
*/
scrollToBottomAndUpdateLastRead() {
ReportScrollManager.scrollToBottom();
Report.updateLastReadActionID(this.props.reportID);
}

/**
Expand Down Expand Up @@ -353,7 +353,7 @@ class ReportActionsView extends React.Component {

// Only mark as read if the report is open
if (!this.props.isDrawerOpen) {
Report.updateLastReadActionID(this.props.reportID);
Report.openReport(this.props.reportID);
}
}

Expand Down