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
import ATProtoKit
import Darwin
import Foundation
import OSLog
import Security
import UIKit
extension ATProtoBlueskyService {
func strongReference(for post: Post) throws -> [String: String] {
guard !post.id.isEmpty, !post.cid.isEmpty else { throw ServiceError.postReferenceUnavailable }
return ["uri": post.id, "cid": post.cid]
}
func publishFacets(in text: String) async -> [PostFacet] {
let candidates = ComposerText.facetCandidates(in: text)
var facets: [PostFacet] = []
for candidate in candidates {
let feature: PostFacet.Feature
switch candidate.kind {
case .link:
guard let url = URL(string: candidate.value),
["http", "https"].contains(url.scheme?.lowercased() ?? "")
else { continue }
feature = .link(url)
case .mention:
do {
feature = .mention(try await resolveDID(for: candidate.value))
} catch {
continue
}
case .tag:
feature = .tag(candidate.value)
}
facets.append(PostFacet(byteStart: candidate.byteStart, byteEnd: candidate.byteEnd, feature: feature))
}
return facets.sorted { $0.byteStart < $1.byteStart }
}
static func encodedFacet(_ facet: PostFacet) -> [String: Any] {
let feature: [String: Any]
switch facet.feature {
case .link(let url): feature = ["$type": "app.bsky.richtext.facet#link", "uri": url.absoluteString]
case .mention(let did): feature = ["$type": "app.bsky.richtext.facet#mention", "did": did]
case .tag(let tag): feature = ["$type": "app.bsky.richtext.facet#tag", "tag": tag]
}
return ["index": ["byteStart": facet.byteStart, "byteEnd": facet.byteEnd], "features": [feature]]
}
}