2020-08-21 02:29:01 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
2020-09-03 03:28:34 +00:00
|
|
|
import DB
|
2020-09-05 02:31:43 +00:00
|
|
|
import Foundation
|
2020-08-30 23:33:11 +00:00
|
|
|
import Mastodon
|
2020-09-04 01:55:46 +00:00
|
|
|
import MastodonAPI
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public struct StatusService {
|
|
|
|
public let status: Status
|
2020-09-25 05:39:06 +00:00
|
|
|
public let navigationService: NavigationService
|
2020-09-04 01:55:46 +00:00
|
|
|
private let mastodonAPIClient: MastodonAPIClient
|
2020-08-21 02:29:01 +00:00
|
|
|
private let contentDatabase: ContentDatabase
|
|
|
|
|
2020-09-15 01:39:35 +00:00
|
|
|
init(status: Status, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
|
2020-08-21 02:29:01 +00:00
|
|
|
self.status = status
|
2020-09-25 05:39:06 +00:00
|
|
|
self.navigationService = NavigationService(
|
2020-09-15 01:39:35 +00:00
|
|
|
mastodonAPIClient: mastodonAPIClient,
|
2020-10-05 19:58:03 +00:00
|
|
|
contentDatabase: contentDatabase,
|
|
|
|
status: status.displayStatus)
|
2020-09-15 01:39:35 +00:00
|
|
|
self.mastodonAPIClient = mastodonAPIClient
|
2020-08-21 02:29:01 +00:00
|
|
|
self.contentDatabase = contentDatabase
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public extension StatusService {
|
2020-10-14 00:03:01 +00:00
|
|
|
func toggleShowContent() -> AnyPublisher<Never, Error> {
|
|
|
|
contentDatabase.toggleShowContent(id: status.displayStatus.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleShowAttachments() -> AnyPublisher<Never, Error> {
|
|
|
|
contentDatabase.toggleShowAttachments(id: status.displayStatus.id)
|
2020-10-07 21:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func toggleFavorited() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(status.displayStatus.favourited
|
|
|
|
? StatusEndpoint.unfavourite(id: status.displayStatus.id)
|
|
|
|
: StatusEndpoint.favourite(id: status.displayStatus.id))
|
2020-09-02 09:39:44 +00:00
|
|
|
.flatMap(contentDatabase.insert(status:))
|
2020-08-24 02:50:54 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-09-25 05:39:06 +00:00
|
|
|
|
2020-09-28 23:23:41 +00:00
|
|
|
func rebloggedByService() -> AccountListService {
|
|
|
|
AccountListService(
|
2020-10-05 22:50:05 +00:00
|
|
|
endpoint: .rebloggedBy(id: status.id),
|
2020-09-28 23:23:41 +00:00
|
|
|
mastodonAPIClient: mastodonAPIClient,
|
|
|
|
contentDatabase: contentDatabase)
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:39:06 +00:00
|
|
|
func favoritedByService() -> AccountListService {
|
|
|
|
AccountListService(
|
2020-10-05 22:50:05 +00:00
|
|
|
endpoint: .favouritedBy(id: status.id),
|
2020-09-25 05:39:06 +00:00
|
|
|
mastodonAPIClient: mastodonAPIClient,
|
|
|
|
contentDatabase: contentDatabase)
|
|
|
|
}
|
2020-10-25 02:31:44 +00:00
|
|
|
|
|
|
|
func vote(selectedOptions: Set<Int>) -> AnyPublisher<Never, Error> {
|
|
|
|
guard let poll = status.displayStatus.poll else { return Empty().eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
return mastodonAPIClient.request(PollEndpoint.votes(id: poll.id, choices: Array(selectedOptions)))
|
|
|
|
.flatMap { contentDatabase.update(id: status.displayStatus.id, poll: $0) }
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPoll() -> AnyPublisher<Never, Error> {
|
|
|
|
guard let poll = status.displayStatus.poll else { return Empty().eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
return mastodonAPIClient.request(PollEndpoint.poll(id: poll.id))
|
|
|
|
.flatMap { contentDatabase.update(id: status.displayStatus.id, poll: $0) }
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|