1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-11 08:55:54 +01:00
metatext-app-ios-iphone-ipad/Shared/Services/IdentitiesService.swift

89 lines
3.6 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-14 03:59:17 +02: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:))
.tryMap { accessToken -> Void in
try secretsService.set(accessToken.accessToken, forItem: .accessToken)
return ()
}
.eraseToAnyPublisher()
2020-08-09 07:37:04 +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)
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
}
.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()
}
}