chore: Address PR codestyle feedback
This commit is contained in:
parent
855f626c42
commit
f530d109d3
|
@ -6,17 +6,33 @@ import MastodonSDK
|
||||||
|
|
||||||
extension APIService.CoreData {
|
extension APIService.CoreData {
|
||||||
|
|
||||||
static func createOrMergeInstanceV2(
|
public struct PersistContext {
|
||||||
into managedObjectContext: NSManagedObjectContext,
|
public let domain: String
|
||||||
|
public let entity: Mastodon.Entity.V2.Instance
|
||||||
|
public let networkDate: Date
|
||||||
|
public let log: Logger
|
||||||
|
|
||||||
|
public init(
|
||||||
domain: String,
|
domain: String,
|
||||||
entity: Mastodon.Entity.V2.Instance,
|
entity: Mastodon.Entity.V2.Instance,
|
||||||
networkDate: Date,
|
networkDate: Date,
|
||||||
log: Logger
|
log: Logger
|
||||||
|
) {
|
||||||
|
self.domain = domain
|
||||||
|
self.entity = entity
|
||||||
|
self.networkDate = networkDate
|
||||||
|
self.log = log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func createOrMergeInstance(
|
||||||
|
in managedObjectContext: NSManagedObjectContext,
|
||||||
|
context: PersistContext
|
||||||
) -> (instance: Instance, isCreated: Bool) {
|
) -> (instance: Instance, isCreated: Bool) {
|
||||||
// fetch old mastodon user
|
// fetch old mastodon user
|
||||||
let old: Instance? = {
|
let old: Instance? = {
|
||||||
let request = Instance.sortedFetchRequest
|
let request = Instance.sortedFetchRequest
|
||||||
request.predicate = Instance.predicate(domain: domain)
|
request.predicate = Instance.predicate(domain: context.domain)
|
||||||
request.fetchLimit = 1
|
request.fetchLimit = 1
|
||||||
request.returnsObjectsAsFaults = false
|
request.returnsObjectsAsFaults = false
|
||||||
do {
|
do {
|
||||||
|
@ -28,19 +44,17 @@ extension APIService.CoreData {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if let old = old {
|
if let old = old {
|
||||||
APIService.CoreData.mergeV2(
|
APIService.CoreData.merge(
|
||||||
instance: old,
|
instance: old,
|
||||||
entity: entity,
|
context: context
|
||||||
domain: domain,
|
|
||||||
networkDate: networkDate
|
|
||||||
)
|
)
|
||||||
return (old, false)
|
return (old, false)
|
||||||
} else {
|
} else {
|
||||||
let instance = Instance.insert(
|
let instance = Instance.insert(
|
||||||
into: managedObjectContext,
|
into: managedObjectContext,
|
||||||
property: Instance.Property(domain: domain, version: entity.version)
|
property: Instance.Property(domain: context.domain, version: context.entity.version)
|
||||||
)
|
)
|
||||||
let configurationRaw = entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
|
let configurationRaw = context.entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
|
||||||
instance.update(configurationV2Raw: configurationRaw)
|
instance.update(configurationV2Raw: configurationRaw)
|
||||||
|
|
||||||
return (instance, true)
|
return (instance, true)
|
||||||
|
@ -51,19 +65,17 @@ extension APIService.CoreData {
|
||||||
|
|
||||||
extension APIService.CoreData {
|
extension APIService.CoreData {
|
||||||
|
|
||||||
static func mergeV2(
|
static func merge(
|
||||||
instance: Instance,
|
instance: Instance,
|
||||||
entity: Mastodon.Entity.V2.Instance,
|
context: PersistContext
|
||||||
domain: String,
|
|
||||||
networkDate: Date
|
|
||||||
) {
|
) {
|
||||||
guard networkDate > instance.updatedAt else { return }
|
guard context.networkDate > instance.updatedAt else { return }
|
||||||
|
|
||||||
let configurationRaw = entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
|
let configurationRaw = context.entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
|
||||||
instance.update(configurationV2Raw: configurationRaw)
|
instance.update(configurationV2Raw: configurationRaw)
|
||||||
instance.version = entity.version
|
instance.version = context.entity.version
|
||||||
|
|
||||||
instance.didUpdate(at: networkDate)
|
instance.didUpdate(at: context.networkDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,13 +118,15 @@ extension InstanceService {
|
||||||
let managedObjectContext = self.backgroundManagedObjectContext
|
let managedObjectContext = self.backgroundManagedObjectContext
|
||||||
return managedObjectContext.performChanges {
|
return managedObjectContext.performChanges {
|
||||||
// get instance
|
// get instance
|
||||||
let (instance, _) = APIService.CoreData.createOrMergeInstanceV2(
|
let (instance, _) = APIService.CoreData.createOrMergeInstance(
|
||||||
into: managedObjectContext,
|
in: managedObjectContext,
|
||||||
|
context: .init(
|
||||||
domain: domain,
|
domain: domain,
|
||||||
entity: response.value,
|
entity: response.value,
|
||||||
networkDate: response.networkDate,
|
networkDate: response.networkDate,
|
||||||
log: Logger(subsystem: "Update", category: "InstanceService")
|
log: Logger(subsystem: "Update", category: "InstanceService")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
// update relationship
|
// update relationship
|
||||||
let request = MastodonAuthentication.sortedFetchRequest
|
let request = MastodonAuthentication.sortedFetchRequest
|
||||||
|
|
Loading…
Reference in New Issue