From 28cf9056ea37e6bdc600518a224dccf092b262d1 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 15 Mar 2021 15:01:35 -0600 Subject: [PATCH 1/2] Only update lastMessageText from pusher event --- src/libs/actions/Report.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3a746aef0156..7b70ad8821a5 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -257,14 +257,21 @@ function updateReportWithNewAction(reportID, reportAction) { // Always merge the reportID into Onyx // If the report doesn't exist in Onyx yet, then all the rest of the data will be filled out // by handleReportChanged - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { + const updatedAction = { reportID, unreadActionCount: newMaxSequenceNumber - (lastReadSequenceNumbers[reportID] || 0), maxSequenceNumber: reportAction.sequenceNumber, - lastMessageTimestamp: reportAction.timestamp, - lastMessageText: messageText, - lastActorEmail: reportAction.actorEmail, - }); + }; + + // If the report action from pusher is a higher sequence number than we know about (meaning it has come from + // a chat participant in another application), then the last message text and author needs to be updated as well + if (newMaxSequenceNumber > (lastReadSequenceNumbers[reportID] || 0)) { + updatedAction.lastMessageTimestamp = reportAction.timestamp; + updatedAction.lastMessageText = messageText; + updatedAction.lastActorEmail = reportAction.actorEmail; + } + + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedAction); const reportActionsToMerge = {}; if (reportAction.clientID) { @@ -576,17 +583,24 @@ function fetchAll(shouldRedirectToReport = true, shouldRecordHomePageTiming = fa function addAction(reportID, text, file) { // Convert the comment from MD into HTML because that's how it is stored in the database const parser = new ExpensiMark(); - const htmlComment = parser.replace(text); + const commentText = parser.replace(text); const isAttachment = _.isEmpty(text) && file !== undefined; // The new sequence number will be one higher than the highest const highestSequenceNumber = reportMaxSequenceNumbers[reportID] || 0; const newSequenceNumber = highestSequenceNumber + 1; + const htmlForNewComment = isAttachment ? 'Uploading Attachment...' : commentText; + + // Remove HTML from text when applying optimistic offline comment + const textForNewComment = isAttachment ? '[Attachment]' + : htmlForNewComment.replace(/<[^>]*>?/gm, ''); // Update the report in Onyx to have the new sequence number Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { maxSequenceNumber: newSequenceNumber, lastMessageTimestamp: moment().unix(), + lastMessageText: textForNewComment, + lastActorEmail: currentUserEmail, }); // Generate a clientID so we can save the optimistic action to storage with the clientID as key. Later, we will @@ -629,11 +643,8 @@ function addAction(reportID, text, file) { message: [ { type: 'COMMENT', - html: isAttachment ? 'Uploading Attachment...' : htmlComment, - - // Remove HTML from text when applying optimistic offline comment - text: isAttachment ? '[Attachment]' - : htmlComment.replace(/<[^>]*>?/gm, ''), + html: htmlForNewComment, + text: textForNewComment, }, ], isFirstItem: false, @@ -645,7 +656,7 @@ function addAction(reportID, text, file) { API.Report_AddComment({ reportID, - reportComment: htmlComment, + reportComment: htmlForNewComment, file, clientID: optimisticReportActionID, From 90c81f854b55d5e416b236b9fad4f544c5ad6ebd Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 16 Mar 2021 09:12:10 -0600 Subject: [PATCH 2/2] Update variable name and DRY up some logic --- src/libs/actions/Report.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 7b70ad8821a5..47af1083b7c9 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -244,6 +244,7 @@ function removeOptimisticActions(reportID) { function updateReportWithNewAction(reportID, reportAction) { const newMaxSequenceNumber = reportAction.sequenceNumber; const isFromCurrentUser = reportAction.actorAccountID === currentUserAccountID; + const lastReadSequenceNumber = lastReadSequenceNumbers[reportID] || 0; // When handling an action from the current users we can assume that their // last read actionID has been updated in the server but not necessarily reflected @@ -257,21 +258,21 @@ function updateReportWithNewAction(reportID, reportAction) { // Always merge the reportID into Onyx // If the report doesn't exist in Onyx yet, then all the rest of the data will be filled out // by handleReportChanged - const updatedAction = { + const updatedReportObject = { reportID, - unreadActionCount: newMaxSequenceNumber - (lastReadSequenceNumbers[reportID] || 0), + unreadActionCount: newMaxSequenceNumber - lastReadSequenceNumber, maxSequenceNumber: reportAction.sequenceNumber, }; // If the report action from pusher is a higher sequence number than we know about (meaning it has come from // a chat participant in another application), then the last message text and author needs to be updated as well - if (newMaxSequenceNumber > (lastReadSequenceNumbers[reportID] || 0)) { - updatedAction.lastMessageTimestamp = reportAction.timestamp; - updatedAction.lastMessageText = messageText; - updatedAction.lastActorEmail = reportAction.actorEmail; + if (newMaxSequenceNumber > lastReadSequenceNumber) { + updatedReportObject.lastMessageTimestamp = reportAction.timestamp; + updatedReportObject.lastMessageText = messageText; + updatedReportObject.lastActorEmail = reportAction.actorEmail; } - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedAction); + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedReportObject); const reportActionsToMerge = {}; if (reportAction.clientID) {