Add card when merging

This commit is contained in:
Kyle Bashour 2022-11-26 23:42:02 -08:00
parent 8a8ecb0b68
commit 176067800c
2 changed files with 31 additions and 14 deletions

View File

@ -50,7 +50,7 @@ public final class Card: NSManagedObject {
// sourcery: autoGenerateProperty
@NSManaged public private(set) var blurhash: String?
// one-to-one relationship
// sourcery: autoGenerateRelationship
@NSManaged public private(set) var status: Status
}
@ -157,13 +157,17 @@ extension Card: AutoGenerateRelationship {
// Generated using Sourcery
// DO NOT EDIT
public struct Relationship {
public let status: Status
public init(
status: Status
) {
self.status = status
}
}
public func configure(relationship: Relationship) {
self.status = relationship.status
}
// sourcery:end

View File

@ -87,7 +87,7 @@ extension Persistence.Status {
}
if let oldStatus = fetch(in: managedObjectContext, context: context) {
merge(mastodonStatus: oldStatus, context: context)
merge(in: managedObjectContext, mastodonStatus: oldStatus, context: context)
return PersistResult(
status: oldStatus,
isNewInsertion: false,
@ -108,18 +108,7 @@ extension Persistence.Status {
return result.poll
}()
let card: Card? = {
guard let entity = context.entity.card else { return nil }
let result = Persistence.Card.create(
in: managedObjectContext,
context: Persistence.Card.PersistContext(
domain: context.domain,
entity: entity,
me: context.me
)
)
return result.card
}()
let card = createCard(in: managedObjectContext, context: context)
let authorResult = Persistence.MastodonUser.createOrMerge(
in: managedObjectContext,
@ -196,6 +185,7 @@ extension Persistence.Status {
}
public static func merge(
in managedObjectContext: NSManagedObjectContext,
mastodonStatus status: Status,
context: PersistContext
) {
@ -217,8 +207,31 @@ extension Persistence.Status {
)
)
}
if status.card == nil, context.entity.card != nil {
let card = createCard(in: managedObjectContext, context: context)
let relationship = Card.Relationship(status: status)
card?.configure(relationship: relationship)
}
update(status: status, context: context)
}
private static func createCard(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> Card? {
guard let entity = context.entity.card else { return nil }
let result = Persistence.Card.create(
in: managedObjectContext,
context: Persistence.Card.PersistContext(
domain: context.domain,
entity: entity,
me: context.me
)
)
return result.card
}
private static func update(
status: Status,