From 51cd7f82f6f03c62ac795295190cf63bd84359c9 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 17 Aug 2024 10:06:28 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinMessages.swift | 6 ------ ConversationsClassic/AppData/Client/Client.swift | 15 ++------------- ConversationsClassic/AppData/Model/Message.swift | 10 +++++++++- .../AppData/Store/ConversationStore.swift | 7 +++++-- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift index 67fb35f..a5b44c0 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift @@ -69,12 +69,6 @@ final class ClientMartinMessagesManager { ) // Save message - Task { - await updateMsgInDB(msg) - } - } - - private func updateMsgInDB(_ msg: Message) async { Task { do { try await msg.save() diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 50fabb8..db99443 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -87,7 +87,7 @@ extension Client { } extension Client { - func sendMessage(_ message: Message) async { + func sendMessage(_ message: Message) async throws { guard let to = message.to else { return } @@ -96,19 +96,8 @@ extension Client { } let msg = chat.createMessage(text: message.body ?? "??", id: message.id) - do { - try await chat.send(message: msg) - } catch { - print("Error sending message: \(error)") - } + try await chat.send(message: msg) } - // func sendMessage(_ message: String, to roster: Roster) async { - // guard let chat = chatsManager.chat(for: connection.context, with: BareJID(roster.contactBareJid)) else { - // return - // } - // let message = chat.createMessage(text: message, id: UUID().uuidString) - // try? await chat.send(message: message) - // } } extension Client { diff --git a/ConversationsClassic/AppData/Model/Message.swift b/ConversationsClassic/AppData/Model/Message.swift index a9a4a26..e327afb 100644 --- a/ConversationsClassic/AppData/Model/Message.swift +++ b/ConversationsClassic/AppData/Model/Message.swift @@ -26,7 +26,7 @@ struct Message: DBStorable, Equatable { let type: MessageType let date: Date let contentType: MessageContentType - let status: MessageStatus + var status: MessageStatus let from: String let to: String? @@ -43,4 +43,12 @@ extension Message { try self.insert(db) } } + + func setStatus(_ status: MessageStatus) async throws { + try await Database.shared.dbQueue.write { db in + var updatedMessage = self + updatedMessage.status = status + try updatedMessage.update(db, columns: ["status"]) + } + } } diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/ConversationStore.swift index 4d6d1da..817f7aa 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/ConversationStore.swift @@ -42,8 +42,11 @@ extension ConversationStore { // store as pending on db, and send do { try await message.save() - await client.sendMessage(message) - } catch {} + try await client.sendMessage(message) + try await message.setStatus(.sent) + } catch { + try? await message.setStatus(.error) + } } }