Skip to content

Commit 1eee3de

Browse files
committed
Release fix: Truncation related fixes for mark as/jump to unread (#2806)
* Reset read for truncated messages * Allow automatic marking as read when discarding unread messages * Make sure we can mark as read if the last message was seen or is currently on screen
1 parent f20a028 commit 1eee3de

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Sources/StreamChat/Controllers/ChannelController/ChannelController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
861861
updater.markRead(cid: channel.cid, userId: currentUserId) { error in
862862
self.callback {
863863
self.markingRead = false
864+
self.isMarkedAsUnread = false
864865
completion?(error)
865866
}
866867
}

Sources/StreamChat/WebSocketClient/EventMiddlewares/ChannelTruncatedEventMiddleware.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ struct ChannelTruncatedEventMiddleware: EventMiddleware {
1414
}
1515

1616
do {
17-
if let channelDTO = session.channel(cid: truncatedEvent.channel.cid) {
18-
channelDTO.truncatedAt = truncatedEvent.channel.truncatedAt?.bridgeDate
19-
} else {
20-
throw ClientError.ChannelDoesNotExist(cid: truncatedEvent.channel.cid)
17+
let cid = truncatedEvent.channel.cid
18+
guard let channelDTO = session.channel(cid: cid) else {
19+
throw ClientError.ChannelDoesNotExist(cid: cid)
20+
}
21+
22+
channelDTO.truncatedAt = truncatedEvent.channel.truncatedAt?.bridgeDate
23+
24+
// Until BE returns valid values for user's read after truncation, we are clearing them.
25+
if let userId = truncatedEvent.user?.id, let read = session.loadChannelRead(cid: cid, userId: userId) {
26+
read.lastReadMessageId = nil
27+
read.lastReadAt = truncatedEvent.channel.truncatedAt?.bridgeDate ?? DBDate()
28+
read.unreadMessageCount = 0
2129
}
2230
} catch {
2331
log.error("Failed to write the `truncatedAt` field update in the database, error: \(error)")

Sources/StreamChatUI/ChatChannel/ChatChannelVC.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ open class ChatChannelVC: _ViewController,
9090
return false
9191
}
9292

93-
return hasSeenLastMessage && hasSeenFirstUnreadMessage && channelController.hasLoadedAllNextMessages && !hasMarkedMessageAsUnread
93+
return isLastMessageVisibleOrSeen && hasSeenFirstUnreadMessage && channelController.hasLoadedAllNextMessages && !hasMarkedMessageAsUnread
94+
}
95+
96+
private var isLastMessageVisibleOrSeen: Bool {
97+
hasSeenLastMessage || isLastMessageFullyVisible
9498
}
9599

96100
/// A component responsible to handle when to load new or old messages.
@@ -106,7 +110,7 @@ open class ChatChannelVC: _ViewController,
106110
/// Determines whether first unread message has been seen
107111
private var hasSeenFirstUnreadMessage: Bool = false
108112

109-
/// Determines whether last cell has been seen
113+
/// Determines whether last cell has been seen since the last time it was marked as read
110114
private var hasSeenLastMessage: Bool = false
111115

112116
/// The id of the first unread message

Sources/StreamChatUI/ChatMessageList/ChatMessageListVC.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,6 @@ private extension ChatMessageListVC {
10391039
self?.adjustContentOffset(oldContentOffset: oldContentOffset, oldContentSize: oldContentSize)
10401040
}
10411041

1042-
self?.updateScrollDependentButtonsVisibility()
1043-
10441042
UIView.performWithoutAnimation {
10451043
self?.scrollToBottomIfNeeded(with: changes, newestChange: newestChange)
10461044
self?.reloadMovedMessage(newestChange: newestChange)

0 commit comments

Comments
 (0)