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

View File

@ -87,7 +87,7 @@ extension Persistence.Status {
} }
if let oldStatus = fetch(in: managedObjectContext, context: context) { if let oldStatus = fetch(in: managedObjectContext, context: context) {
merge(mastodonStatus: oldStatus, context: context) merge(in: managedObjectContext, mastodonStatus: oldStatus, context: context)
return PersistResult( return PersistResult(
status: oldStatus, status: oldStatus,
isNewInsertion: false, isNewInsertion: false,
@ -108,18 +108,7 @@ extension Persistence.Status {
return result.poll return result.poll
}() }()
let card: Card? = { let card = createCard(in: managedObjectContext, context: context)
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 authorResult = Persistence.MastodonUser.createOrMerge( let authorResult = Persistence.MastodonUser.createOrMerge(
in: managedObjectContext, in: managedObjectContext,
@ -196,6 +185,7 @@ extension Persistence.Status {
} }
public static func merge( public static func merge(
in managedObjectContext: NSManagedObjectContext,
mastodonStatus status: Status, mastodonStatus status: Status,
context: PersistContext 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) 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( private static func update(
status: Status, status: Status,