2020-08-09 07:37:04 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
2020-08-26 23:35:06 +02:00
|
|
|
struct AllIdentitiesService {
|
2020-08-26 10:40:54 +02:00
|
|
|
let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
|
2020-08-09 07:37:04 +02:00
|
|
|
|
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-26 10:40:54 +02:00
|
|
|
mostRecentlyUsedIdentityID = identityDatabase.mostRecentlyUsedIdentityIDObservation()
|
2020-08-09 07:37:04 +02:00
|
|
|
.replaceError(with: nil)
|
2020-08-26 10:40:54 +02:00
|
|
|
.eraseToAnyPublisher()
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 23:35:06 +02:00
|
|
|
extension AllIdentitiesService {
|
2020-08-09 07:37:04 +02:00
|
|
|
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-26 11:19:38 +02:00
|
|
|
func createIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Never, Error> {
|
2020-08-09 13:27:38 +02:00
|
|
|
identityDatabase.createIdentity(id: id, url: instanceURL)
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:19:38 +02:00
|
|
|
func authorizeIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Never, 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:))
|
2020-08-26 11:19:38 +02:00
|
|
|
.tryMap { try secretsService.set($0.accessToken, forItem: .accessToken) }
|
|
|
|
.ignoreOutput()
|
2020-08-09 13:27:38 +02:00
|
|
|
.eraseToAnyPublisher()
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:19:38 +02:00
|
|
|
func deleteIdentity(_ identity: Identity) -> AnyPublisher<Never, Error> {
|
2020-08-14 05:40:46 +02:00
|
|
|
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)
|
2020-08-26 11:19:38 +02:00
|
|
|
.tryMap { _ in
|
2020-08-14 05:40:46 +02:00
|
|
|
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() }
|
2020-08-26 11:19:38 +02:00
|
|
|
.ignoreOutput()
|
2020-08-09 07:37:04 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-12 09:24:39 +02:00
|
|
|
|
2020-08-26 11:19:38 +02:00
|
|
|
func updatePushSubscriptions(deviceToken: String) -> AnyPublisher<Never, Error> {
|
2020-08-12 09:24:39 +02:00
|
|
|
identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken)
|
2020-08-26 11:19:38 +02:00
|
|
|
.tryMap { identities -> [AnyPublisher<Never, Never>] in
|
2020-08-26 10:40:54 +02:00
|
|
|
try identities.map {
|
|
|
|
try identityService(id: $0.id)
|
2020-08-14 03:24:53 +02:00
|
|
|
.createPushSubscription(deviceToken: deviceToken, alerts: $0.pushSubscriptionAlerts)
|
2020-08-26 10:40:54 +02:00
|
|
|
.catch { _ in Empty() } // don't want to disrupt pipeline
|
2020-08-14 03:24:53 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-12 09:24:39 +02:00
|
|
|
}
|
2020-08-14 03:24:53 +02:00
|
|
|
.map(Publishers.MergeMany.init)
|
2020-08-26 11:19:38 +02:00
|
|
|
.ignoreOutput()
|
2020-08-12 09:24:39 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|