import ATProtoKit import Darwin import Foundation import OSLog import Security import UIKit extension ATProtoBlueskyService { func deletePost(_ post: Post) async throws { guard let current = stored else { throw ServiceError.noSession } let parts = post.id.split(separator: "/", omittingEmptySubsequences: false) guard post.author.id == current.did, parts.count == 5, parts[0] == "at:", parts[1].isEmpty, parts[2] == Substring(current.did), parts[3] == "app.bsky.feed.post", !parts[4].isEmpty, !parts[4].contains("?"), !parts[4].contains("#") else { throw ActionNotSentError(underlying: ServiceError.postReferenceUnavailable) } try await deleteRecord(uri: post.id, repository: current.did) } func toggleLike(post: Post, liked: Bool) async throws -> String? { guard let current = stored else { throw ServiceError.noSession } if liked { let reference: [String: String] do { reference = try strongReference(for: post) } catch { throw ActionNotSentError(underlying: error) } let record: [String: Any] = [ "$type": "app.bsky.feed.like", "subject": reference, "createdAt": ISO8601DateFormatter().string(from: Date()) ] return try await createRecord(collection: "app.bsky.feed.like", record: record).uri } guard let uri = post.likeURI else { throw ActionNotSentError(underlying: ServiceError.invalidResponse) } try await deleteRecord(uri: uri, repository: current.did) return nil } func toggleRepost(post: Post, reposted: Bool) async throws -> String? { guard let current = stored else { throw ServiceError.noSession } if reposted { let reference: [String: String] do { reference = try strongReference(for: post) } catch { throw ActionNotSentError(underlying: error) } let record: [String: Any] = [ "$type": "app.bsky.feed.repost", "subject": reference, "createdAt": ISO8601DateFormatter().string(from: Date()) ] return try await createRecord(collection: "app.bsky.feed.repost", record: record).uri } guard let uri = post.repostURI else { throw ActionNotSentError(underlying: ServiceError.invalidResponse) } try await deleteRecord(uri: uri, repository: current.did) return nil } }