1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
}
}