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

202 lines
8.2 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 environment: AppEnvironment
private let mastodonAPIClient: MastodonAPIClient
2020-08-21 02:29:01 +00:00
private let contentDatabase: ContentDatabase
init(environment: AppEnvironment,
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(
environment: environment,
2020-09-15 01:39:35 +00:00
mastodonAPIClient: mastodonAPIClient,
2020-10-05 19:58:03 +00:00
contentDatabase: contentDatabase,
status: status.displayStatus)
self.environment = environment
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
}
func toggleReblogged(identityId: Identity.Id?) -> AnyPublisher<Never, Error> {
if let identityId = identityId {
return request(identityId: identityId, endpointClosure: StatusEndpoint.reblog(id:))
} else {
return mastodonAPIClient.request(status.displayStatus.reblogged
? StatusEndpoint.unreblog(id: status.displayStatus.id)
: StatusEndpoint.reblog(id: status.displayStatus.id))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
2021-01-04 01:51:52 +00:00
}
func toggleFavorited(identityId: Identity.Id?) -> AnyPublisher<Never, Error> {
if let identityId = identityId {
return request(identityId: identityId, endpointClosure: StatusEndpoint.favourite(id:))
} else {
return mastodonAPIClient.request(status.displayStatus.favourited
? StatusEndpoint.unfavourite(id: status.displayStatus.id)
: StatusEndpoint.favourite(id: status.displayStatus.id))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
2020-08-24 02:50:54 +00:00
}
2020-09-25 05:39:06 +00:00
2020-12-01 23:53:14 +00:00
func toggleBookmarked() -> AnyPublisher<Never, Error> {
mastodonAPIClient.request(status.displayStatus.bookmarked
? StatusEndpoint.unbookmark(id: status.displayStatus.id)
: StatusEndpoint.bookmark(id: status.displayStatus.id))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
2021-01-11 03:12:06 +00:00
func togglePinned() -> AnyPublisher<Never, Error> {
mastodonAPIClient.request(status.displayStatus.pinned ?? false
? StatusEndpoint.unpin(id: status.displayStatus.id)
: StatusEndpoint.pin(id: status.displayStatus.id))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
func toggleMuted() -> AnyPublisher<Never, Error> {
mastodonAPIClient.request(status.displayStatus.muted
? StatusEndpoint.unmute(id: status.displayStatus.id)
: StatusEndpoint.mute(id: status.displayStatus.id))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
2021-01-11 22:45:30 +00:00
func delete() -> AnyPublisher<Status, Error> {
mastodonAPIClient.request(StatusEndpoint.delete(id: status.displayStatus.id))
.flatMap { status in contentDatabase.delete(id: status.id).collect().map { _ in status } }
.eraseToAnyPublisher()
}
func deleteAndRedraft() -> AnyPublisher<(Status, Self?), Error> {
let inReplyToPublisher: AnyPublisher<Self?, Never>
if let inReplyToId = status.displayStatus.inReplyToId {
inReplyToPublisher = mastodonAPIClient.request(StatusEndpoint.status(id: inReplyToId))
.map {
Self(environment: environment,
status: $0,
2021-01-11 22:45:30 +00:00
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase) as Self?
}
.replaceError(with: nil)
.eraseToAnyPublisher()
} else {
inReplyToPublisher = Just(nil).eraseToAnyPublisher()
}
return mastodonAPIClient.request(StatusEndpoint.delete(id: status.displayStatus.id))
.flatMap { status in contentDatabase.delete(id: status.id).collect().map { _ in status } }
.zip(inReplyToPublisher.setFailureType(to: Error.self))
.eraseToAnyPublisher()
}
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),
environment: environment,
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),
environment: environment,
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()
}
2021-03-21 23:23:41 +00:00
func asIdentity(id: Identity.Id) -> AnyPublisher<Self, Error> {
fetchAs(identityId: id).tryMap {
Self(environment: environment,
status: $0,
mastodonAPIClient: try MastodonAPIClient.forIdentity(id: id, environment: environment),
contentDatabase: try ContentDatabase(
id: id,
useHomeTimelineLastReadId: true,
inMemory: environment.inMemoryContent,
appGroup: AppEnvironment.appGroup,
keychain: environment.keychain)) }
.eraseToAnyPublisher()
}
2020-08-24 02:50:54 +00:00
}
private extension StatusService {
2021-03-21 23:23:41 +00:00
func fetchAs(identityId: Identity.Id) -> AnyPublisher<Status, Error> {
let client: MastodonAPIClient
do {
client = try MastodonAPIClient.forIdentity(id: identityId, environment: environment)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}
return client
2021-05-10 05:10:14 +00:00
.request(ResultsEndpoint.search(.init(query: status.displayStatus.uri, limit: 1)))
.tryMap {
2021-03-21 23:23:41 +00:00
guard let status = $0.statuses.first else { throw APIError.unableToFetchRemoteStatus }
2021-03-21 23:23:41 +00:00
return status
}
2021-03-21 23:23:41 +00:00
.eraseToAnyPublisher()
}
func request(identityId: Identity.Id,
endpointClosure: @escaping (Status.Id) -> StatusEndpoint) -> AnyPublisher<Never, Error> {
let client: MastodonAPIClient
do {
client = try MastodonAPIClient.forIdentity(id: identityId, environment: environment)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}
return fetchAs(identityId: identityId)
.flatMap { client.request(endpointClosure($0.id)) }
.flatMap { _ in mastodonAPIClient.request(StatusEndpoint.status(id: status.displayStatus.id)) }
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
}