metatext-app-ios-iphone-ipad/ServiceLayer/Sources/ServiceLayer/Services/ProfileService.swift

71 lines
2.4 KiB
Swift
Raw Normal View History

2020-09-18 02:16:41 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import DB
import Foundation
import Mastodon
import MastodonAPI
2020-09-27 04:03:53 +02:00
public struct ProfileService {
2020-10-01 04:35:06 +02:00
public let accountServicePublisher: AnyPublisher<AccountService, Error>
2020-10-06 00:50:05 +02:00
private let id: Account.Id
2020-09-18 02:16:41 +02:00
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
init(account: Account, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.init(
id: account.id,
account: account,
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase)
}
2020-10-06 00:50:05 +02:00
init(id: Account.Id, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.init(id: id, account: nil, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
private init(
2020-10-06 00:50:05 +02:00
id: Account.Id,
account: Account?,
mastodonAPIClient: MastodonAPIClient,
contentDatabase: ContentDatabase) {
2020-10-06 00:50:05 +02:00
self.id = id
2020-09-18 02:16:41 +02:00
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
2020-10-06 00:50:05 +02:00
var accountPublisher = contentDatabase.accountObservation(id: id)
if let account = account {
accountPublisher = accountPublisher
.merge(with: Just(account).setFailureType(to: Error.self))
2020-09-30 06:08:08 +02:00
.removeDuplicates()
.eraseToAnyPublisher()
}
2020-10-01 04:35:06 +02:00
accountServicePublisher = accountPublisher
2020-09-26 09:13:50 +02:00
.map { AccountService(account: $0, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) }
.eraseToAnyPublisher()
2020-09-22 08:53:11 +02:00
}
}
2020-09-22 08:53:11 +02:00
2020-09-27 04:03:53 +02:00
public extension ProfileService {
2020-10-05 09:04:15 +02:00
func timelineService(profileCollection: ProfileCollection) -> TimelineService {
TimelineService(
2020-10-06 00:50:05 +02:00
timeline: .profile(accountId: id, profileCollection: profileCollection),
2020-09-18 02:16:41 +02:00
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase)
}
func fetchPinnedStatuses() -> AnyPublisher<Never, Error> {
mastodonAPIClient.request(
StatusesEndpoint.accountsStatuses(
2020-10-06 00:50:05 +02:00
id: id,
2020-09-18 02:16:41 +02:00
excludeReplies: true,
onlyMedia: false,
pinned: true))
2020-10-06 00:50:05 +02:00
.flatMap { contentDatabase.insert(pinnedStatuses: $0, accountId: id) }
2020-09-18 02:16:41 +02:00
.eraseToAnyPublisher()
}
}