2022-11-24 05:03:45 +01:00
|
|
|
//
|
|
|
|
// Persistence+Card.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2021-12-9.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension Persistence.Card {
|
|
|
|
|
|
|
|
public struct PersistContext {
|
|
|
|
public let domain: String
|
|
|
|
public let entity: Mastodon.Entity.Card
|
|
|
|
public let me: MastodonUser?
|
|
|
|
public init(
|
|
|
|
domain: String,
|
|
|
|
entity: Mastodon.Entity.Card,
|
|
|
|
me: MastodonUser?
|
|
|
|
) {
|
|
|
|
self.domain = domain
|
|
|
|
self.entity = entity
|
|
|
|
self.me = me
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct PersistResult {
|
|
|
|
public let card: Card
|
|
|
|
public let isNewInsertion: Bool
|
|
|
|
|
|
|
|
public init(
|
|
|
|
card: Card,
|
|
|
|
isNewInsertion: Bool
|
|
|
|
) {
|
|
|
|
self.card = card
|
|
|
|
self.isNewInsertion = isNewInsertion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static func create(
|
|
|
|
in managedObjectContext: NSManagedObjectContext,
|
|
|
|
context: PersistContext
|
|
|
|
) -> PersistResult {
|
|
|
|
var type: MastodonCardType {
|
|
|
|
switch context.entity.type {
|
|
|
|
case .link: return .link
|
|
|
|
case .photo: return .photo
|
|
|
|
case .video: return .video
|
|
|
|
case .rich: return ._other(context.entity.type.rawValue)
|
|
|
|
case ._other(let rawValue): return ._other(rawValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let property = Card.Property(
|
2022-11-24 06:51:39 +01:00
|
|
|
urlRaw: context.entity.url,
|
2022-11-24 05:03:45 +01:00
|
|
|
title: context.entity.title,
|
|
|
|
desc: context.entity.description,
|
|
|
|
type: type,
|
|
|
|
authorName: context.entity.authorName,
|
2022-11-24 06:51:39 +01:00
|
|
|
authorURLRaw: context.entity.authorURL,
|
2022-11-24 05:03:45 +01:00
|
|
|
providerName: context.entity.providerName,
|
2022-11-24 06:51:39 +01:00
|
|
|
providerURLRaw: context.entity.providerURL,
|
2022-11-24 05:03:45 +01:00
|
|
|
width: Int64(context.entity.width ?? 0),
|
|
|
|
height: Int64(context.entity.height ?? 0),
|
|
|
|
image: context.entity.image,
|
2022-11-24 06:51:39 +01:00
|
|
|
embedURLRaw: context.entity.embedURL,
|
2022-12-03 02:35:11 +01:00
|
|
|
blurhash: context.entity.blurhash,
|
|
|
|
html: context.entity.html.flatMap { $0.isEmpty ? nil : $0 }
|
2022-11-24 05:03:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
let card = Card.insert(
|
|
|
|
into: managedObjectContext,
|
|
|
|
property: property
|
|
|
|
)
|
|
|
|
|
|
|
|
return PersistResult(
|
|
|
|
card: card,
|
|
|
|
isNewInsertion: true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|