metatext/Services/Status List Services/ContextService.swift

95 lines
3.4 KiB
Swift
Raw Normal View History

2020-08-19 22:16:03 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-30 23:33:11 +00:00
import Mastodon
2020-08-19 22:16:03 +00:00
struct ContextService {
let statusSections: AnyPublisher<[[Status]], Error>
2020-08-28 22:39:17 +00:00
let paginates = false
2020-08-19 22:16:03 +00:00
2020-08-24 04:34:19 +00:00
private let status: Status
2020-08-30 23:59:49 +00:00
private let context = CurrentValueSubject<Context, Never>(Context(ancestors: [], descendants: []))
private let networkClient: APIClient
2020-08-19 22:16:03 +00:00
private let contentDatabase: ContentDatabase
private let collection: TransientStatusCollection
2020-08-30 23:59:49 +00:00
init(status: Status, networkClient: APIClient, contentDatabase: ContentDatabase) {
2020-08-19 22:16:03 +00:00
self.status = status
self.networkClient = networkClient
self.contentDatabase = contentDatabase
collection = TransientStatusCollection(id: "context-\(status.id)")
statusSections = contentDatabase.statusesObservation(collection: collection)
.combineLatest(context.setFailureType(to: Error.self))
.map { statuses, context in
[
context.ancestors.map { a in statuses.first { $0.id == a.id } ?? a },
[statuses.first { $0.id == status.id } ?? status],
context.descendants.map { d in statuses.first { $0.id == d.id } ?? d }
]
}
.removeDuplicates()
.eraseToAnyPublisher()
}
}
extension ContextService: StatusListService {
2020-08-30 05:31:30 +00:00
var filters: AnyPublisher<[Filter], Error> {
contentDatabase.activeFiltersObservation(date: Date(), context: .thread)
}
2020-08-24 04:34:19 +00:00
var contextParentID: String? { status.id }
2020-08-19 22:16:03 +00:00
2020-08-21 02:29:01 +00:00
func isReplyInContext(status: Status) -> Bool {
let flatContext = flattenedContext()
guard
let index = flatContext.firstIndex(where: { $0.id == status.id }),
index > 0
else { return false }
let previousStatus = flatContext[index - 1]
2020-08-24 04:34:19 +00:00
return previousStatus.id != contextParentID && status.inReplyToId == previousStatus.id
2020-08-21 02:29:01 +00:00
}
func hasReplyFollowing(status: Status) -> Bool {
let flatContext = flattenedContext()
guard
let index = flatContext.firstIndex(where: { $0.id == status.id }),
flatContext.count > index + 1
else { return false }
let nextStatus = flatContext[index + 1]
2020-08-24 04:34:19 +00:00
return status.id != contextParentID && nextStatus.inReplyToId == status.id
2020-08-21 02:29:01 +00:00
}
2020-08-26 09:19:38 +00:00
func request(maxID: String?, minID: String?) -> AnyPublisher<Never, Error> {
2020-08-24 04:34:19 +00:00
Publishers.Merge(
networkClient.request(StatusEndpoint.status(id: status.id))
.map { ([$0], collection) }
.flatMap(contentDatabase.insert(statuses:collection:)),
networkClient.request(ContextEndpoint.context(id: status.id))
.handleEvents(receiveOutput: context.send)
.map { ($0.ancestors + $0.descendants, collection) }
.flatMap(contentDatabase.insert(statuses:collection:)))
2020-08-19 22:16:03 +00:00
.eraseToAnyPublisher()
}
2020-08-21 02:29:01 +00:00
func statusService(status: Status) -> StatusService {
StatusService(status: status, networkClient: networkClient, contentDatabase: contentDatabase)
}
2020-08-19 22:16:03 +00:00
func contextService(status: Status) -> ContextService {
2020-08-21 02:29:01 +00:00
ContextService(status: status.displayStatus, networkClient: networkClient, contentDatabase: contentDatabase)
}
}
private extension ContextService {
func flattenedContext() -> [Status] {
context.value.ancestors + [status] + context.value.descendants
2020-08-19 22:16:03 +00:00
}
}