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 thread events ordering #1412

Merged
merged 4 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {

// Reset store pagination
storeMessagesEnumerator = store.messagesEnumerator(forRoom: thread.roomId)

if isLiveTimeline {
hasReachedHomeServerBackwardsPaginationEnd = false
hasReachedHomeServerForwardsPaginationEnd = false
}
}

public func __resetPaginationAroundInitialEvent(withLimit limit: UInt,
Expand Down Expand Up @@ -376,33 +381,38 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {
// MARK: - Private

private func processPaginationResponse(_ response: MXAggregationPaginatedResponse, direction: MXTimelineDirection) {
let dispatchGroup = DispatchGroup()
MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: \(response.chunk.count) messages from the server, direction: \(direction)")

dispatchGroup.enter()
decryptEvents(response.chunk) {
// process chunk first
for event in response.chunk {
self.addEvent(event, direction: direction, fromStore: false)
}
dispatchGroup.leave()
}
if let rootEvent = response.originalEvent, response.nextBatch == nil {
dispatchGroup.enter()
decryptEvents([rootEvent]) {
self.addEvent(rootEvent, direction: direction, fromStore: false)
dispatchGroup.leave()

let updatePaginationInfo = {
switch direction {
case .backwards:
self.backwardsPaginationToken = response.nextBatch
self.hasReachedHomeServerBackwardsPaginationEnd = response.nextBatch == nil
MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: hasReachedHomeServerBackwardsPaginationEnd: \(self.hasReachedHomeServerBackwardsPaginationEnd)")
case .forwards:
self.forwardsPaginationToken = response.nextBatch
self.hasReachedHomeServerForwardsPaginationEnd = response.nextBatch == nil
MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: hasReachedHomeServerForwardsPaginationEnd: \(self.hasReachedHomeServerForwardsPaginationEnd)")
@unknown default:
fatalError("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: Unknown direction")
}
}
}

dispatchGroup.notify(queue: .main) {
switch direction {
case .backwards:
self.backwardsPaginationToken = response.nextBatch
self.hasReachedHomeServerBackwardsPaginationEnd = response.nextBatch == nil
case .forwards:
self.forwardsPaginationToken = response.nextBatch
self.hasReachedHomeServerForwardsPaginationEnd = response.nextBatch == nil
@unknown default:
fatalError("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: Unknown direction")
// then process the root if there is no more messages in server

if let rootEvent = response.originalEvent, response.nextBatch == nil {
self.decryptEvents([rootEvent]) {
self.addEvent(rootEvent, direction: direction, fromStore: false)
updatePaginationInfo()
}
} else {
updatePaginationInfo()
}
}
}
Expand Down Expand Up @@ -435,13 +445,8 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {
if fromStore {
notifyListeners(event, direction: direction)
} else {
if let threadingService = thread.session?.threadingService {
threadingService.handleEvent(event, direction: direction) { handled in
if handled {
self.notifyListeners(event, direction: direction)
}
}
}
self.notifyListeners(event, direction: direction)
thread.session?.threadingService.handleEvent(event, direction: direction, completion: nil)

if !isLiveTimeline {
store.storeEvent(forRoom: thread.roomId, event: event, direction: direction)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5840.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MXThreadEventTimeline: Fix processing order of thread events & fix empty thread screen issue.