mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/Service/AuthenticationService.swift

156 lines
5.7 KiB
Swift
Raw Normal View History

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
public final class AuthenticationService: NSObject {
2021-02-03 09:01:08 +01:00
var disposeBag = Set<AnyCancellable>()
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
@Published public var mastodonAuthentications: [ManagedObjectRecord<MastodonAuthentication>] = []
@Published public var mastodonAuthenticationBoxes: [MastodonAuthenticationBox] = []
public let updateActiveUserAccountPublisher = PassthroughSubject<Void, Never>()
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
$mastodonAuthentications
.map { authentications -> [MastodonAuthenticationBox] in
return authentications
.compactMap { $0.object(in: managedObjectContext) }
.sorted(by: { $0.activedAt > $1.activedAt })
.compactMap { authentication -> MastodonAuthenticationBox? in
return MastodonAuthenticationBox(authentication: authentication)
}
2021-02-03 09:01:08 +01:00
}
.assign(to: &$mastodonAuthenticationBoxes)
2021-02-03 09:01:08 +01:00
do {
try mastodonAuthenticationFetchedResultsController.performFetch()
mastodonAuthentications = mastodonAuthenticationFetchedResultsController.fetchedObjects?
.sorted(by: { $0.activedAt > $1.activedAt })
2022-11-13 18:53:59 +01:00
.compactMap { $0.asRecord } ?? []
2021-02-03 09:01:08 +01:00
} catch {
assertionFailure(error.localizedDescription)
}
}
}
extension AuthenticationService {
public func activeMastodonUser(domain: String, userID: MastodonUser.ID) async throws -> Bool {
var isActive = false
2021-02-03 09:01:08 +01:00
let managedObjectContext = backgroundManagedObjectContext
try await managedObjectContext.performChanges {
2021-02-03 09:01:08 +01:00
let request = MastodonAuthentication.sortedFetchRequest
request.predicate = MastodonAuthentication.predicate(domain: domain, userID: userID)
request.fetchLimit = 1
guard let mastodonAuthentication = try? managedObjectContext.fetch(request).first else {
2021-02-03 09:01:08 +01:00
return
}
mastodonAuthentication.update(activedAt: Date())
isActive = true
2021-02-03 09:01:08 +01:00
}
return isActive
2021-02-03 09:01:08 +01:00
}
public func signOutMastodonUser(authenticationBox: MastodonAuthenticationBox) async throws {
let managedObjectContext = backgroundManagedObjectContext
try await managedObjectContext.performChanges {
// remove Feed
let request = Feed.sortedFetchRequest
request.predicate = Feed.predicate(
acct: .mastodon(
domain: authenticationBox.domain,
userID: authenticationBox.userID
)
)
let feeds = managedObjectContext.safeFetch(request)
for feed in feeds {
managedObjectContext.delete(feed)
}
guard let authentication = authenticationBox.authenticationRecord.object(in: managedObjectContext) else {
assertionFailure()
throw APIService.APIError.implicit(.authenticationMissing)
}
managedObjectContext.delete(authentication)
}
// 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 {
public func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
2021-02-03 09:01:08 +01:00
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
guard controller === mastodonAuthenticationFetchedResultsController else {
assertionFailure()
return
2021-02-03 09:01:08 +01:00
}
mastodonAuthentications = mastodonAuthenticationFetchedResultsController.fetchedObjects?
.sorted(by: { $0.activedAt > $1.activedAt })
2022-11-13 18:53:59 +01:00
.compactMap { $0.asRecord } ?? []
2021-02-03 09:01:08 +01:00
}
}