Skip to content

Commit

Permalink
Merge pull request #1781 from Expensify/tgolen-fix-lhn-sync
Browse files Browse the repository at this point in the history
Only update lastMessageText from pusher event from other users
  • Loading branch information
marcaaron authored Mar 16, 2021
2 parents c256ccc + 90c81f8 commit ac813fa
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -257,14 +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
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
const updatedReportObject = {
reportID,
unreadActionCount: newMaxSequenceNumber - (lastReadSequenceNumbers[reportID] || 0),
unreadActionCount: newMaxSequenceNumber - lastReadSequenceNumber,
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 > lastReadSequenceNumber) {
updatedReportObject.lastMessageTimestamp = reportAction.timestamp;
updatedReportObject.lastMessageText = messageText;
updatedReportObject.lastActorEmail = reportAction.actorEmail;
}

Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedReportObject);

const reportActionsToMerge = {};
if (reportAction.clientID) {
Expand Down Expand Up @@ -576,17 +584,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
Expand Down Expand Up @@ -629,11 +644,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,
Expand All @@ -645,7 +657,7 @@ function addAction(reportID, text, file) {

API.Report_AddComment({
reportID,
reportComment: htmlComment,
reportComment: htmlForNewComment,
file,
clientID: optimisticReportActionID,

Expand Down

0 comments on commit ac813fa

Please sign in to comment.