From c69e7e50cfca028c7945b9fece1a8f4eec145a7e Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 3 Sep 2024 16:06:23 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client.swift | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 35f885e..418dce6 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -102,8 +102,9 @@ extension Client { return } - let msg = chat.createMessage(text: message.body ?? "??", id: message.id) + var msg = chat.createMessage(text: message.body ?? "??", id: message.id) msg.oob = message.oobUrl + msg = try await encryptMessage(msg) try await chat.send(message: msg) } @@ -151,6 +152,33 @@ extension Client { } } +private extension Client { + func encryptMessage(_ message: Martin.Message) async throws -> Martin.Message { + try await withCheckedThrowingContinuation { continuation in + connection.module(.omemo).encode(message: message, completionHandler: { result in + switch result { + case .successMessage(let encodedMessage, _): + // guard connection.isConnected else { + // continuation.resume(returning: message) + // return + // } + continuation.resume(returning: encodedMessage) + + case .failure(let error): + var errorMessage = NSLocalizedString("It was not possible to send encrypted message due to encryption error", comment: "message encryption failure") + switch error { + case .noSession: + errorMessage = NSLocalizedString("There is no trusted device to send message to", comment: "message encryption failure") + default: + break + } + continuation.resume(throwing: XMPPError.unexpected_request(errorMessage)) + } + }) + } + } +} + extension Client { static func tryLogin(with credentials: Credentials) async throws -> Client { let client = Client(credentials: credentials)