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

[CIS-784] Fix channel unread count calculations for new messages #969

Merged
merged 10 commits into from
Apr 8, 2021

Conversation

VojtaStavik
Copy link
Contributor

This PR fixes a lot of small issues which together caused very strange behavior of channel unread count number.

The most important changes:

  • Changes to ChannelReadDTO now trigger ChannelDTO updated callbacks
  • MessageReadEvent incorrectly had a mandatory unreadCount parameter which made it impossible to parse and handle for other than the current user.
  • After consulting with the backend team in Slack, ChannelReadUpdaterMiddleware now increases the read unread count for new incoming messages.

unread_count.mov

@VojtaStavik VojtaStavik requested a review from b-onc April 8, 2021 15:59
Copy link
Contributor

@b-onc b-onc left a comment

Choose a reason for hiding this comment

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

I'm really not sure about unreadCount becoming a Cached value...

@@ -164,7 +165,7 @@ public struct _ChatChannel<ExtraData: ExtraDataTypes> {
currentlyTypingMembers: Set<_ChatChannelMember<ExtraData.User>> = [],
lastActiveWatchers: @escaping (() -> [_ChatUser<ExtraData.User>]) = { [] },
team: TeamId? = nil,
unreadCount: ChannelUnreadCount = .noUnread,
unreadCount: @escaping () -> ChannelUnreadCount = { .noUnread },
Copy link
Contributor

Choose a reason for hiding this comment

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

so unreadCount is not a value but a dynamic thingy now.. This makes us move further away from "snapshot models". Since we send channelUpdated delegate callbacks when unread count updates, why don't we still use static unread counts in models?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, this just means it's computed lazily. Getting unread count requires iterating over channel reads + an additional DB query. That's why it's computed lazily. See other @Cached properties in our models.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, explanation + re-reading code made it clearer. Makes sense 👌

userId = try response.value(at: \.user?.id)
cid = try response.value(at: \.cid)
readAt = try response.value(at: \.createdAt)
unreadCount = try response.value(at: \.unreadCount)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused, backend doesn't send this info anymore, or it never sent this, or this is not useful?

Copy link
Contributor Author

@VojtaStavik VojtaStavik Apr 8, 2021

Choose a reason for hiding this comment

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

This was actually a complete misunderstanding of how MessageReadEvent works :) The unread count is always zero hence is not sent. This unread count is the total unread count and is sent only for the current user -> see the PR description for more info.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be optional and useful when available? If it exists, we can use it to update current user's unread count

userId = try response.value(at: \.message?.user.id)
cid = try response.value(at: \.channel?.cid)
messageId = try response.value(at: \.message?.id)
createdAt = try response.value(at: \.message?.createdAt)
unreadCount = try response.value(at: \.unreadCount)
unreadCount = try? response.value(at: \.unreadCount)
Copy link
Contributor

Choose a reason for hiding this comment

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

In which cases backend doesn't send this value? would be nice to comment this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are several conditions. See https://getstream.slack.com/archives/CE5N802GP/p1617740759146000 for more info. This whole thing about unread counts is still not ideal and will need a rethink.

@codecov
Copy link

codecov bot commented Apr 8, 2021

Codecov Report

Merging #969 (e52f374) into main (e8f0cb1) will decrease coverage by 0.03%.
The diff coverage is 90.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #969      +/-   ##
==========================================
- Coverage   89.44%   89.41%   -0.04%     
==========================================
  Files         203      203              
  Lines        8330     8371      +41     
==========================================
+ Hits         7451     7485      +34     
- Misses        879      886       +7     
Flag Coverage Δ
llc-tests 89.41% <90.27%> (-0.04%) ⬇️
llc-tests-ios12 85.48% <90.27%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
Sources/StreamChat/Database/DatabaseSession.swift 97.56% <ø> (ø)
Sources/StreamChat/Models/Channel.swift 73.91% <66.66%> (-1.09%) ⬇️
...ventMiddlewares/ChannelReadUpdaterMiddleware.swift 89.13% <88.57%> (-4.62%) ⬇️
Sources/StreamChat/Database/DTOs/ChannelDTO.swift 98.94% <90.90%> (-1.06%) ⬇️
...rces/StreamChat/Database/DTOs/ChannelReadDTO.swift 100.00% <100.00%> (ø)
...eamChat/WebSocketClient/Events/MessageEvents.swift 100.00% <100.00%> (ø)
...at/WebSocketClient/Events/NotificationEvents.swift 94.64% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e8f0cb1...e52f374. Read the comment docs.

@VojtaStavik VojtaStavik merged commit ea0155e into main Apr 8, 2021
@VojtaStavik VojtaStavik deleted the CIS-784-fix-channel-unread-count branch April 8, 2021 16:26
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.

2 participants