~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
48 lines · 1.9 KB Raw History
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]]
    }
}