diff --git a/ServiceLayer/Sources/ServiceLayer/Database/ContentDatabase.swift b/ServiceLayer/Sources/ServiceLayer/Database/ContentDatabase.swift index d83333b..b8fe49b 100644 --- a/ServiceLayer/Sources/ServiceLayer/Database/ContentDatabase.swift +++ b/ServiceLayer/Sources/ServiceLayer/Database/ContentDatabase.swift @@ -28,18 +28,20 @@ struct ContentDatabase { } extension ContentDatabase { - func insert(statuses: [Status], timeline: Timeline? = nil) -> AnyPublisher { + func insert(status: Status) -> AnyPublisher { + databaseQueue.writePublisher(updates: status.save) + .ignoreOutput() + .eraseToAnyPublisher() + } + + func insert(statuses: [Status], timeline: Timeline) -> AnyPublisher { 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 { 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) { diff --git a/ServiceLayer/Sources/ServiceLayer/StatusListService.swift b/ServiceLayer/Sources/ServiceLayer/StatusListService.swift index ec53d78..beffcfd 100644 --- a/ServiceLayer/Sources/ServiceLayer/StatusListService.swift +++ b/ServiceLayer/Sources/ServiceLayer/StatusListService.swift @@ -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() } diff --git a/ServiceLayer/Sources/ServiceLayer/StatusService.swift b/ServiceLayer/Sources/ServiceLayer/StatusService.swift index 8a22148..6be66fa 100644 --- a/ServiceLayer/Sources/ServiceLayer/StatusService.swift +++ b/ServiceLayer/Sources/ServiceLayer/StatusService.swift @@ -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() } }