metatext/Services/AllIdentitiesService.swift

84 lines
3.4 KiB
Swift
Raw Normal View History

2020-08-09 05:37:04 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-26 21:35:06 +00:00
struct AllIdentitiesService {
2020-08-26 08:40:54 +00:00
let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
2020-08-09 05:37:04 +00:00
private let identityDatabase: IdentityDatabase
2020-08-09 05:37:04 +00:00
private let environment: AppEnvironment
2020-08-12 07:24:39 +00:00
init(identityDatabase: IdentityDatabase, environment: AppEnvironment) {
self.identityDatabase = identityDatabase
2020-08-09 05:37:04 +00:00
self.environment = environment
2020-08-26 08:40:54 +00:00
mostRecentlyUsedIdentityID = identityDatabase.mostRecentlyUsedIdentityIDObservation()
2020-08-09 05:37:04 +00:00
.replaceError(with: nil)
2020-08-26 08:40:54 +00:00
.eraseToAnyPublisher()
2020-08-09 05:37:04 +00:00
}
}
2020-08-26 21:35:06 +00:00
extension AllIdentitiesService {
2020-08-09 05:37:04 +00:00
func identityService(id: UUID) throws -> IdentityService {
try IdentityService(identityID: id,
identityDatabase: identityDatabase,
environment: environment)
2020-08-09 05:37:04 +00:00
}
2020-08-26 09:19:38 +00:00
func createIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Never, Error> {
identityDatabase.createIdentity(id: id, url: instanceURL)
}
2020-08-26 09:19:38 +00:00
func authorizeIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Never, Error> {
2020-08-14 01:59:17 +00:00
let secretsService = SecretsService(identityID: id, keychainService: 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:))
2020-08-26 09:19:38 +00:00
.tryMap { try secretsService.set($0.accessToken, forItem: .accessToken) }
.ignoreOutput()
.eraseToAnyPublisher()
2020-08-09 05:37:04 +00:00
}
2020-08-26 09:19:38 +00:00
func deleteIdentity(_ identity: Identity) -> AnyPublisher<Never, Error> {
let secretsService = SecretsService(identityID: identity.id, keychainService: environment.keychainServiceType)
2020-08-21 02:29:01 +00:00
let networkClient = MastodonClient(environment: environment)
networkClient.instanceURL = identity.url
return identityDatabase.deleteIdentity(id: identity.id)
2020-08-26 09:19:38 +00:00
.tryMap { _ in
DeletionEndpoint.oauthRevoke(
token: try secretsService.item(.accessToken),
clientID: try secretsService.item(.clientID),
clientSecret: try secretsService.item(.clientSecret))
2020-08-09 05:37:04 +00:00
}
.flatMap(networkClient.request)
.tryMap { _ in try secretsService.deleteAllItems() }
2020-08-26 09:19:38 +00:00
.ignoreOutput()
2020-08-09 05:37:04 +00:00
.eraseToAnyPublisher()
}
2020-08-12 07:24:39 +00:00
2020-08-26 09:19:38 +00:00
func updatePushSubscriptions(deviceToken: String) -> AnyPublisher<Never, Error> {
2020-08-12 07:24:39 +00:00
identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken)
2020-08-26 09:19:38 +00:00
.tryMap { identities -> [AnyPublisher<Never, Never>] in
2020-08-26 08:40:54 +00:00
try identities.map {
try identityService(id: $0.id)
2020-08-14 01:24:53 +00:00
.createPushSubscription(deviceToken: deviceToken, alerts: $0.pushSubscriptionAlerts)
2020-08-26 08:40:54 +00:00
.catch { _ in Empty() } // don't want to disrupt pipeline
2020-08-14 01:24:53 +00:00
.eraseToAnyPublisher()
}
2020-08-12 07:24:39 +00:00
}
2020-08-14 01:24:53 +00:00
.map(Publishers.MergeMany.init)
2020-08-26 09:19:38 +00:00
.ignoreOutput()
2020-08-12 07:24:39 +00:00
.eraseToAnyPublisher()
}
}