1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-09 23:25:49 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/Persistence/Persistence+StatusEdit.swift
Nathan Mattes 0c224f47df
Implement post editing / edit history (#875)
Co-authored-by: Marcus Kida <marcus.kida@bearologics.com>
Co-authored-by: Jed Fox <git@jedfox.com>
2023-03-02 11:06:13 +01:00

49 lines
1.7 KiB
Swift

// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import CoreData
import CoreDataStack
import MastodonSDK
extension Persistence.StatusEdit {
public static func createOrMerge(
in managedObjectContext: NSManagedObjectContext,
statusEdits: [Mastodon.Entity.StatusEdit],
forStatus status: Status
) {
guard statusEdits.isEmpty == false else { return }
// remove all edits for status
if let editHistory = status.editHistory {
for statusEdit in Array(editHistory) {
managedObjectContext.delete(statusEdit)
}
}
status.update(editHistory: Set())
let persistedEdits = create(in: managedObjectContext, statusEdits: statusEdits, forStatus: status)
status.update(editHistory: Set(persistedEdits))
}
public static func create(
in managedObjectContext: NSManagedObjectContext,
statusEdits: [Mastodon.Entity.StatusEdit],
forStatus status: Status
) -> [StatusEdit] {
var entries: [StatusEdit] = []
for statusEdit in statusEdits {
let property = StatusEdit.Property(createdAt: statusEdit.createdAt, content: statusEdit.content, sensitive: statusEdit.sensitive, spoilerText: statusEdit.spoilerText, emojis: statusEdit.mastodonEmojis, attachments: statusEdit.mastodonAttachments, poll: statusEdit.poll.map { StatusEdit.Poll(options: $0.options.map { StatusEdit.Poll.Option(title: $0.title) } ) })
let statusEditEntry = StatusEdit.insert(into: managedObjectContext, property: property)
entries.append(statusEditEntry)
}
status.update(editHistory: Set(entries))
return entries
}
}