This commit is contained in:
fmodf 2024-08-17 10:06:28 +02:00
parent 66262f2f75
commit 51cd7f82f6
4 changed files with 16 additions and 22 deletions

View file

@ -69,12 +69,6 @@ final class ClientMartinMessagesManager {
) )
// Save message // Save message
Task {
await updateMsgInDB(msg)
}
}
private func updateMsgInDB(_ msg: Message) async {
Task { Task {
do { do {
try await msg.save() try await msg.save()

View file

@ -87,7 +87,7 @@ extension Client {
} }
extension Client { extension Client {
func sendMessage(_ message: Message) async { func sendMessage(_ message: Message) async throws {
guard let to = message.to else { guard let to = message.to else {
return return
} }
@ -96,19 +96,8 @@ extension Client {
} }
let msg = chat.createMessage(text: message.body ?? "??", id: message.id) let msg = chat.createMessage(text: message.body ?? "??", id: message.id)
do {
try await chat.send(message: msg) try await chat.send(message: msg)
} catch {
print("Error sending message: \(error)")
} }
}
// 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 { extension Client {

View file

@ -26,7 +26,7 @@ struct Message: DBStorable, Equatable {
let type: MessageType let type: MessageType
let date: Date let date: Date
let contentType: MessageContentType let contentType: MessageContentType
let status: MessageStatus var status: MessageStatus
let from: String let from: String
let to: String? let to: String?
@ -43,4 +43,12 @@ extension Message {
try self.insert(db) 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"])
}
}
} }

View file

@ -42,8 +42,11 @@ extension ConversationStore {
// store as pending on db, and send // store as pending on db, and send
do { do {
try await message.save() try await message.save()
await client.sendMessage(message) try await client.sendMessage(message)
} catch {} try await message.setStatus(.sent)
} catch {
try? await message.setStatus(.error)
}
} }
} }