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

geesawra.industries/old-fella

Old people's social media app

0
1
main
113 lines · 4.4 KB Raw History
import ATProtoKit
import Darwin
import Foundation
import OSLog
import Security
import UIKit

extension ATProtoBlueskyService {
    struct TypedRecord<Record: ATRecordProtocol>: ATRecordProtocol {
        static var type: String { Record.type }
        let record: Record

        init(_ record: Record) { self.record = record }

        init(from decoder: Decoder) throws {
            record = try Record(from: decoder)
        }

        func encode(to encoder: Encoder) throws {
            try record.encode(to: encoder)
            var container = encoder.container(keyedBy: RecordTypeCodingKeys.self)
            try container.encode(Self.type, forKey: .type)
        }

    }

    enum RecordTypeCodingKeys: String, CodingKey {
        case type = "$type"
    }

    func createRecord(collection: String, record: [String: Any]) async throws -> CreateRecordResponse {
        guard let current = stored else { throw ServiceError.noSession }
        let libraryRecord: UnknownType
        do { libraryRecord = try decodedLibraryRecord(collection: collection, record: record) } catch {
            throw ActionNotSentError(underlying: error)
        }
        let client: ATProtoKit
        do { client = try await authenticatedClient() } catch { throw ActionNotSentError(underlying: error) }
        do {
            let reference = try await client.createRecord(
                repositoryDID: current.did, collection: collection, record: libraryRecord)
            if let id = client.sessionConfiguration?.instanceUUID {
                await UserSessionRegistry.shared.removeSession(for: id)
            }
            return CreateRecordResponse(uri: reference.recordURI, cid: reference.recordCID)
        } catch {
            if let id = client.sessionConfiguration?.instanceUUID {
                await UserSessionRegistry.shared.removeSession(for: id)
            }
            throw error
        }
    }

    func deleteRecord(uri: String, repository: String) async throws {
        let parts = uri.split(separator: "/")
        guard parts.count >= 4 else { throw ActionNotSentError(underlying: ServiceError.postReferenceUnavailable) }
        let client: ATProtoKit
        do { client = try await authenticatedClient() } catch { throw ActionNotSentError(underlying: error) }
        do {
            try await client.deleteRecord(
                repositoryDID: repository, collection: String(parts[2]), recordKey: String(parts[3]))
        } catch {
            if let id = client.sessionConfiguration?.instanceUUID {
                await UserSessionRegistry.shared.removeSession(for: id)
            }
            throw error
        }
        if let id = client.sessionConfiguration?.instanceUUID {
            await UserSessionRegistry.shared.removeSession(for: id)
        }
    }

    func getProfile(actorID: String) async throws -> AppBskyLexicon.Actor.ProfileViewDetailedDefinition {
        let client: ATProtoKit
        if stored != nil {
            client = try await authenticatedClient()
        } else {
            client = await apiClient(for: appViewURL)
        }
        do {
            let profile = try await client.getProfile(for: actorID)
            if let id = client.sessionConfiguration?.instanceUUID {
                await UserSessionRegistry.shared.removeSession(for: id)
            }
            return profile
        } catch {
            if let id = client.sessionConfiguration?.instanceUUID {
                await UserSessionRegistry.shared.removeSession(for: id)
            }
            throw error
        }
    }
    private func decodedLibraryRecord(collection: String, record: [String: Any]) throws -> UnknownType {
        let data = try JSONSerialization.data(withJSONObject: record)
        let decoder = JSONDecoder()
        switch collection {
        case "app.bsky.feed.post":
            return .record(try decoder.decode(AppBskyLexicon.Feed.PostRecord.self, from: data))
        case "app.bsky.feed.like":
            return .record(
                TypedRecord(try decoder.decode(AppBskyLexicon.Feed.LikeRecord.self, from: data)))
        case "app.bsky.feed.repost":
            return .record(
                TypedRecord(try decoder.decode(AppBskyLexicon.Feed.RepostRecord.self, from: data)))
        case "app.bsky.graph.follow":
            return .record(
                TypedRecord(try decoder.decode(AppBskyLexicon.Graph.FollowRecord.self, from: data)))
        default:
            throw ServiceError.invalidResponse
        }
    }

}