2020-08-09 07:37:04 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2020-09-05 04:31:43 +02:00
|
|
|
import Combine
|
2020-09-03 05:28:34 +02:00
|
|
|
import DB
|
2020-08-09 07:37:04 +02:00
|
|
|
import Foundation
|
2020-08-31 01:33:11 +02:00
|
|
|
import Mastodon
|
2020-09-04 03:55:46 +02:00
|
|
|
import MastodonAPI
|
2020-09-04 02:54:05 +02:00
|
|
|
import Secrets
|
2020-08-09 07:37:04 +02:00
|
|
|
|
2020-08-31 12:21:01 +02:00
|
|
|
public struct AllIdentitiesService {
|
|
|
|
public let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
|
2020-09-07 06:56:18 +02:00
|
|
|
public let instanceFilterService: InstanceFilterService
|
2020-08-09 07:37:04 +02:00
|
|
|
|
2020-09-07 18:33:36 +02:00
|
|
|
private let database: IdentityDatabase
|
2020-08-09 07:37:04 +02:00
|
|
|
private let environment: AppEnvironment
|
|
|
|
|
2020-08-31 12:21:01 +02:00
|
|
|
public init(environment: AppEnvironment) throws {
|
2020-09-07 18:33:36 +02:00
|
|
|
self.database = try IdentityDatabase(inMemory: environment.inMemoryContent,
|
2020-09-04 08:12:06 +02:00
|
|
|
fixture: environment.identityFixture,
|
|
|
|
keychain: environment.keychain)
|
2020-08-09 07:37:04 +02:00
|
|
|
self.environment = environment
|
|
|
|
|
2020-09-07 18:33:36 +02:00
|
|
|
mostRecentlyUsedIdentityID = database.mostRecentlyUsedIdentityIDObservation()
|
2020-08-09 07:37:04 +02:00
|
|
|
.replaceError(with: nil)
|
2020-08-26 10:40:54 +02:00
|
|
|
.eraseToAnyPublisher()
|
2020-09-07 06:56:18 +02:00
|
|
|
instanceFilterService = InstanceFilterService(environment: environment)
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:21:01 +02:00
|
|
|
public extension AllIdentitiesService {
|
2020-09-07 18:33:36 +02:00
|
|
|
func identifiedEnvironment(id: UUID) throws -> IdentifiedEnvironment {
|
|
|
|
try IdentifiedEnvironment(id: id, database: database, 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-09-07 18:33:36 +02:00
|
|
|
database.createIdentity(id: id, url: instanceURL)
|
2020-08-09 13:27:38 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:19:38 +02:00
|
|
|
func authorizeIdentity(id: UUID, instanceURL: URL) -> AnyPublisher<Never, Error> {
|
2020-09-04 02:54:05 +02:00
|
|
|
let secrets = Secrets(identityID: id, keychain: environment.keychain)
|
2020-08-09 13:27:38 +02:00
|
|
|
let authenticationService = AuthenticationService(environment: environment)
|
|
|
|
|
|
|
|
return authenticationService.authorizeApp(instanceURL: instanceURL)
|
|
|
|
.tryMap { appAuthorization -> (URL, AppAuthorization) in
|
2020-09-04 08:44:04 +02:00
|
|
|
try secrets.setClientID(appAuthorization.clientId)
|
|
|
|
try secrets.setClientSecret(appAuthorization.clientSecret)
|
2020-08-09 13:27:38 +02:00
|
|
|
|
|
|
|
return (instanceURL, appAuthorization)
|
|
|
|
}
|
|
|
|
.flatMap(authenticationService.authenticate(instanceURL:appAuthorization:))
|
2020-09-04 08:44:04 +02:00
|
|
|
.tryMap { try secrets.setAccessToken($0.accessToken) }
|
2020-08-26 11:19:38 +02:00
|
|
|
.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-09-04 02:54:05 +02:00
|
|
|
let secrets = Secrets(identityID: identity.id, keychain: environment.keychain)
|
2020-09-04 03:55:46 +02:00
|
|
|
let mastodonAPIClient = MastodonAPIClient(session: environment.session)
|
2020-08-14 05:40:46 +02:00
|
|
|
|
2020-09-04 03:55:46 +02:00
|
|
|
mastodonAPIClient.instanceURL = identity.url
|
2020-08-14 05:40:46 +02:00
|
|
|
|
2020-09-07 18:33:36 +02:00
|
|
|
return database.deleteIdentity(id: identity.id)
|
2020-09-03 03:14:33 +02:00
|
|
|
.collect()
|
2020-08-26 11:19:38 +02:00
|
|
|
.tryMap { _ in
|
2020-08-14 05:40:46 +02:00
|
|
|
DeletionEndpoint.oauthRevoke(
|
2020-09-04 08:44:04 +02:00
|
|
|
token: try secrets.getAccessToken(),
|
|
|
|
clientID: try secrets.getClientID(),
|
|
|
|
clientSecret: try secrets.getClientSecret())
|
2020-08-09 07:37:04 +02:00
|
|
|
}
|
2020-09-04 03:55:46 +02:00
|
|
|
.flatMap(mastodonAPIClient.request)
|
2020-09-03 03:14:33 +02:00
|
|
|
.collect()
|
|
|
|
.tryMap { _ in
|
2020-09-04 02:54:05 +02:00
|
|
|
try secrets.deleteAllItems()
|
2020-09-03 03:14:33 +02:00
|
|
|
try ContentDatabase.delete(forIdentityID: identity.id)
|
|
|
|
}
|
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-09-06 23:37:54 +02:00
|
|
|
func updatePushSubscriptions(deviceToken: Data) -> AnyPublisher<Never, Error> {
|
2020-09-07 18:33:36 +02:00
|
|
|
database.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 {
|
2020-09-07 18:33:36 +02:00
|
|
|
try IdentityService(id: $0.id, instanceURL: $0.url, database: database, environment: environment)
|
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()
|
|
|
|
}
|
|
|
|
}
|