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
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
}
}