Skip to content

Commit

Permalink
Merge pull request #1412 from matrix-org/ismail/5840_thread_events_order
Browse files Browse the repository at this point in the history
Fix thread events ordering
  • Loading branch information
ismailgulek authored Mar 18, 2022
2 parents 63f7e74 + 8550b76 commit a12a74c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
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.

0 comments on commit a12a74c

Please sign in to comment.