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

Thread List API #1368

Merged
merged 21 commits into from
Feb 15, 2022
Merged
Changes from 1 commit
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
65 changes: 45 additions & 20 deletions MatrixSDK/Threads/MXThreadingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,33 +144,58 @@ public class MXThreadingService: NSObject {
return nil
}

let filter = MXRoomEventFilter()
filter.relationTypes = [MXEventRelationTypeThread]
if onlyParticipated {
filter.relationSenders = [session.myUserId]
}
return session.matrixRestClient.messages(forRoom: roomId,
from: "",
direction: .backwards,
limit: nil,
filter: filter) { response in
switch response {
case .success(let paginationResponse):
guard let rootEvents = paginationResponse.chunk else {
completion(.success([]))
return
}
if session.homeserverCapabilities?.threads?.isEnabled == true {
// homeserver supports threads
let filter = MXRoomEventFilter()
filter.relationTypes = [MXEventRelationTypeThread]
if onlyParticipated {
filter.relationSenders = [session.myUserId]
}
return session.matrixRestClient.messages(forRoom: roomId,
from: "",
direction: .backwards,
limit: nil,
filter: filter) { response in
switch response {
case .success(let paginationResponse):
guard let rootEvents = paginationResponse.chunk else {
completion(.success([]))
ismailgulek marked this conversation as resolved.
Show resolved Hide resolved
return
}

let threads = rootEvents.map { self.thread(forRootEvent: $0, session: session) }.sorted(by: <)
completion(.success(threads))
case .failure(let error):
completion(.failure(error))
let threads = rootEvents.map { self.thread(forRootEvent: $0, session: session) }.sorted(by: <)
completion(.success(threads))
case .failure(let error):
completion(.failure(error))
}
}
} else {
// use local implementation
if onlyParticipated {
DispatchQueue.main.async {
completion(.success(self.localThreads(inRoom: roomId)))
}
} else {
DispatchQueue.main.async {
completion(.success(self.localParticipatedThreads(inRoom: roomId)))
}
}
return nil
}
}

// MARK: - Private

private func localThreads(inRoom roomId: String) -> [MXThreadProtocol] {
// sort threads so that the newer is the first
return unsortedThreads(inRoom: roomId).sorted(by: <)
}

private func localParticipatedThreads(inRoom roomId: String) -> [MXThreadProtocol] {
// filter only participated threads and then sort threads so that the newer is the first
return unsortedParticipatedThreads(inRoom: roomId).sorted(by: <)
}

private func thread(forRootEvent rootEvent: MXEvent, session: MXSession) -> MXThreadModel {
let notificationCount: UInt
let highlightCount: UInt
Expand Down