-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathChatAPI.swift
More file actions
216 lines (181 loc) · 9.52 KB
/
ChatAPI.swift
File metadata and controls
216 lines (181 loc) · 9.52 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Ably
@MainActor
internal final class ChatAPI: Sendable {
private let realtime: any InternalRealtimeClientProtocol
private let apiVersionV3 = "/chat/v3"
public init(realtime: any InternalRealtimeClientProtocol) {
self.realtime = realtime
}
// (CHA-M6) Messages should be queryable from a paginated REST API.
internal func getMessages(roomId: String, params: QueryOptions) async throws(InternalError) -> any PaginatedResult<Message> {
let endpoint = "\(apiVersionV3)/rooms/\(roomId)/messages"
let result: Result<PaginatedResultWrapper<Message>, InternalError> = await makePaginatedRequest(endpoint, params: params.asQueryItems())
return try result.get()
}
internal struct SendMessageResponse: JSONObjectDecodable {
internal let serial: String
internal let createdAt: Int64
internal init(jsonObject: [String: JSONValue]) throws(InternalError) {
serial = try jsonObject.stringValueForKey("serial")
createdAt = try Int64(jsonObject.numberValueForKey("createdAt"))
}
}
internal struct MessageOperationResponse: JSONObjectDecodable {
internal let version: String
internal let timestamp: Int64
internal init(jsonObject: [String: JSONValue]) throws(InternalError) {
version = try jsonObject.stringValueForKey("version")
timestamp = try Int64(jsonObject.numberValueForKey("timestamp"))
}
}
internal typealias UpdateMessageResponse = MessageOperationResponse
internal typealias DeleteMessageResponse = MessageOperationResponse
// (CHA-M3) Messages are sent to Ably via the Chat REST API, using the send method.
// (CHA-M3a) When a message is sent successfully, the caller shall receive a struct representing the Message in response (as if it were received via Realtime event).
internal func sendMessage(roomId: String, params: SendMessageParams) async throws(InternalError) -> Message {
guard let clientId = realtime.clientId else {
throw ARTErrorInfo.create(withCode: 40000, message: "Ensure your Realtime instance is initialized with a clientId.").toInternalError()
}
let endpoint = "\(apiVersionV3)/rooms/\(roomId)/messages"
var body: [String: JSONValue] = ["text": .string(params.text)]
// (CHA-M3b) A message may be sent without metadata or headers. When these are not specified by the user, they must be omitted from the REST payload.
if let metadata = params.metadata {
body["metadata"] = .object(metadata)
}
if let headers = params.headers {
body["headers"] = .object(headers.mapValues(\.toJSONValue))
}
let response: SendMessageResponse = try await makeRequest(endpoint, method: "POST", body: body)
// response.createdAt is in milliseconds, convert it to seconds
let createdAtInSeconds = TimeInterval(Double(response.createdAt) / 1000)
let createdAtDate = Date(timeIntervalSince1970: createdAtInSeconds)
let message = Message(
serial: response.serial,
action: .create,
clientID: clientId,
roomID: roomId,
text: params.text,
createdAt: createdAtDate,
metadata: params.metadata ?? [:],
headers: params.headers ?? [:],
version: response.serial,
timestamp: createdAtDate
)
return message
}
// (CHA-M8) A client must be able to update a message in a room.
// (CHA-M8a) A client may update a message via the Chat REST API by calling the update method.
internal func updateMessage(with modifiedMessage: Message, description: String?, metadata: OperationMetadata?) async throws(InternalError) -> Message {
guard let clientID = realtime.clientId else {
throw ARTErrorInfo.create(withCode: 40000, message: "Ensure your Realtime instance is initialized with a clientId.").toInternalError()
}
let endpoint = "\(apiVersionV3)/rooms/\(modifiedMessage.roomID)/messages/\(modifiedMessage.serial)"
var body: [String: JSONValue] = [:]
let messageObject: [String: JSONValue] = [
"text": .string(modifiedMessage.text),
"metadata": .object(modifiedMessage.metadata),
"headers": .object(modifiedMessage.headers.mapValues(\.toJSONValue)),
]
body["message"] = .object(messageObject)
if let description {
body["description"] = .string(description)
}
if let metadata {
body["metadata"] = .object(metadata)
}
// (CHA-M8c) An update operation has PUT semantics. If a field is not specified in the update, it is assumed to be removed.
let response: UpdateMessageResponse = try await makeRequest(endpoint, method: "PUT", body: body)
// response.timestamp is in milliseconds, convert it to seconds
let timestampInSeconds = TimeInterval(Double(response.timestamp) / 1000)
// (CHA-M8b) When a message is updated successfully via the REST API, the caller shall receive a struct representing the Message in response, as if it were received via Realtime event.
let message = Message(
serial: modifiedMessage.serial,
action: .update,
clientID: modifiedMessage.clientID,
roomID: modifiedMessage.roomID,
text: modifiedMessage.text,
createdAt: modifiedMessage.createdAt,
metadata: modifiedMessage.metadata,
headers: modifiedMessage.headers,
version: response.version,
timestamp: Date(timeIntervalSince1970: timestampInSeconds),
operation: .init(
clientID: clientID,
description: description,
metadata: metadata
)
)
return message
}
// (CHA-M9) A client must be able to delete a message in a room.
// (CHA-M9a) A client may delete a message via the Chat REST API by calling the delete method.
internal func deleteMessage(message: Message, params: DeleteMessageParams) async throws(InternalError) -> Message {
let endpoint = "\(apiVersionV3)/rooms/\(message.roomID)/messages/\(message.serial)/delete"
var body: [String: JSONValue] = [:]
if let description = params.description {
body["description"] = .string(description)
}
if let metadata = params.metadata {
body["metadata"] = .object(metadata)
}
let response: DeleteMessageResponse = try await makeRequest(endpoint, method: "POST", body: body)
// response.timestamp is in milliseconds, convert it to seconds
let timestampInSeconds = TimeInterval(Double(response.timestamp) / 1000)
// (CHA-M9b) When a message is deleted successfully via the REST API, the caller shall receive a struct representing the Message in response, as if it were received via Realtime event.
let message = Message(
serial: message.serial,
action: .delete,
clientID: message.clientID,
roomID: message.roomID,
text: message.text,
createdAt: message.createdAt,
metadata: message.metadata,
headers: message.headers,
version: response.version,
timestamp: Date(timeIntervalSince1970: timestampInSeconds),
operation: .init(
clientID: message.clientID,
description: params.description,
metadata: params.metadata
)
)
return message
}
internal func getOccupancy(roomId: String) async throws(InternalError) -> OccupancyEvent {
let endpoint = "\(apiVersionV3)/rooms/\(roomId)/occupancy"
return try await makeRequest(endpoint, method: "GET")
}
private func makeRequest<Response: JSONDecodable>(_ url: String, method: String, body: [String: JSONValue]? = nil) async throws(InternalError) -> Response {
let ablyCocoaBody: Any? = if let body {
JSONValue.object(body).toAblyCocoaData
} else {
nil
}
// (CHA-M3e & CHA-M8d & CHA-M9c) If an error is returned from the REST API, its ErrorInfo representation shall be thrown as the result of the send call.
let paginatedResponse = try await realtime.request(method, path: url, params: [:], body: ablyCocoaBody, headers: [:])
guard let firstItem = paginatedResponse.items.first else {
throw ChatError.noItemInResponse.toInternalError()
}
let jsonValue = JSONValue(ablyCocoaData: firstItem)
return try Response(jsonValue: jsonValue)
}
// TODO: (https://github.com/ably/ably-chat-swift/issues/267) switch this back to use `throws` once Xcode 16.3 typed throw crashes are fixed
private func makePaginatedRequest<Response: JSONDecodable & Sendable & Equatable>(
_ url: String,
params: [String: String]? = nil
) async -> Result<PaginatedResultWrapper<Response>, InternalError> {
do {
let paginatedResponse = try await realtime.request("GET", path: url, params: params, body: nil, headers: [:])
let jsonValues = paginatedResponse.items.map { JSONValue(ablyCocoaData: $0) }
let items = try jsonValues.map { jsonValue throws(InternalError) in
try Response(jsonValue: jsonValue)
}
return .success(paginatedResponse.toPaginatedResult(items: items))
} catch {
return .failure(error)
}
}
internal enum ChatError: Error {
case noItemInResponse
}
}