App icon indicating copy to clipboard operation
App copied to clipboard

[HOLD for payment 2021-12-13] [HOLD for payment 2021-12-06] Delete comment - Deleted comment is still visible on LHN

Open isagoico opened this issue 5 years ago • 43 comments

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


Expected Result:

LHN should show the message that was before the deleted message.

Actual Result:

LHN still shows the deleted comment. After refreshing the page, the email address of the sender is displayed.

Action Performed:

  1. Log in to e.cash
  2. Navigate to a conversation
  3. Send a message
  4. Delete the message
  5. Check the LHN
  6. Refresh the page

Workaround:

No need, visual issue.

Platform:

Where is this issue occurring?

Web ✔️ iOS✔️ Android ✔️ Desktop App (Don't have a mac to check) Mobile Web ✔️

Version Number: 1.0.59-0

Logs: https://stackoverflow.com/c/expensify/questions/4856

Notes/Photos/Videos:

image

https://user-images.githubusercontent.com/44479856/120511373-5bf28d80-c398-11eb-8337-f96bbe508290.mp4

Expensify/Expensify Issue URL:

view this job on upwork

View all open jobs on Upwork


From @rafecolton https://expensify.slack.com/archives/C01GTK53T8Q/p1622591805431300

Deleted comment shows as the recipient's email in the LHN. At first, the deleted comment still showed there, and then after a hard refresh, it just shows the email. Expected behavior is it would show the previous comment as though the deleted comment had never occurred.

isagoico avatar Jun 02 '21 15:06 isagoico

Triggered auto assignment to @cead22 (Engineering), see https://stackoverflow.com/c/expensify/questions/4319 for more details.

MelvinBot avatar Jun 02 '21 15:06 MelvinBot

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

MelvinBot avatar Jun 02 '21 18:06 MelvinBot

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

MelvinBot avatar Jun 02 '21 19:06 MelvinBot

There are 2 issues here (last message shown is not updated after deleting it and after reload it displays sender email) that need to be addressed separately, but I think a solution can be refactored into one new function partially.

Addressing the refresh issue

It is caused by the fact that function getSimplifiedReportObject, that is responsible for converting API data into object that is stored locally, gets last message by simply getting the last object value. This is not correct anymore because apparently a delete comment action just changes it's text to an empty string and it is still served by BE as an actual message.

The reason it displays an email is as simple as it being a fallback in case message is empty.

So my proposal here is to update the way lastReportAction is found here:

https://github.com/Expensify/Expensify.cash/blob/8b979de2cf00deafbba85c5c139f6d78cd1fd3f2/src/libs/actions/Report.js#L148-L153

with something like this:

    const reportActionKey = _.find(
        Object.keys(reportActionList).reverse(),
        key => reportActionList[key].message.html !== '',
    );
    const lastReportAction = reportActionList[reportActionKey];

This will make sure last message has text (is not deleted).

Addressing the on delete issue

The root cause of this is the fact that data like lastMessageText, lastMessageTimestamp and lastActorEmail for a specific report are not updated on delete action. It is the way things work for updateReportActionMessage:

https://github.com/Expensify/Expensify.cash/blob/8b979de2cf00deafbba85c5c139f6d78cd1fd3f2/src/libs/actions/Report.js#L488-L493

and updateReportWithNewAction:

https://github.com/Expensify/Expensify.cash/blob/8b979de2cf00deafbba85c5c139f6d78cd1fd3f2/src/libs/actions/Report.js#L528-L534

but deleteReportComment function is missing that code, so I would need to implement something similar there:

    ...
    
    Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, reportActionsToMerge);

    // If this is the most recent message, update the lastMessage
    if (reportAction.sequenceNumber === reportMaxSequenceNumbers[reportID]) {
        #TODO figure out how to get reportActionList here
        const reportActionList = []; 
        const reportActionKey = _.find(
            Object.keys(reportActionList).reverse(),
            key => _.last(reportActionList[key].message).text !== '',
        );
        const lastReportAction = reportActionList[reportActionKey];
        const createTimestamp = lastReportAction ? lastReportAction.created : 0;
        const lastMessageTimestamp = moment.utc(createTimestamp).unix();
        const lastMessageText = lodashGet(lastReportAction, ['message', 'html'], '')
            .replace(/((<br[^>]*>)+)/gi, ' ')
            .replace(/(<([^>]+)>)/gi, '');
        const lastActorEmail = lodashGet(lastReportAction, 'accountEmail', '');
        Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
            lastMessageText,
            lastMessageTimestamp,
            lastActorEmail,
        });
    }
        
     ...

Refactoring

I'm not exactly sure it would be worth it, but the code where I get last non-empty message text, timestamp and email can be refactored into a separate function getLastReportActionDetails(reportActionList) that would return an object with lastMessageText, lastMessageTimestamp and lastActorEmail. This function could be then used in both getSimplifiedReportObject and deleteReportComment.

Summary

I am yet to figure out how to get reportActionList in deleteReportComment function and I would also need to test if using sequence numbers to determine last message is good enough now that deleted messages can still have them, but otherwise that's it.

P.S. What does LHN stand for? It is referenced in the issue, but I couldn't find a relevant meaning online. Judging by the context I would assume it's synonymous with Last Chat Message, but I better be sure. Thanks.

EDIT: Fix grammar

dklymenk avatar Jun 03 '21 07:06 dklymenk

@dklymenk LHN = left hand navigation

jboniface avatar Jun 03 '21 18:06 jboniface

@dklymenk couple of notes on your proposal:

I think we could simplify your proposed lastReportAction logic by doing the following:

    const lastReportAction = !_.isEmpty(reportActionList)
        ? _.find([...reportActionList].reverse(), reportAction => reportAction.message.html !== '')
        : null;

It's also notable that IOU report actions don't have message.html, but your logic accounted for that already 👍

Your logic for deleteReportComment looks good, though I agree getting the reportActionList is a major blocker for being able to implement what you've proposed.

cc: @sketchydroide since you wrote a lot of the delete comment logic in Report.js

NikkiWines avatar Jun 04 '21 01:06 NikkiWines

@NikkiWines

I have given it another look and there should be no issues with doing the following inside deleteReportComment to get access to reportActionList.

Onyx.connect({
    key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
    callback: (reportActions) => {
        // TODO get last non-empty message and update the relevant data for report with reportID
    },
});

Additionally in my original proposal the code is presented in such a way that last message is updated regardless of the API response. So there is no case handling when this optimistic update needs to be reverted.

What I suggest to avoid this problem is not doing an optimistic update at all. To do this we run the "update last message" code only after we get 200 response from BE, since we are running async code anyway, it shouldn't a big deal. The code will be a lot cleaner and we don't have to think about potential race conditions.

If you insist on there being an optimistic update for last message, I can do that too. Since we already have last report action in that function, I can get the data from it and there will be no need for doing any Onyx.connect stuff the second time around. But I will need to make sure there is nothing funny happening like optimistic update occurring after the request response arrives.

dklymenk avatar Jun 08 '21 14:06 dklymenk

I think the approach @NikkiWines has is better, and I had thought of a similar solution. as for the on delete action, I wander if the regular on edit action suffers from similar problems, I mean when you edit a comment, the one in the LHN is not updated at the moment as well (at least I don't think it would be). Do you think this solution would work for both cases?

sketchydroide avatar Jun 08 '21 14:06 sketchydroide

I think the approach @NikkiWines has is better, and I had thought of a similar solution.

Yes, I agree the code for finding the last report action proposed by @NikkiWines is better.

as for the on delete action, I wander if the regular on edit action suffers from similar problems, I mean when you edit a comment, the one in the LHN is not updated at the moment as well (at least I don't think it would be). Do you think this solution would work for both cases?

My solution will only affect the on delete action, however, as I stated in my original proposal, updateReportActionMessage function is already updating the last message, so there is no such issue with on edit.

dklymenk avatar Jun 08 '21 15:06 dklymenk

sounds good then

sketchydroide avatar Jun 08 '21 15:06 sketchydroide

Cool, @jboniface can you hire @dklymenk for this job on Upwork please?

NikkiWines avatar Jun 08 '21 19:06 NikkiWines

@NikkiWines What do you think about optimistic update? Should I bother with it or should I update the last message only after successful response from BE?

dklymenk avatar Jun 08 '21 19:06 dklymenk

hired!

jboniface avatar Jun 08 '21 20:06 jboniface

@dklymenk we typically prefer an optimistic approach but it looks like updateReportActionMessage isn't being used optimistically at the moment. You can go with the simpler non-optimistic approach

NikkiWines avatar Jun 08 '21 21:06 NikkiWines

@NikkiWines I have submitted a PR for this issue #3526

There are a few issues I have run into on the way. They made me alter my original proposal in a few ways, so I thought I need to list them:

  1. The callback for Onyx.connect gets called twice and sometimes even three times for me. I tried my best to figure out the cause, but after debugging react-native-onyx code I couldn't find the issue. Anyway, I used _.once to ensure the callback is called only once, and it seems to work perfectly.
  2. I haven't stumbled upon any code worth refactoring into single function because reportActionList is a completely different data entity in context of getSimplifiedReportObject and deleteReportComment functions. In the first case it's an array of objects returned from API, and in the second one it's an object of objects from local storage. They are not 100% different, but IMO there are enough differences to avoid trying to write some generalized function that would extract lastMessageDetails from reportActionList in both cases.
  3. The if statement from my original proposal, that was meant to ensure the code runs only if last message is deleted, works only for the first deleted message:
reportAction.sequenceNumber === reportMaxSequenceNumbers[reportID]

Updating reportMaxSequenceNumbers directly does not work from the context of that callback. If I was to do some Onyx.merge to update it, there would always be some weird issues when you delete 2 messages really fast and the maxSequenceNumber for the report wasn't updated fast enough, causing the second deletion to not pass the check. So, I decided to leave it as is. If you have any idea how this could be improved, I'd be happy to learn. Anyway, I don't believe this is a big deal.

Other than some minor additions for edge case handling (if you delete one and only message of the chat or next last message is an attachment), I think this is it.

Thanks.

dklymenk avatar Jun 10 '21 21:06 dklymenk

Hi, @dklymenk thanks for posting an update.

  1. I think using _.once is fine here - though the multiple calls make me wonder if using _.once means you're not guaranteed to get the most up-to-date reportActionsList.

  2. I think it's fine without the refactoring

  3. I also don't think this is a huge deal - but I'll think on some potential alternatives

NikkiWines avatar Jun 15 '21 02:06 NikkiWines

I think using _.once is fine here - though the multiple calls make me wonder if using _.once means you're not guaranteed to get the most up-to-date reportActionsList.

In my testing that was never the case. I believe it's because every time we delete a comment a new instance of the onyx callback is called, so _.once function is called at least once on each comment deletion, so it does exactly what we want.

Not sure if my explanation makes a lot of sense, but I think there is a way to refactor the code in such a way that would cause the target function to literally be executed only once. Let's say we declare the callback in another scope and then just pass the function returned from _.once as a callback to Onyx.connect. I believe that in that case the function would actually be called only once, preventing it from being called a second time at all.

dklymenk avatar Jun 15 '21 09:06 dklymenk

Hmm, I think that's fine then... but just to be safe cc: @marcaaron just to be sure we're not going against anything by using _.once here.

NikkiWines avatar Jun 17 '21 01:06 NikkiWines

Let's take it to #expensify-open-source ?

marcaaron avatar Jun 17 '21 01:06 marcaaron

UPDATE: #3979 has been merged into main yesterday. It fixes this issue by showing 'comment deleted' message instead of previous one, if you delete the last comment. You are free to read discussion on #3743 to see how we got here, but basically showing next last message is either too inefficient or requires a lot of refactoring.

Thanks.

dklymenk avatar Jul 13 '21 07:07 dklymenk