Implements attachment uploading
This commit is contained in:
@@ -133,10 +133,14 @@ final class XPCClient
|
||||
return results
|
||||
}
|
||||
|
||||
public func sendMessage(conversationId: String, message: String) async throws {
|
||||
public func sendMessage(conversationId: String, message: String, transferGuids: Set<String>) async throws {
|
||||
var args: [String: xpc_object_t] = [:]
|
||||
args["conversation_id"] = xpcString(conversationId)
|
||||
args["text"] = xpcString(message)
|
||||
|
||||
if !transferGuids.isEmpty {
|
||||
args["attachment_guids"] = xpcStringArray(transferGuids)
|
||||
}
|
||||
|
||||
let req = makeRequest(method: "SendMessage", arguments: args)
|
||||
guard let reply = try await sendSync(req), xpc_get_type(reply) == XPC_TYPE_DICTIONARY else { throw Error.typeError }
|
||||
@@ -150,6 +154,16 @@ final class XPCClient
|
||||
let req = makeRequest(method: "DownloadAttachment", arguments: args)
|
||||
_ = try await sendSync(req)
|
||||
}
|
||||
|
||||
public func uploadAttachment(path: String) async throws -> String {
|
||||
var args: [String: xpc_object_t] = [:]
|
||||
args["path"] = xpcString(path)
|
||||
|
||||
let req = makeRequest(method: "UploadAttachment", arguments: args)
|
||||
guard let reply = try await sendSync(req), xpc_get_type(reply) == XPC_TYPE_DICTIONARY else { throw Error.typeError }
|
||||
guard let guid: String = reply["upload_guid"] else { throw Error.encodingError }
|
||||
return guid
|
||||
}
|
||||
|
||||
public func openAttachmentFileHandle(attachmentId: String, preview: Bool) async throws -> FileHandle {
|
||||
var args: [String: xpc_object_t] = [:]
|
||||
@@ -274,6 +288,15 @@ extension XPCClient
|
||||
return s.withCString { ptr in xpc_string_create(ptr) }
|
||||
}
|
||||
|
||||
private func xpcStringArray(_ set: Set<String>) -> xpc_object_t {
|
||||
let array = xpc_array_create(nil, 0)
|
||||
for str in set {
|
||||
xpc_array_append_value(array, xpcString(str))
|
||||
}
|
||||
|
||||
return array
|
||||
}
|
||||
|
||||
private func sendSync(_ request: xpc_object_t) async throws -> xpc_object_t? {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
let conn: xpc_connection_t? = self.connectionQueue.sync { self.connection }
|
||||
|
||||
Reference in New Issue
Block a user