2020-08-09 07:37:04 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class IdentitiesService {
|
|
|
|
@Published var mostRecentlyUsedIdentityID: UUID?
|
|
|
|
|
2020-08-09 13:27:38 +02:00
|
|
|
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) {
|
2020-08-09 13:27:38 +02:00
|
|
|
self.identityDatabase = identityDatabase
|
2020-08-09 07:37:04 +02:00
|
|
|
self.environment = environment
|
|
|
|
|
2020-08-09 13:27:38 +02:00
|
|
|
identityDatabase.mostRecentlyUsedIdentityIDObservation()
|
2020-08-09 07:37:04 +02:00
|
|
|
.replaceError(with: nil)
|
|
|
|
.assign(to: &$mostRecentlyUsedIdentityID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension IdentitiesService {
|
|
|
|
func identityService(id: UUID) throws -> IdentityService {
|
2020-08-09 13:27:38 +02:00
|
|
|
try IdentityService(identityID: id,
|
|
|
|
identityDatabase: identityDatabase,
|
|
|
|
environment: environment)
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-09 13:27:38 +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-14 03:59:17 +02:00
|
|
|
let secretsService = SecretsService(identityID: id, keychainService: environment.keychainServiceType)
|
2020-08-09 13:27:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-14 05:40:46 +02:00
|
|
|
func deleteIdentity(_ identity: Identity) -> AnyPublisher<Void, Error> {
|
|
|
|
let secretsService = SecretsService(identityID: identity.id, keychainService: environment.keychainServiceType)
|
2020-08-21 04:29:01 +02:00
|
|
|
let networkClient = MastodonClient(environment: environment)
|
2020-08-14 05:40:46 +02:00
|
|
|
|
|
|
|
networkClient.instanceURL = identity.url
|
|
|
|
|
|
|
|
return identityDatabase.deleteIdentity(id: identity.id)
|
|
|
|
.tryMap {
|
|
|
|
DeletionEndpoint.oauthRevoke(
|
|
|
|
token: try secretsService.item(.accessToken),
|
|
|
|
clientID: try secretsService.item(.clientID),
|
|
|
|
clientSecret: try secretsService.item(.clientSecret))
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
2020-08-14 05:40:46 +02:00
|
|
|
.flatMap(networkClient.request)
|
|
|
|
.tryMap { _ in try secretsService.deleteAllItems() }
|
|
|
|
.print()
|
2020-08-09 07:37:04 +02:00
|
|
|
.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()
|
|
|
|
}
|
|
|
|
}
|