2021-02-03 09:01:08 +01:00
|
|
|
//
|
|
|
|
// AuthenticationService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021/2/3.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2021-03-15 07:40:10 +01:00
|
|
|
final class AuthenticationService: NSObject {
|
2021-02-03 09:01:08 +01:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2021-02-03 09:01:08 +01:00
|
|
|
// input
|
|
|
|
weak var apiService: APIService?
|
|
|
|
let managedObjectContext: NSManagedObjectContext // read-only
|
|
|
|
let backgroundManagedObjectContext: NSManagedObjectContext
|
|
|
|
let mastodonAuthenticationFetchedResultsController: NSFetchedResultsController<MastodonAuthentication>
|
|
|
|
|
|
|
|
// output
|
|
|
|
let mastodonAuthentications = CurrentValueSubject<[MastodonAuthentication], Never>([])
|
2021-07-20 10:40:04 +02:00
|
|
|
let mastodonAuthenticationBoxes = CurrentValueSubject<[MastodonAuthenticationBox], Never>([])
|
2021-02-03 09:01:08 +01:00
|
|
|
let activeMastodonAuthentication = CurrentValueSubject<MastodonAuthentication?, Never>(nil)
|
2021-07-20 10:40:04 +02:00
|
|
|
let activeMastodonAuthenticationBox = CurrentValueSubject<MastodonAuthenticationBox?, Never>(nil)
|
2021-02-03 09:01:08 +01:00
|
|
|
|
|
|
|
init(
|
|
|
|
managedObjectContext: NSManagedObjectContext,
|
|
|
|
backgroundManagedObjectContext: NSManagedObjectContext,
|
|
|
|
apiService: APIService
|
|
|
|
) {
|
|
|
|
self.managedObjectContext = managedObjectContext
|
|
|
|
self.backgroundManagedObjectContext = backgroundManagedObjectContext
|
|
|
|
self.apiService = apiService
|
|
|
|
self.mastodonAuthenticationFetchedResultsController = {
|
|
|
|
let fetchRequest = MastodonAuthentication.sortedFetchRequest
|
|
|
|
fetchRequest.returnsObjectsAsFaults = false
|
|
|
|
fetchRequest.fetchBatchSize = 20
|
|
|
|
let controller = NSFetchedResultsController(
|
|
|
|
fetchRequest: fetchRequest,
|
|
|
|
managedObjectContext: managedObjectContext,
|
|
|
|
sectionNameKeyPath: nil,
|
|
|
|
cacheName: nil
|
|
|
|
)
|
|
|
|
return controller
|
|
|
|
}()
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
mastodonAuthenticationFetchedResultsController.delegate = self
|
|
|
|
|
|
|
|
// TODO: verify credentials for active authentication
|
|
|
|
|
|
|
|
// bind data
|
|
|
|
mastodonAuthentications
|
|
|
|
.map { $0.sorted(by: { $0.activedAt > $1.activedAt }).first }
|
|
|
|
.assign(to: \.value, on: activeMastodonAuthentication)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
mastodonAuthentications
|
2021-07-20 10:40:04 +02:00
|
|
|
.map { authentications -> [MastodonAuthenticationBox] in
|
2021-04-26 10:57:50 +02:00
|
|
|
return authentications
|
|
|
|
.sorted(by: { $0.activedAt > $1.activedAt })
|
2021-07-20 10:40:04 +02:00
|
|
|
.compactMap { authentication -> MastodonAuthenticationBox? in
|
|
|
|
return MastodonAuthenticationBox(
|
2022-01-27 14:23:39 +01:00
|
|
|
authenticationRecord: .init(objectID: authentication.objectID),
|
2021-04-26 10:57:50 +02:00
|
|
|
domain: authentication.domain,
|
|
|
|
userID: authentication.userID,
|
|
|
|
appAuthorization: Mastodon.API.OAuth.Authorization(accessToken: authentication.appAccessToken),
|
|
|
|
userAuthorization: Mastodon.API.OAuth.Authorization(accessToken: authentication.userAccessToken)
|
|
|
|
)
|
|
|
|
}
|
2021-02-03 09:01:08 +01:00
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
.assign(to: \.value, on: mastodonAuthenticationBoxes)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
mastodonAuthenticationBoxes
|
|
|
|
.map { $0.first }
|
2021-02-03 09:01:08 +01:00
|
|
|
.assign(to: \.value, on: activeMastodonAuthenticationBox)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
do {
|
|
|
|
try mastodonAuthenticationFetchedResultsController.performFetch()
|
|
|
|
mastodonAuthentications.value = mastodonAuthenticationFetchedResultsController.fetchedObjects ?? []
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AuthenticationService {
|
|
|
|
|
|
|
|
func activeMastodonUser(domain: String, userID: MastodonUser.ID) -> AnyPublisher<Result<Bool, Error>, Never> {
|
2021-06-24 13:20:41 +02:00
|
|
|
var isActive = false
|
2021-10-11 13:19:27 +02:00
|
|
|
var _mastodonAuthentication: MastodonAuthentication?
|
2021-02-03 09:01:08 +01:00
|
|
|
|
2021-10-11 13:19:27 +02:00
|
|
|
return backgroundManagedObjectContext.performChanges { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
2021-02-03 09:01:08 +01:00
|
|
|
let request = MastodonAuthentication.sortedFetchRequest
|
|
|
|
request.predicate = MastodonAuthentication.predicate(domain: domain, userID: userID)
|
|
|
|
request.fetchLimit = 1
|
2021-06-24 13:20:41 +02:00
|
|
|
guard let mastodonAuthentication = try? self.backgroundManagedObjectContext.fetch(request).first else {
|
2021-02-03 09:01:08 +01:00
|
|
|
return
|
|
|
|
}
|
2021-06-24 13:20:41 +02:00
|
|
|
mastodonAuthentication.update(activedAt: Date())
|
2021-10-11 13:19:27 +02:00
|
|
|
_mastodonAuthentication = mastodonAuthentication
|
2021-06-24 13:20:41 +02:00
|
|
|
isActive = true
|
2021-10-11 13:19:27 +02:00
|
|
|
|
2021-02-03 09:01:08 +01:00
|
|
|
}
|
2021-10-11 13:19:27 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.map { [weak self] result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
if let self = self,
|
|
|
|
let mastodonAuthentication = _mastodonAuthentication
|
|
|
|
{
|
|
|
|
// force set to avoid delay
|
|
|
|
self.activeMastodonAuthentication.value = mastodonAuthentication
|
|
|
|
self.activeMastodonAuthenticationBox.value = MastodonAuthenticationBox(
|
2022-01-27 14:23:39 +01:00
|
|
|
authenticationRecord: .init(objectID: mastodonAuthentication.objectID),
|
2021-10-11 13:19:27 +02:00
|
|
|
domain: mastodonAuthentication.domain,
|
|
|
|
userID: mastodonAuthentication.userID,
|
|
|
|
appAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.appAccessToken),
|
|
|
|
userAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.userAccessToken)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case .failure:
|
|
|
|
break
|
|
|
|
}
|
2021-06-24 13:20:41 +02:00
|
|
|
return result.map { isActive }
|
2021-02-03 09:01:08 +01:00
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
func signOutMastodonUser(
|
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws {
|
2021-04-26 10:57:50 +02:00
|
|
|
let managedObjectContext = backgroundManagedObjectContext
|
2022-02-11 12:27:14 +01:00
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
// remove Feed
|
|
|
|
let request = Feed.sortedFetchRequest
|
|
|
|
request.predicate = Feed.predicate(
|
|
|
|
acct: .mastodon(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
userID: authenticationBox.userID
|
|
|
|
)
|
2021-07-26 10:31:57 +02:00
|
|
|
)
|
2022-02-11 12:27:14 +01:00
|
|
|
let feeds = managedObjectContext.safeFetch(request)
|
|
|
|
for feed in feeds {
|
|
|
|
managedObjectContext.delete(feed)
|
2021-07-26 10:31:57 +02:00
|
|
|
}
|
2022-02-11 12:27:14 +01:00
|
|
|
|
|
|
|
guard let authentication = authenticationBox.authenticationRecord.object(in: managedObjectContext) else {
|
|
|
|
assertionFailure()
|
|
|
|
throw APIService.APIError.implicit(.authenticationMissing)
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
managedObjectContext.delete(authentication)
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
2022-02-11 12:27:14 +01:00
|
|
|
|
|
|
|
// cancel push notification subscription
|
|
|
|
do {
|
|
|
|
_ = try await apiService?.cancelSubscription(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
authorization: authenticationBox.userAuthorization
|
|
|
|
)
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2021-02-03 09:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - NSFetchedResultsControllerDelegate
|
|
|
|
extension AuthenticationService: NSFetchedResultsControllerDelegate {
|
|
|
|
|
|
|
|
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
|
|
|
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
|
|
|
if controller === mastodonAuthenticationFetchedResultsController {
|
|
|
|
mastodonAuthentications.value = mastodonAuthenticationFetchedResultsController.fetchedObjects ?? []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|