Skip to content

Commit

Permalink
Refactor viewNewReportAction to use timestamp instead of sequence number
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetlath committed Dec 12, 2022
1 parent b85a5f7 commit 377e9f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ function readNewestAction(reportID, created) {
*
* @param {String} reportID
* @param {String} created
* @param {Number} sequenceNumber
*/
function markCommentAsUnread(reportID, created) {
// We subtract 1 millisecond so that the lastRead is updated to just before this reportAction's created date
Expand Down Expand Up @@ -1235,19 +1234,22 @@ function subscribeToNewActionEvent(reportID, callback) {
* @param {Object} action
*/
function viewNewReportAction(reportID, action) {
const report = allReports[reportID];
const isFromCurrentUser = action.actorAccountID === currentUserAccountID;
const updatedReportObject = {};

// When handling an action from the current user we can assume that their last read actionID has been updated in the server,
// but not necessarily reflected locally so we will update the lastReadSequenceNumber to mark the report as read.
updatedReportObject.maxSequenceNumber = action.sequenceNumber;
// When handling an action from the current user we can assume that their lastReadMessage has been updated in the server,
// but not necessarily reflected locally so we will update the lastMessageTimestamp to mark the report as read.
if (report.lastMessageTimestamp < action.timestamp) {
updatedReportObject.lastMessageTimestamp = action.timestamp;
}

if (isFromCurrentUser) {
updatedReportObject.lastReadTimestamp = Date.now();
updatedReportObject.lastReadSequenceNumber = action.sequenceNumber;
}

if (reportID === newActionSubscriber.reportID) {
newActionSubscriber.callback(isFromCurrentUser, updatedReportObject.maxSequenceNumber);
newActionSubscriber.callback(isFromCurrentUser);
}

Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedReportObject);
Expand Down
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 @@ -152,7 +152,7 @@ export default [
successIcon: Expensicons.Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
onPress: (closePopover, {reportAction, reportID}) => {
Report.markCommentAsUnread(reportID, reportAction.created, reportAction.sequenceNumber);
Report.markCommentAsUnread(reportID, reportAction.created);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/actions/ReportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe('actions/Report', () => {
expect(ReportUtils.isUnread(report)).toBe(false);

// When the user manually marks a message as "unread"
Report.markCommentAsUnread(REPORT_ID, reportActionCreatedDate, 1);
Report.markCommentAsUnread(REPORT_ID, reportActionCreatedDate);
return waitForPromisesToResolve();
})
.then(() => {
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('actions/Report', () => {
expect(ReportUtils.isUnread(report)).toBe(false);

// When the user manually marks a message as "unread"
Report.markCommentAsUnread(REPORT_ID, reportActionCreatedDate, 3);
Report.markCommentAsUnread(REPORT_ID, reportActionCreatedDate);
return waitForPromisesToResolve();
})
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/UnreadIndicatorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('Unread Indicators', () => {
.then(() => {
// It's difficult to trigger marking a report comment as unread since we would have to mock the long press event and then
// another press on the context menu item so we will do it via the action directly and then test if the UI has updated properly
Report.markCommentAsUnread(REPORT_ID, reportAction3CreatedDate, 3);
Report.markCommentAsUnread(REPORT_ID, reportAction3CreatedDate);
return waitForPromisesToResolve();
})
.then(() => {
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('Unread Indicators', () => {
expect(unreadIndicator).toHaveLength(0);

// Mark a previous comment as unread and verify the unread action indicator returns
Report.markCommentAsUnread(REPORT_ID, reportAction9CreatedDate, 9);
Report.markCommentAsUnread(REPORT_ID, reportAction9CreatedDate);
return waitForPromisesToResolve();
})
.then(() => {
Expand Down

0 comments on commit 377e9f0

Please sign in to comment.