Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-02 02:39:44 -07:00
parent 825eb8bf39
commit 66e7e38496
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
3 changed files with 20 additions and 26 deletions

View File

@ -28,18 +28,20 @@ struct ContentDatabase {
} }
extension 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 { databaseQueue.writePublisher {
try timeline?.save($0) try timeline.save($0)
for status in statuses { for status in statuses {
for component in status.storedComponents() { try status.save($0)
try component.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() .ignoreOutput()
@ -49,9 +51,7 @@ extension ContentDatabase {
func insert(context: Context, parentID: String) -> AnyPublisher<Never, Error> { func insert(context: Context, parentID: String) -> AnyPublisher<Never, Error> {
databaseQueue.writePublisher { databaseQueue.writePublisher {
for status in context.ancestors + context.descendants { for status in context.ancestors + context.descendants {
for component in status.storedComponents() { try status.save($0)
try component.save($0)
}
} }
for (section, statuses) in [(StatusContextJoin.Section.ancestors, context.ancestors), for (section, statuses) in [(StatusContextJoin.Section.ancestors, context.ancestors),
@ -502,17 +502,15 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
} }
private extension Status { private extension Status {
func storedComponents() -> [PersistableRecord] { func save(_ db: Database) throws {
var components: [PersistableRecord] = [account] try account.save(db)
if let reblog = reblog { if let reblog = reblog {
components.append(reblog.account) try reblog.account.save(db)
components.append(StoredStatus(status: reblog)) try StoredStatus(status: reblog).save(db)
} }
components.append(StoredStatus(status: self)) try StoredStatus(status: self).save(db)
return components
} }
convenience init(statusResult: StatusResult) { convenience init(statusResult: StatusResult) {

View File

@ -33,8 +33,7 @@ extension StatusListService {
networkClient: networkClient, networkClient: networkClient,
contentDatabase: contentDatabase) { maxID, minID in contentDatabase: contentDatabase) { maxID, minID in
networkClient.request(Paged(timeline.endpoint, maxID: maxID, minID: minID)) networkClient.request(Paged(timeline.endpoint, maxID: maxID, minID: minID))
.map { ($0, timeline) } .flatMap { contentDatabase.insert(statuses: $0, timeline: timeline) }
.flatMap(contentDatabase.insert(statuses:timeline:))
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
} }
@ -62,12 +61,10 @@ public extension StatusListService {
contentDatabase: contentDatabase) { _, _ in contentDatabase: contentDatabase) { _, _ in
Publishers.Merge( Publishers.Merge(
networkClient.request(StatusEndpoint.status(id: statusID)) networkClient.request(StatusEndpoint.status(id: statusID))
.map { ([$0], nil) } .flatMap(contentDatabase.insert(status:))
.flatMap(contentDatabase.insert(statuses:timeline:))
.eraseToAnyPublisher(), .eraseToAnyPublisher(),
networkClient.request(ContextEndpoint.context(id: statusID)) networkClient.request(ContextEndpoint.context(id: statusID))
.map { ($0, statusID) } .flatMap { contentDatabase.insert(context: $0, parentID: statusID) }
.flatMap(contentDatabase.insert(context:parentID:))
.eraseToAnyPublisher()) .eraseToAnyPublisher())
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }

View File

@ -21,8 +21,7 @@ public extension StatusService {
networkClient.request(status.displayStatus.favourited networkClient.request(status.displayStatus.favourited
? StatusEndpoint.unfavourite(id: status.displayStatus.id) ? StatusEndpoint.unfavourite(id: status.displayStatus.id)
: StatusEndpoint.favourite(id: status.displayStatus.id)) : StatusEndpoint.favourite(id: status.displayStatus.id))
.map { ([$0], nil) } .flatMap(contentDatabase.insert(status:))
.flatMap(contentDatabase.insert(statuses:timeline:))
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
} }