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

85 lines
3.2 KiB
Swift
Raw Normal View History

2020-08-09 07:37:04 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
class IdentitiesService {
@Published var mostRecentlyUsedIdentityID: UUID?
private let identityDatabase: IdentityDatabase
2020-08-09 07:37:04 +02:00
private let environment: AppEnvironment
2020-08-12 09:24:39 +02:00
init(identityDatabase: IdentityDatabase, environment: AppEnvironment) {
self.identityDatabase = identityDatabase
2020-08-09 07:37:04 +02:00
self.environment = environment
identityDatabase.mostRecentlyUsedIdentityIDObservation()
2020-08-09 07:37:04 +02:00
.replaceError(with: nil)
.assign(to: &$mostRecentlyUsedIdentityID)
}
}
extension IdentitiesService {
func identityService(id: UUID) throws -> IdentityService {
try IdentityService(identityID: id,
identityDatabase: identityDatabase,
environment: environment)
2020-08-09 07:37:04 +02:00
}
func createIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Void, Error> {
identityDatabase.createIdentity(id: id, url: instanceURL)
}
func authorizeIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Void, Error> {
2020-08-12 09:24:39 +02:00
let secretsService = SecretsService(identityID: id, keychainServiceType: environment.keychainServiceType)
let authenticationService = AuthenticationService(environment: environment)
return authenticationService.authorizeApp(instanceURL: instanceURL)
.tryMap { appAuthorization -> (URL, AppAuthorization) in
try secretsService.set(appAuthorization.clientId, forItem: .clientID)
try secretsService.set(appAuthorization.clientSecret, forItem: .clientSecret)
return (instanceURL, appAuthorization)
}
.flatMap(authenticationService.authenticate(instanceURL:appAuthorization:))
.tryMap { accessToken -> Void in
try secretsService.set(accessToken.accessToken, forItem: .accessToken)
return ()
}
.eraseToAnyPublisher()
2020-08-09 07:37:04 +02:00
}
func deleteIdentity(id: UUID) -> AnyPublisher<Void, Error> {
2020-08-12 09:24:39 +02:00
let environment = self.environment
return identityDatabase.deleteIdentity(id: id)
.tryMap { _ -> Void in
2020-08-09 07:37:04 +02:00
try SecretsService(
identityID: id,
2020-08-12 09:24:39 +02:00
keychainServiceType: environment.keychainServiceType)
2020-08-09 07:37:04 +02:00
.deleteAllItems()
return ()
}
.eraseToAnyPublisher()
}
2020-08-12 09:24:39 +02:00
func updatePushSubscriptions(deviceToken: String) -> AnyPublisher<Void, Error> {
identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken)
2020-08-14 03:24:53 +02:00
.tryMap { [weak self] identities -> [AnyPublisher<Void, Never>] in
guard let self = self else { return [Empty().eraseToAnyPublisher()] }
return try identities.map {
try self.identityService(id: $0.id)
.createPushSubscription(deviceToken: deviceToken, alerts: $0.pushSubscriptionAlerts)
.catch { _ in Empty() } // don't want to disrupt pipeline, consider future telemetry
.eraseToAnyPublisher()
}
2020-08-12 09:24:39 +02:00
}
2020-08-14 03:24:53 +02:00
.map(Publishers.MergeMany.init)
.map { _ in () }
2020-08-12 09:24:39 +02:00
.eraseToAnyPublisher()
}
}