App icon indicating copy to clipboard operation
App copied to clipboard

[HOLD for payment 2021-12-13] [HOLD for payment 2021-12-06] After deleting the last message, editing the next last message doesn't update the message displayed in LHN

Open dklymenk opened this issue 5 years ago • 57 comments

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


Action Performed:

  1. Send 2 messages (message 1 and message 2) in a chat
  2. Delete the last message (message 2)
  3. Edit the next last message (message 1)

Expected Result:

The LHN should be updated after editing last message regardless of your previous actions

Actual Result:

The LHN is not updated

Workaround:

Sending a new message and editing it updates LHN properly

Platform:

Where is this issue occurring?

Web :heavy_check_mark: iOS :heavy_check_mark: Android :heavy_check_mark: Desktop App :heavy_check_mark: Mobile Web :heavy_check_mark:

Proposal:

The issue is caused by unreliable values in reportMaxSequenceNumbers in src/libs/actions/Report.js. This array is not updated when you delete a message.

Here is how I would rewrite updateReportActionMessage to update the reportMaxSequenceNumbers.

function updateReportActionMessage(reportID, sequenceNumber, message) {
   const actionToMerge = {};
   actionToMerge[sequenceNumber] = {message: [message]};
   Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionToMerge);

   // If this is the most recent message
   if (sequenceNumber === reportMaxSequenceNumbers[reportID]) {
       // If message is deleted we need to update maxSequenceNumber
       if (message.html === '') {
           const connectionID = Onyx.connect({
               key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
               callback: (reportActionList) => {
                   // To make sure the callback is only called once
                   Onyx.disconnect(connectionID);

                   // Find last non-empty message
                   const lastReportAction = !_.isEmpty(reportActionList)
                       ? _.find(Object.values(reportActionList).reverse(),
                           reportAction => _.last(reportAction.message).html !== ''
                           && reportAction.created !== 'CREATED')
                       : null;

                   Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
                       maxSequenceNumber: lastReportAction.sequenceNumber,
                   });
               },
           });
       } else {
           // Update the lastMessageText in the report object
           Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
               lastMessageText: message.html,
           });
       }
   }
}

I would also need to check how other functions across the app interact with maxSequenceNumber to make sure reducing it doesn't break anything.

Version Number: 1.0.68-4 Logs: https://stackoverflow.com/c/expensify/questions/4856 Notes/Photos/Videos: Any additional supporting documentation Expensify/Expensify Issue URL:

View all open jobs on Upwork

dklymenk avatar Jun 24 '21 15:06 dklymenk

Triggered auto assignment to @strepanier03 (AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.

MelvinBot avatar Jun 24 '21 15:06 MelvinBot

A note that this issue is related to the changes made in this PR: https://github.com/Expensify/Expensify.cash/pull/3526

NikkiWines avatar Jun 24 '21 23:06 NikkiWines

Is there any way to push this one forward? It's blocking PR #3526 and all the code is already there, I'm just waiting for a green light to go ahead with the implementation.

dklymenk avatar Jul 02 '21 10:07 dklymenk

So this is related to changes in #3526 which NikkiWines mentioned above. So I think you are good to go here to submit the PR for this. As this part of old PR.

parasharrajat avatar Jul 02 '21 10:07 parasharrajat

But if you are waiting for your solution to be reviewed then waiting is good.

parasharrajat avatar Jul 02 '21 10:07 parasharrajat

Hmm, it looks like @strepanier03 is out-of-office currently. Going to add the External label so this issue can be triaged appropriately

NikkiWines avatar Jul 02 '21 18:07 NikkiWines

Triggered auto assignment to @mallenexpensify (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.

MelvinBot avatar Jul 02 '21 18:07 MelvinBot

Everything looks good to me, I'm going to assign an engineer to review.

mallenexpensify avatar Jul 02 '21 19:07 mallenexpensify

Triggered auto assignment to @iwiznia (Exported), see https://stackoverflow.com/c/expensify/questions/7972 for more details.

MelvinBot avatar Jul 02 '21 20:07 MelvinBot

Job posted https://www.upwork.com/jobs/~01a6f9be9af612f0cd @iwiznia please review the proposal then assign to to @dklymenk

mallenexpensify avatar Jul 02 '21 20:07 mallenexpensify

Triggered auto assignment to @puneetlath (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.

MelvinBot avatar Jul 02 '21 20:07 MelvinBot

@puneetlath assigned to you via the External label because I'm out next week. Leaving myself on assignment too, can pick up when back if needed

mallenexpensify avatar Jul 02 '21 21:07 mallenexpensify

That proposal seems it would work, but I am a bit concerned about the one time connect call to get all actions of the report of the action being updated. Do you think we could instead put the logic to update the maxSequenceNumber here instead where we should already have all the data available without having to do that one time connect call @dklymenk?

iwiznia avatar Jul 02 '21 22:07 iwiznia

Adding the Help Wanted label, as it looks like we're still accepting new proposals on this one (please remove the label if it's not applicable)

NikkiWines avatar Jul 02 '21 23:07 NikkiWines

@iwiznia

Do you think we could instead put the logic to update the maxSequenceNumber here instead where we should already have all the data available without having to do that one time connect call @dklymenk?

There are a few issues at play here.

  1. Since all this Onyx stuff is async, we'll never get up-to-date information in context of one function, where an Onyx.merge call updates something in local storage. The values that are updated via Onyx.connect subscribers outside that function will only be updated after the function finished execution.

Let's say we move my suggested code (including this Onyx.merge call below) to the place you suggested

 Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
     maxSequenceNumber: lastReportAction.sequenceNumber,
 });

The value we care about in updateReportActionMessage is reportMaxSequenceNumbers[reportID] that is updated by an Onyx.connect subscriber here. Due to the nature of event-driven architecture and async functions, after we call Onyx.merge to update the report action, neither this callback gets called nor does this one until updateReportActionMessage function finishes it's execution, so the value reportMaxSequenceNumbers[reportID] will still be unreliable.

This is why regardless of where exactly we put that code, in the end we will still need to do a Onyx.connect inside the function to get up-to-date information about report actions or report.

  1. The main reason for me to do it like this is because I'm not just fetching all report actions to get max sequence number there, but also to potentially get last message text/html in order to update LHN. This suggestion for me is not just a fix for the issue described in the OP, but also an infrastructure development for this PR #3526. Since I'll need to fetch all reportActions via Onyx.connect to get new last message details after last message deletion anyway, I see no harm in doing it that way. If my proposal in this issue gets approved, the code from #3526 will fit in flawlessly, like a puzzle piece.

Thanks.

dklymenk avatar Jul 03 '21 04:07 dklymenk

I'd like to put a little bit of a hold on this. It seems to me that this is complex code, and both the code being proposed here and in https://github.com/Expensify/Expensify.cash/pull/3526 are only making it more and more complex.

I think it would be worth it to take a step back, look at some of the root causes of these problems, and see if there is a better solution.

Remember, this stuff was originally developed without the understanding that messages would be deleted. That's OK, we avoid pre-optimizing solutions and over-engineering things. This means that when new functionality needs to be added (like deleting report actions), we refactor the original solutions to allow the new functionality to exist.

It seems like some of the root problems that lead up to this are:

  1. There is data being duplicated in Onyx. Specifically, the last message details exist in both the report collection and the report action collection. This leads to complex code that must now keep both of those in sync
  2. When the last message is deleted, the report's maxSequenceNumber isn't updated

Before talking any more about solutions, can we agree that these are the root cause of the problem? Am I missing anything?

tgolen avatar Jul 09 '21 15:07 tgolen

Yes, I 100% agree with your concerns. While my proposed code fixes the user facing issues, the code is unreasonably complex for a feature as simple last message display in LHN.

  1. There is data being duplicated in Onyx. Specifically, the last message details exist in both the report collection and the report action collection. This leads to complex code that must now keep both of those in sync

If I was to redesign this flow, I would remove the last message data from report collection and extract last non-empty message from report action collection directly in component that needs it. This will allow us to simplify functions updateReportActionMessage and getSimplifiedReportObject in lib/actions/Report.js as well eliminate the need for code proposed for deleteReportComment in #3526 .

  1. When the last message is deleted, the report's maxSequenceNumber isn't updated

The vague proposal above will also make it so we don't need to care about maxSequenceNumber, at least in the context of LHN. There will still be some code that uses it, but that issue should IMO be addressed separately, since it won't interfere with LHN in any way.

Please let me know, if that is something you are looking for. I'll happily draft a proposal with actual code.

Thanks.

dklymenk avatar Jul 09 '21 16:07 dklymenk

Awesome, thanks for confirming my thoughts!

If I was to redesign this flow, I would remove the last message data from report collection and extract last non-empty message from report action collection directly in component that needs it.

This is what was running through my head as well. I think we should definitely consider this. Could you work on providing a proposal to do this? I think only the LHN is impacted by this, but we need to be careful that nothing else is.

The vague proposal above will also make it so we don't need to care about maxSequenceNumber, at least in the context of LHN. There will still be some code that uses it, but that issue should IMO be addressed separately, since it won't interfere with LHN in any way.

Cool. I think if it removes the need to worry about maxSequenceNumber in this context, then I agree it should be addressed separately.

tgolen avatar Jul 09 '21 16:07 tgolen

Please keep in mind that the "last message" is saved in the report object when the app first inits so we can populate the LHN in an efficient way. Sets of report actions notoriously:

  1. Take time to fetch completely at large numbers
  2. Are not guaranteed to even exist due to cache eviction

Separately, I'm currently working on a backend optimization that will significantly improve the time required to fetch this information when the app inits and "refreshes". Which, again, largely exists so we can avoid having to fetch all the report actions for all the reports.

So, I'm not sure if I agree yet with the premise that report actions should be the single source of truth. But I agree there is some opportunity to unwind complexity here.

marcaaron avatar Jul 09 '21 19:07 marcaaron

Thanks for that insight, Marc!

On Fri, Jul 9, 2021 at 1:04 PM Marc Glasser @.***> wrote:

Please keep in mind that the "last message" is saved in the report object when the app first inits so we can populate the LHN in an efficient way. Sets of report actions notoriously:

  1. Take time to fetch completely at large numbers
  2. Are not guaranteed to even exist due to cache eviction

Separately, I'm currently working on a backend optimization that will significantly improve the time required to fetch this information when the app inits and "refreshes". Which, again, largely exists so we can avoid having to fetch all the report actions for all the reports.

So, I'm not sure if I agree yet with the premise that report actions should be the single source of truth. But I agree there is some opportunity to unwind complexity here.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Expensify/Expensify.cash/issues/3743#issuecomment-877397434, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJMAB36QBCFRMFVYBGY26LTW5B5LANCNFSM47IEW6QA .

tgolen avatar Jul 09 '21 20:07 tgolen