Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(messenger): make sure chats have an unread count of 0 for channels you can't view #5062

Merged
merged 1 commit into from May 15, 2024

Conversation

jrainville
Copy link
Member

Fixes status-im/status-desktop#14421

The problem is that you can receive messages to a channel, then later, before marking them as read, a permission is added to them, so you no longer have access. Then, you can't even mark it as read if it's hidden. Here, I fix it by setting the unread count on Init at 0 if the user doesn't have view access to it.

It's not the cleanest solution, but it's the simplest one to fix previous accounts that had that issue.

Another solution would be to change the DB value of the chat when we receive a new permission and detect that we no longer have view access. It's cleaner, but it doesn't fix previous account having the issue.
Also, keeping the DB value means that if the channel goes back to visible to us, we'll have a badge for the previous unread messages we had.

Let me know what you guys think.

@status-im-auto
Copy link
Member

status-im-auto commented Apr 16, 2024

Jenkins Builds

Click to see older builds (21)
Commit #️⃣ Finished (UTC) Duration Platform Result
✖️ 0e244ed #1 2024-04-16 20:07:52 ~1 min tests 📄log
✔️ 0e244ed #1 2024-04-16 20:10:44 ~4 min linux 📦zip
✔️ 0e244ed #1 2024-04-16 20:11:30 ~4 min ios 📦zip
✔️ 0e244ed #1 2024-04-16 20:11:57 ~5 min android 📦aar
✖️ a8000ed #2 2024-04-17 18:39:09 ~1 min tests 📄log
✔️ a8000ed #2 2024-04-17 18:40:38 ~2 min linux 📦zip
✔️ a8000ed #2 2024-04-17 18:41:42 ~3 min ios 📦zip
✔️ a8000ed #2 2024-04-17 18:43:19 ~5 min android 📦aar
✖️ f142ea0 #3 2024-04-19 14:15:20 ~3 min tests 📄log
✔️ f142ea0 #3 2024-04-19 14:16:53 ~4 min linux 📦zip
✔️ f142ea0 #3 2024-04-19 14:16:54 ~4 min ios 📦zip
✔️ f142ea0 #3 2024-04-19 14:18:12 ~6 min android 📦aar
✔️ 6f6ab1c #4 2024-04-19 16:14:01 ~2 min android 📦aar
✔️ 6f6ab1c #4 2024-04-19 16:14:16 ~2 min linux 📦zip
✔️ 6f6ab1c #4 2024-04-19 16:15:16 ~3 min ios 📦zip
✖️ 6f6ab1c #4 2024-04-19 16:48:17 ~36 min tests 📄log
✔️ 804c8f8 #5 2024-04-19 16:58:30 ~2 min linux 📦zip
✔️ 804c8f8 #5 2024-04-19 16:59:10 ~3 min ios 📦zip
✔️ 804c8f8 #5 2024-04-19 17:02:18 ~6 min android 📦aar
✖️ 804c8f8 #5 2024-04-19 17:35:35 ~39 min tests 📄log
✖️ 804c8f8 #6 2024-04-19 18:42:53 ~39 min tests 📄log
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ b7e045e #6 2024-05-15 18:23:53 ~4 min linux 📦zip
✔️ b7e045e #6 2024-05-15 18:24:19 ~4 min ios 📦zip
✔️ b7e045e #6 2024-05-15 18:24:50 ~5 min android 📦aar
✔️ b7e045e #7 2024-05-15 19:04:04 ~44 min tests 📄log

Copy link
Contributor

@osmaczko osmaczko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for precise description.

As far as I can see, current fix proposal will not work at runtime. Meaning, if the app is already opened and the permission to channel is lost, unview counts will stay as they are.

Also, keeping the DB value means that if the channel goes back to visible to us, we'll have a badge for the previous unread messages we had.

I am not sure about that assumption. The cache you modified will be eventually saved to the db and the values will be zeroed there as well.

Another solution would be to change the DB value of the chat when we receive a new permission and detect that we no longer have view access. It's cleaner, but it doesn't fix previous account having the issue.

I think the issue is more complicated than assumed. unviewed_message_count and unviewed_mentions_count are denormalized fields and can be modified in BlockContact for instance. So if you block contact that wrote a message on channel you have no permissions to, the unread counts will be back there.

I am thinking about the following solution:

  1. Add canView column to chats' db
  2. Modify the logic of denormalized fields recalculation, so that it zeroes when !chat.canView - this will ensure no surprises as described above
  3. Whenever chat.canView is changed, trigger denormalized fields recalculation
  4. Keep changes in Init as you did + update chat.canView

Ideally point 4) should be done through migration but since we keep community description marshaled I don't think it is even possible.

The points 1-3 are good candidates to be addressed in another PR I suppose.

@jrainville
Copy link
Member Author

As far as I can see, current fix proposal will not work at runtime. Meaning, if the app is already opened and the permission to channel is lost, unview counts will stay as they are.

You're right.

Also, keeping the DB value means that if the channel goes back to visible to us, we'll have a badge for the previous unread messages we had.

I am not sure about that assumption. The cache you modified will be eventually saved to the db and the values will be zeroed there as well.

Good point, I hadn't thought about it.

Ideally point 4) should be done through migration but since we keep community description marshaled I don't think it is even possible.

Indeed, I was thinking to see if there was a possible migration that would fix this in one pass and not think about it anymore, but it's impossible with how the Community description is marshalled.

The points 1-3 are good candidates to be addressed in another PR I suppose.

I think to correctly fix the issue, I'll need to implement some of the points from 1-3 right away. Otherwise, like you said, we'll be missing the update on runtime.

Thanks for the great insight!

@jrainville jrainville force-pushed the fix/unread-msgs-in-hidden-chats branch from 0e244ed to a8000ed Compare April 17, 2024 18:37
@jrainville
Copy link
Member Author

Alright, I pushed a new commit that addresses the problem that when the channel becomes unavailable, the count wouldn't update.

I fixed it by checking if we are removed from a community channel, then we reset the counts to 0.

I'll keep the changes in Init too to fix people who had the problem happen to them before this fix (since we can't easily do a migration).

These changes don't address putting back the old count when the channel is back available, but IMO it's an edge case plus not that useful, so it's not worth recalculating.

As for the other points to improve, I think they will be mostly addressed in this refactor to align CommunityChats and Chats (adding canView, canPost, etc to the Chat) status-im/status-desktop#14219

@jrainville jrainville force-pushed the fix/unread-msgs-in-hidden-chats branch 3 times, most recently from 6f6ab1c to 804c8f8 Compare April 19, 2024 16:55
@jrainville
Copy link
Member Author

Now that you're back @osmaczko you can review again 😄

// Check if we have been removed from a chat (ie no longer have access)
for id, changes := range communityResponse.Changes.ChatsModified {
if _, ok := changes.MembersRemoved[common.PubkeyToHex(&m.identity.PublicKey)]; ok {
chatID := community.IDString() + id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
chatID := community.IDString() + id
chatID := community.ChatID(id)

@@ -3089,6 +3089,24 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi
}
}

// Check if we have been removed from a chat (ie no longer have access)
for id, changes := range communityResponse.Changes.ChatsModified {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for id, changes := range communityResponse.Changes.ChatsModified {
for channelID, changes := range communityResponse.Changes.ChatsModified {

…ls you can't view

Fixes status-im/status-desktop#14421

The problem is that you can receive messages to  a channel, then later, before marking them as read, a permission is added to them, so you no longer have access.
Then, you can't even mark it as read if it's hidden.
Here, I fix it by setting the unread count on Init at 0 if the user doesn't have view access to it. And I make sure we update the counts when we are removed from a channel
@jrainville jrainville force-pushed the fix/unread-msgs-in-hidden-chats branch from 804c8f8 to b7e045e Compare May 15, 2024 18:19
@jrainville jrainville merged commit 5ca1cb0 into develop May 15, 2024
9 checks passed
@jrainville jrainville deleted the fix/unread-msgs-in-hidden-chats branch May 15, 2024 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Chat] Unread badge even for unreachable channels
3 participants