metatext-app-ios-iphone-ipad/Shared/Services/IdentityService.swift

92 lines
3.3 KiB
Swift
Raw Normal View History

2020-08-07 23:57:18 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-09 01:08:47 +02:00
class IdentityService {
2020-08-09 10:04:43 +02:00
@Published private(set) var identity: Identity
2020-08-07 23:57:18 +02:00
let observationErrors: AnyPublisher<Error, Never>
private let identityDatabase: IdentityDatabase
2020-08-09 07:37:04 +02:00
private let environment: AppEnvironment
private let networkClient: MastodonClient
2020-08-07 23:57:18 +02:00
private let observationErrorsInput = PassthroughSubject<Error, Never>()
init(identityID: UUID,
identityDatabase: IdentityDatabase,
keychainService: KeychainServiceType,
environment: AppEnvironment) throws {
self.identityDatabase = identityDatabase
2020-08-09 07:37:04 +02:00
self.environment = environment
2020-08-07 23:57:18 +02:00
observationErrors = observationErrorsInput.eraseToAnyPublisher()
let observation = identityDatabase.identityObservation(id: identityID).share()
2020-08-07 23:57:18 +02:00
var initialIdentity: Identity?
2020-08-09 10:04:43 +02:00
_ = observation.first().sink(
2020-08-07 23:57:18 +02:00
receiveCompletion: { _ in },
receiveValue: { initialIdentity = $0 })
guard let identity = initialIdentity else { throw IdentityDatabaseError.identityNotFound }
self.identity = identity
2020-08-09 10:04:43 +02:00
networkClient = MastodonClient(configuration: environment.URLSessionConfiguration)
2020-08-07 23:57:18 +02:00
networkClient.instanceURL = identity.url
2020-08-09 10:04:43 +02:00
networkClient.accessToken = try SecretsService(
identityID: identityID,
keychainService: keychainService)
2020-08-09 10:04:43 +02:00
.item(.accessToken)
2020-08-07 23:57:18 +02:00
observation.catch { [weak self] error -> Empty<Identity, Never> in
self?.observationErrorsInput.send(error)
return Empty()
}
.assign(to: &$identity)
}
}
2020-08-09 01:08:47 +02:00
extension IdentityService {
2020-08-07 23:57:18 +02:00
var isAuthorized: Bool { networkClient.accessToken != nil }
2020-08-09 07:37:04 +02:00
func updateLastUse() -> AnyPublisher<Void, Error> {
identityDatabase.updateLastUsedAt(identityID: identity.id)
2020-08-09 07:37:04 +02:00
}
2020-08-07 23:57:18 +02:00
func verifyCredentials() -> AnyPublisher<Void, Error> {
networkClient.request(AccountEndpoint.verifyCredentials)
.continuingIfWeakReferenceIsStillAlive(to: self)
.map { ($0, $1.identity.id) }
.flatMap(identityDatabase.updateAccount)
2020-08-07 23:57:18 +02:00
.eraseToAnyPublisher()
}
func refreshServerPreferences() -> AnyPublisher<Void, Error> {
networkClient.request(PreferencesEndpoint.preferences)
.continuingIfWeakReferenceIsStillAlive(to: self)
.map { ($1.identity.preferences.updated(from: $0), $1.identity.id) }
.flatMap(identityDatabase.updatePreferences)
2020-08-07 23:57:18 +02:00
.eraseToAnyPublisher()
}
func refreshInstance() -> AnyPublisher<Void, Error> {
networkClient.request(InstanceEndpoint.instance)
.continuingIfWeakReferenceIsStillAlive(to: self)
.map { ($0, $1.identity.id) }
.flatMap(identityDatabase.updateInstance)
2020-08-07 23:57:18 +02:00
.eraseToAnyPublisher()
}
func identitiesObservation() -> AnyPublisher<[Identity], Error> {
identityDatabase.identitiesObservation()
2020-08-07 23:57:18 +02:00
}
func recentIdentitiesObservation() -> AnyPublisher<[Identity], Error> {
identityDatabase.recentIdentitiesObservation(excluding: identity.id)
2020-08-07 23:57:18 +02:00
}
func updatePreferences(_ preferences: Identity.Preferences) -> AnyPublisher<Void, Error> {
identityDatabase.updatePreferences(preferences, forIdentityID: identity.id)
2020-08-07 23:57:18 +02:00
}
}