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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
}
}
}