metatext/Shared/Services/IdentityService.swift

146 lines
5.6 KiB
Swift
Raw Normal View History

2020-08-07 21:57:18 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-08 23:08:47 +00:00
class IdentityService {
2020-08-09 08:04:43 +00:00
@Published private(set) var identity: Identity
2020-08-07 21:57:18 +00:00
let observationErrors: AnyPublisher<Error, Never>
private let identityDatabase: IdentityDatabase
2020-08-18 05:13:37 +00:00
private let contentDatabase: ContentDatabase
2020-08-09 05:37:04 +00:00
private let environment: AppEnvironment
private let networkClient: MastodonClient
2020-08-14 01:24:53 +00:00
private let secretsService: SecretsService
2020-08-07 21:57:18 +00:00
private let observationErrorsInput = PassthroughSubject<Error, Never>()
init(identityID: UUID,
identityDatabase: IdentityDatabase,
environment: AppEnvironment) throws {
self.identityDatabase = identityDatabase
2020-08-09 05:37:04 +00:00
self.environment = environment
2020-08-07 21:57:18 +00:00
observationErrors = observationErrorsInput.eraseToAnyPublisher()
let observation = identityDatabase.identityObservation(id: identityID).share()
2020-08-07 21:57:18 +00:00
var initialIdentity: Identity?
2020-08-09 08:04:43 +00:00
_ = observation.first().sink(
2020-08-07 21:57:18 +00:00
receiveCompletion: { _ in },
receiveValue: { initialIdentity = $0 })
guard let identity = initialIdentity else { throw IdentityDatabaseError.identityNotFound }
self.identity = identity
2020-08-14 01:24:53 +00:00
secretsService = SecretsService(
2020-08-09 08:04:43 +00:00
identityID: identityID,
2020-08-14 01:59:17 +00:00
keychainService: environment.keychainServiceType)
2020-08-21 02:29:01 +00:00
networkClient = MastodonClient(environment: environment)
2020-08-14 01:24:53 +00:00
networkClient.instanceURL = identity.url
networkClient.accessToken = try? secretsService.item(.accessToken)
2020-08-07 21:57:18 +00:00
2020-08-21 02:29:01 +00:00
contentDatabase = try ContentDatabase(identityID: identityID, environment: environment)
2020-08-18 05:13:37 +00:00
2020-08-07 21:57:18 +00:00
observation.catch { [weak self] error -> Empty<Identity, Never> in
self?.observationErrorsInput.send(error)
return Empty()
}
.assign(to: &$identity)
}
}
2020-08-08 23:08:47 +00:00
extension IdentityService {
2020-08-07 21:57:18 +00:00
var isAuthorized: Bool { networkClient.accessToken != nil }
2020-08-26 09:19:38 +00:00
func updateLastUse() -> AnyPublisher<Never, Error> {
identityDatabase.updateLastUsedAt(identityID: identity.id)
2020-08-09 05:37:04 +00:00
}
2020-08-26 09:19:38 +00:00
func verifyCredentials() -> AnyPublisher<Never, Error> {
2020-08-07 21:57:18 +00:00
networkClient.request(AccountEndpoint.verifyCredentials)
2020-08-12 08:45:01 +00:00
.zip(Just(identity.id).first().setFailureType(to: Error.self))
.flatMap(identityDatabase.updateAccount)
2020-08-07 21:57:18 +00:00
.eraseToAnyPublisher()
}
2020-08-26 09:19:38 +00:00
func refreshServerPreferences() -> AnyPublisher<Never, Error> {
2020-08-07 21:57:18 +00:00
networkClient.request(PreferencesEndpoint.preferences)
2020-08-12 08:45:01 +00:00
.zip(Just(self).first().setFailureType(to: Error.self))
2020-08-07 21:57:18 +00:00
.map { ($1.identity.preferences.updated(from: $0), $1.identity.id) }
.flatMap(identityDatabase.updatePreferences)
2020-08-07 21:57:18 +00:00
.eraseToAnyPublisher()
}
2020-08-26 09:19:38 +00:00
func refreshInstance() -> AnyPublisher<Never, Error> {
2020-08-07 21:57:18 +00:00
networkClient.request(InstanceEndpoint.instance)
2020-08-12 08:45:01 +00:00
.zip(Just(identity.id).first().setFailureType(to: Error.self))
.flatMap(identityDatabase.updateInstance)
2020-08-07 21:57:18 +00:00
.eraseToAnyPublisher()
}
func identitiesObservation() -> AnyPublisher<[Identity], Error> {
identityDatabase.identitiesObservation()
2020-08-07 21:57:18 +00:00
}
func recentIdentitiesObservation() -> AnyPublisher<[Identity], Error> {
identityDatabase.recentIdentitiesObservation(excluding: identity.id)
2020-08-07 21:57:18 +00:00
}
2020-08-26 09:19:38 +00:00
func updatePreferences(_ preferences: Identity.Preferences) -> AnyPublisher<Never, Error> {
identityDatabase.updatePreferences(preferences, forIdentityID: identity.id)
2020-08-15 00:14:21 +00:00
.zip(Just(self).first().setFailureType(to: Error.self))
.filter { $1.identity.preferences.useServerPostingReadingPreferences }
.map { _ in () }
.flatMap(refreshServerPreferences)
.eraseToAnyPublisher()
2020-08-07 21:57:18 +00:00
}
2020-08-14 01:24:53 +00:00
2020-08-26 09:19:38 +00:00
func createPushSubscription(deviceToken: String, alerts: PushSubscription.Alerts) -> AnyPublisher<Never, Error> {
2020-08-14 01:24:53 +00:00
let publicKey: String
let auth: String
do {
publicKey = try secretsService.generatePushKeyAndReturnPublicKey().base64EncodedString()
auth = try secretsService.generatePushAuth().base64EncodedString()
} catch {
return Fail(error: error).eraseToAnyPublisher()
}
let identityID = identity.id
let endpoint = Self.pushSubscriptionEndpointURL
.appendingPathComponent(deviceToken)
.appendingPathComponent(identityID.uuidString)
return networkClient.request(
PushSubscriptionEndpoint.create(
endpoint: endpoint,
publicKey: publicKey,
auth: auth,
alerts: alerts))
2020-08-14 21:41:55 +00:00
.map { ($0.alerts, deviceToken, identityID) }
.flatMap(identityDatabase.updatePushSubscription(alerts:deviceToken:forIdentityID:))
.eraseToAnyPublisher()
}
2020-08-26 09:19:38 +00:00
func updatePushSubscription(alerts: PushSubscription.Alerts) -> AnyPublisher<Never, Error> {
2020-08-14 21:41:55 +00:00
let identityID = identity.id
return networkClient.request(PushSubscriptionEndpoint.update(alerts: alerts))
.map { ($0.alerts, nil, identityID) }
.flatMap(identityDatabase.updatePushSubscription(alerts:deviceToken:forIdentityID:))
2020-08-14 01:24:53 +00:00
.eraseToAnyPublisher()
}
2020-08-18 05:13:37 +00:00
func service(timeline: Timeline) -> StatusListService {
TimelineService(timeline: timeline, networkClient: networkClient, contentDatabase: contentDatabase)
}
2020-08-14 01:24:53 +00:00
}
private extension IdentityService {
#if DEBUG
static let pushSubscriptionEndpointURL = URL(string: "https://metatext-apns.metabolist.com/push?sandbox=true")!
#else
static let pushSubscriptionEndpointURL = URL(string: "https://metatext-apns.metabolist.com/push")!
#endif
2020-08-07 21:57:18 +00:00
}