2022-06-09 12:09:28 +02:00
|
|
|
//
|
|
|
|
// Account.swift
|
|
|
|
// MastodonIntent
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-6-9.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import Intents
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonCore
|
2022-06-09 12:09:28 +02:00
|
|
|
|
|
|
|
extension Account {
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
static func fetch(in managedObjectContext: NSManagedObjectContext) async throws -> [Account] {
|
|
|
|
// get accounts
|
|
|
|
let accounts: [Account] = try await managedObjectContext.perform {
|
2023-05-25 16:26:17 +02:00
|
|
|
let results = AuthenticationServiceProvider.shared.authentications
|
2022-06-09 12:09:28 +02:00
|
|
|
let accounts = results.compactMap { mastodonAuthentication -> Account? in
|
2023-05-25 16:26:17 +02:00
|
|
|
guard let user = mastodonAuthentication.user(in: managedObjectContext) else {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-09 12:09:28 +02:00
|
|
|
let account = Account(
|
|
|
|
identifier: mastodonAuthentication.identifier.uuidString,
|
|
|
|
display: user.displayNameWithFallback,
|
|
|
|
subtitle: user.acctWithDomain,
|
|
|
|
image: user.avatarImageURL().flatMap { INImage(url: $0) }
|
|
|
|
)
|
|
|
|
account.name = user.displayNameWithFallback
|
|
|
|
account.username = user.acctWithDomain
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
return accounts
|
|
|
|
} // end managedObjectContext.perform
|
|
|
|
|
|
|
|
return accounts
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Array where Element == Account {
|
|
|
|
func mastodonAuthentication(in managedObjectContext: NSManagedObjectContext) throws -> [MastodonAuthentication] {
|
|
|
|
let identifiers = self
|
|
|
|
.compactMap { $0.identifier }
|
|
|
|
.compactMap { UUID(uuidString: $0) }
|
2023-05-25 16:26:17 +02:00
|
|
|
let results = AuthenticationServiceProvider.shared.authentications.filter({ identifiers.contains($0.identifier) })
|
2022-06-09 12:09:28 +02:00
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|