metatext/ServiceLayer/Sources/ServiceLayer/Services/AllIdentitiesService.swift

160 lines
5.5 KiB
Swift
Raw Normal View History

2020-08-09 05:37:04 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2020-09-05 02:31:43 +00:00
import Combine
2020-09-03 03:28:34 +00:00
import DB
2020-08-09 05:37:04 +00:00
import Foundation
2020-08-30 23:33:11 +00:00
import Mastodon
import MastodonAPI
2020-09-04 00:54:05 +00:00
import Secrets
2020-08-09 05:37:04 +00:00
2020-08-31 10:21:01 +00:00
public struct AllIdentitiesService {
2020-10-05 22:50:05 +00:00
public let identitiesCreated: AnyPublisher<Identity.Id, Never>
2020-09-13 00:50:22 +00:00
2020-08-09 05:37:04 +00:00
private let environment: AppEnvironment
2020-09-09 06:17:35 +00:00
private let database: IdentityDatabase
2020-10-05 22:50:05 +00:00
private let identitiesCreatedSubject = PassthroughSubject<Identity.Id, Never>()
2020-08-09 05:37:04 +00:00
2020-08-31 10:21:01 +00:00
public init(environment: AppEnvironment) throws {
2020-09-09 06:17:35 +00:00
self.environment = environment
2020-09-08 02:12:38 +00:00
self.database = try environment.fixtureDatabase ?? IdentityDatabase(
inMemory: environment.inMemoryContent,
2020-11-09 03:07:23 +00:00
appGroup: AppEnvironment.appGroup,
2020-09-08 02:12:38 +00:00
keychain: environment.keychain)
2020-09-13 00:50:22 +00:00
identitiesCreated = identitiesCreatedSubject.eraseToAnyPublisher()
2020-08-09 05:37:04 +00:00
}
}
2020-08-31 10:21:01 +00:00
public extension AllIdentitiesService {
2020-09-13 08:03:08 +00:00
enum IdentityCreation {
case authentication
case registration(Registration)
case browsing
}
2020-10-05 22:50:05 +00:00
func identityService(id: Identity.Id) throws -> IdentityService {
2020-09-08 02:12:38 +00:00
try IdentityService(id: id, database: database, environment: environment)
2020-08-09 05:37:04 +00:00
}
2020-10-06 20:44:22 +00:00
func immediateMostRecentlyUsedIdentityIdPublisher() -> AnyPublisher<Identity.Id?, Error> {
database.immediateMostRecentlyUsedIdentityIdPublisher()
2020-09-09 12:05:43 +00:00
}
2020-12-10 02:44:06 +00:00
func authenticatedIdentitiesPublisher() -> AnyPublisher<[Identity], Error> {
database.authenticatedIdentitiesPublisher()
}
func mostRecentAuthenticatedIdentity() throws -> Identity? {
try database.mostRecentAuthenticatedIdentity()
}
2020-09-13 08:03:08 +00:00
func createIdentity(url: URL, kind: IdentityCreation) -> AnyPublisher<Never, Error> {
let id = environment.uuid()
2020-10-05 22:50:05 +00:00
let secrets = Secrets(identityId: id, keychain: environment.keychain)
2020-08-09 05:37:04 +00:00
2020-09-13 08:03:08 +00:00
do {
try secrets.setInstanceURL(url)
} catch {
return Fail(error: error).eraseToAnyPublisher()
}
let createIdentityPublisher = database.createIdentity(
id: id,
2020-09-11 09:55:06 +00:00
url: url,
2020-09-13 08:03:08 +00:00
authenticated: kind.authenticated,
pending: kind.pending)
.ignoreOutput()
.handleEvents(receiveCompletion: {
if case .finished = $0 {
identitiesCreatedSubject.send(id)
}
})
.eraseToAnyPublisher()
let authenticationPublisher: AnyPublisher<(AppAuthorization, AccessToken), Error>
switch kind {
case .authentication:
authenticationPublisher = AuthenticationService(url: url, environment: environment)
.authenticate()
case let .registration(registration):
authenticationPublisher = AuthenticationService(url: url, environment: environment)
.register(registration, id: id)
case .browsing:
return createIdentityPublisher
}
return authenticationPublisher
.tryMap {
2020-10-05 22:50:05 +00:00
try secrets.setClientId($0.clientId)
2020-09-13 08:03:08 +00:00
try secrets.setClientSecret($0.clientSecret)
try secrets.setAccessToken($1.accessToken)
}
.flatMap { createIdentityPublisher }
.eraseToAnyPublisher()
2020-09-09 05:40:49 +00:00
}
2020-10-05 22:50:05 +00:00
func deleteIdentity(id: Identity.Id) -> AnyPublisher<Never, Error> {
2020-09-09 05:40:49 +00:00
database.deleteIdentity(id: id)
2020-09-03 01:14:33 +00:00
.collect()
2020-09-09 05:40:49 +00:00
.tryMap { _ -> AnyPublisher<Never, Error> in
2020-11-09 03:07:23 +00:00
try ContentDatabase.delete(id: id, appGroup: AppEnvironment.appGroup)
2020-09-09 05:40:49 +00:00
2020-10-05 22:50:05 +00:00
let secrets = Secrets(identityId: id, keychain: environment.keychain)
2020-09-09 05:40:49 +00:00
defer { secrets.deleteAllItems() }
do {
return MastodonAPIClient(
session: environment.session,
instanceURL: try secrets.getInstanceURL())
2020-12-03 05:18:47 +00:00
.request(EmptyEndpoint.oauthRevoke(
2020-09-09 05:40:49 +00:00
token: try secrets.getAccessToken(),
2020-10-05 22:50:05 +00:00
clientId: try secrets.getClientId(),
2020-09-09 05:40:49 +00:00
clientSecret: try secrets.getClientSecret()))
.ignoreOutput()
.eraseToAnyPublisher()
} catch {
return Empty().eraseToAnyPublisher()
}
2020-09-03 01:14:33 +00:00
}
2020-09-09 05:40:49 +00:00
.flatMap { $0 }
2020-08-09 05:37:04 +00:00
.eraseToAnyPublisher()
}
2020-08-12 07:24:39 +00:00
2020-09-06 21:37:54 +00:00
func updatePushSubscriptions(deviceToken: Data) -> AnyPublisher<Never, Error> {
2020-10-06 20:44:22 +00:00
database.fetchIdentitiesWithOutdatedDeviceTokens(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 {
2020-09-08 02:12:38 +00:00
try IdentityService(id: $0.id, database: database, environment: environment)
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-11-21 23:21:04 +00:00
.flatMap { $0 }
2020-08-26 09:19:38 +00:00
.ignoreOutput()
2020-08-12 07:24:39 +00:00
.eraseToAnyPublisher()
}
}
2020-09-11 09:55:06 +00:00
2020-09-13 08:03:08 +00:00
private extension AllIdentitiesService.IdentityCreation {
var authenticated: Bool {
switch self {
case .authentication, .registration:
return true
case .browsing:
return false
2020-09-11 09:55:06 +00:00
}
2020-09-13 08:03:08 +00:00
}
2020-09-11 09:55:06 +00:00
2020-09-13 08:03:08 +00:00
var pending: Bool {
switch self {
case .registration:
return true
case .authentication, .browsing:
return false
2020-09-11 09:55:06 +00:00
}
}
}