metatext/Services/StatusService.swift

28 lines
878 B
Swift
Raw Normal View History

2020-08-21 02:29:01 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
struct StatusService {
let status: Status
private let networkClient: MastodonClient
private let contentDatabase: ContentDatabase
init(status: Status, networkClient: MastodonClient, contentDatabase: ContentDatabase) {
self.status = status
self.networkClient = networkClient
self.contentDatabase = contentDatabase
}
}
2020-08-24 02:50:54 +00:00
extension StatusService {
2020-08-26 09:19:38 +00:00
func toggleFavorited() -> AnyPublisher<Never, Error> {
2020-08-24 02:50:54 +00:00
networkClient.request(status.favourited
? StatusEndpoint.unfavourite(id: status.id)
: StatusEndpoint.favourite(id: status.id))
.map { ([$0], nil) }
.flatMap(contentDatabase.insert(statuses:collection:))
.eraseToAnyPublisher()
}
}