Refactoring
This commit is contained in:
parent
825eb8bf39
commit
66e7e38496
|
@ -28,18 +28,20 @@ struct ContentDatabase {
|
|||
}
|
||||
|
||||
extension ContentDatabase {
|
||||
func insert(statuses: [Status], timeline: Timeline? = nil) -> AnyPublisher<Never, Error> {
|
||||
func insert(status: Status) -> AnyPublisher<Never, Error> {
|
||||
databaseQueue.writePublisher(updates: status.save)
|
||||
.ignoreOutput()
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func insert(statuses: [Status], timeline: Timeline) -> AnyPublisher<Never, Error> {
|
||||
databaseQueue.writePublisher {
|
||||
try timeline?.save($0)
|
||||
try timeline.save($0)
|
||||
|
||||
for status in statuses {
|
||||
for component in status.storedComponents() {
|
||||
try component.save($0)
|
||||
}
|
||||
try status.save($0)
|
||||
|
||||
if let timeline = timeline {
|
||||
try TimelineStatusJoin(timelineId: timeline.id, statusId: status.id).save($0)
|
||||
}
|
||||
try TimelineStatusJoin(timelineId: timeline.id, statusId: status.id).save($0)
|
||||
}
|
||||
}
|
||||
.ignoreOutput()
|
||||
|
@ -49,9 +51,7 @@ extension ContentDatabase {
|
|||
func insert(context: Context, parentID: String) -> AnyPublisher<Never, Error> {
|
||||
databaseQueue.writePublisher {
|
||||
for status in context.ancestors + context.descendants {
|
||||
for component in status.storedComponents() {
|
||||
try component.save($0)
|
||||
}
|
||||
try status.save($0)
|
||||
}
|
||||
|
||||
for (section, statuses) in [(StatusContextJoin.Section.ancestors, context.ancestors),
|
||||
|
@ -502,17 +502,15 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
|
|||
}
|
||||
|
||||
private extension Status {
|
||||
func storedComponents() -> [PersistableRecord] {
|
||||
var components: [PersistableRecord] = [account]
|
||||
func save(_ db: Database) throws {
|
||||
try account.save(db)
|
||||
|
||||
if let reblog = reblog {
|
||||
components.append(reblog.account)
|
||||
components.append(StoredStatus(status: reblog))
|
||||
try reblog.account.save(db)
|
||||
try StoredStatus(status: reblog).save(db)
|
||||
}
|
||||
|
||||
components.append(StoredStatus(status: self))
|
||||
|
||||
return components
|
||||
try StoredStatus(status: self).save(db)
|
||||
}
|
||||
|
||||
convenience init(statusResult: StatusResult) {
|
||||
|
|
|
@ -33,8 +33,7 @@ extension StatusListService {
|
|||
networkClient: networkClient,
|
||||
contentDatabase: contentDatabase) { maxID, minID in
|
||||
networkClient.request(Paged(timeline.endpoint, maxID: maxID, minID: minID))
|
||||
.map { ($0, timeline) }
|
||||
.flatMap(contentDatabase.insert(statuses:timeline:))
|
||||
.flatMap { contentDatabase.insert(statuses: $0, timeline: timeline) }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
@ -62,12 +61,10 @@ public extension StatusListService {
|
|||
contentDatabase: contentDatabase) { _, _ in
|
||||
Publishers.Merge(
|
||||
networkClient.request(StatusEndpoint.status(id: statusID))
|
||||
.map { ([$0], nil) }
|
||||
.flatMap(contentDatabase.insert(statuses:timeline:))
|
||||
.flatMap(contentDatabase.insert(status:))
|
||||
.eraseToAnyPublisher(),
|
||||
networkClient.request(ContextEndpoint.context(id: statusID))
|
||||
.map { ($0, statusID) }
|
||||
.flatMap(contentDatabase.insert(context:parentID:))
|
||||
.flatMap { contentDatabase.insert(context: $0, parentID: statusID) }
|
||||
.eraseToAnyPublisher())
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
|
|
@ -21,8 +21,7 @@ public extension StatusService {
|
|||
networkClient.request(status.displayStatus.favourited
|
||||
? StatusEndpoint.unfavourite(id: status.displayStatus.id)
|
||||
: StatusEndpoint.favourite(id: status.displayStatus.id))
|
||||
.map { ([$0], nil) }
|
||||
.flatMap(contentDatabase.insert(statuses:timeline:))
|
||||
.flatMap(contentDatabase.insert(status:))
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue