import Foundation struct SavedDraftWire { static let localContextDeviceName = "Old Fella (local context)" let id: String let draft: [String: Any] let updatedAt: Date var posts: [[String: Any]] { draft["posts"] as? [[String: Any]] ?? [] } var post: [String: Any] { posts.first ?? [:] } var text: String { post["text"] as? String ?? "" } var images: [[String: Any]] { post["embedImages"] as? [[String: Any]] ?? [] } var videos: [[String: Any]] { post["embedVideos"] as? [[String: Any]] ?? [] } var quotePostID: String? { let records = post["embedRecords"] as? [[String: Any]] let reference = records?.first?["record"] as? [String: Any] return reference?["uri"] as? String } var references: [String] { (images + videos).compactMap { ($0["localRef"] as? [String: Any])?["path"] as? String } } var needsLocalContext: Bool { draft["deviceName"] as? String == Self.localContextDeviceName } var unsupportedReason: String? { guard posts.count == 1 else { return "This draft contains a thread. Open it in Bluesky to edit it." } guard Set(draft.keys).isSubset(of: ["$type", "posts", "deviceId", "deviceName"]), Set(post.keys).isSubset(of: ["$type", "text", "embedImages", "embedVideos", "embedRecords"]), validType(draft, expected: "app.bsky.draft.defs#draft"), validType(post, expected: "app.bsky.draft.defs#draftPost") else { return "This draft uses post options that Old Fella cannot edit." } guard post["text"] is String, images.count <= 4, videos.count <= 1, images.isEmpty || videos.isEmpty, validOptionalArray("embedImages"), validOptionalArray("embedVideos"), validOptionalArray("embedRecords") else { return "This draft uses media that Old Fella cannot edit." } guard images.allSatisfy({ validLocalEmbed($0, isVideo: false) }), videos.allSatisfy({ validLocalEmbed($0, isVideo: true) }) else { return "This draft uses media options that Old Fella cannot edit." } let records = post["embedRecords"] as? [[String: Any]] ?? [] guard records.count <= 1, records.allSatisfy({ record in guard Set(record.keys).isSubset(of: ["$type", "record"]), validType(record, expected: "app.bsky.draft.defs#draftEmbedRecord"), let reference = record["record"] as? [String: Any], Set(reference.keys).isSubset(of: ["$type", "uri", "cid"]), validType(reference, expected: "com.atproto.repo.strongRef"), let uri = reference["uri"] as? String, uri.hasPrefix("at://"), let cid = reference["cid"] as? String, !cid.isEmpty else { return false } return true }) else { return "This draft uses a quote that Old Fella cannot edit." } return nil } static func fingerprint(_ draft: [String: Any]) throws -> String { func withoutTypes(_ value: Any) -> Any { if let array = value as? [Any] { return array.map(withoutTypes) } guard let object = value as? [String: Any] else { return value } return object.filter { $0.key != "$type" }.mapValues(withoutTypes) } return DraftLocalMediaStore.hash( try JSONSerialization.data(withJSONObject: withoutTypes(draft), options: .sortedKeys)) } private func validOptionalArray(_ key: String) -> Bool { post[key] == nil || post[key] is [[String: Any]] } private func validLocalEmbed(_ embed: [String: Any], isVideo: Bool) -> Bool { guard Set(embed.keys).isSubset(of: ["$type", "localRef", "alt"]), validType(embed, expected: "app.bsky.draft.defs#draftEmbed\(isVideo ? "Video" : "Image")"), let local = embed["localRef"] as? [String: Any], Set(local.keys).isSubset(of: ["$type", "path"]), validType(local, expected: "app.bsky.draft.defs#draftEmbedLocalRef"), let path = local["path"] as? String, !path.isEmpty, embed["alt"] == nil || embed["alt"] is String else { return false } return !isVideo || (embed["alt"] as? String ?? "").isEmpty } private func validType(_ object: [String: Any], expected: String) -> Bool { object["$type"] == nil || object["$type"] as? String == expected } }