metatext/ServiceLayer/Sources/ServiceLayer/Services/StatusService.swift

53 lines
1.8 KiB
Swift
Raw Normal View History

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
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
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-07 21:06:26 +00:00
func toggleShowMore() -> AnyPublisher<Never, Error> {
contentDatabase.toggleShowMore(id: status.displayStatus.id)
}
2020-08-26 09:19:38 +00:00
func toggleFavorited() -> AnyPublisher<Never, Error> {
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-08-24 02:50:54 +00:00
}