From cc2207ba8284ae37b5195392782138cd5039acd9 Mon Sep 17 00:00:00 2001 From: Woit Date: Sat, 7 Dec 2024 00:30:27 +0100 Subject: [PATCH] wip --- Monal/another.im/XMPP/Models/Message.swift | 6 +-- .../XMPP/Wrappers/WrapperChat.swift | 37 +++++++------------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/Monal/another.im/XMPP/Models/Message.swift b/Monal/another.im/XMPP/Models/Message.swift index 73e31a6..f84a1e5 100644 --- a/Monal/another.im/XMPP/Models/Message.swift +++ b/Monal/another.im/XMPP/Models/Message.swift @@ -13,12 +13,13 @@ struct Message: Identifiable { let accountId: Int let participantJid: String let dbId: Int + let messageId: String let stanzaId: String let timestamp: Date let body: String let isInbound: Bool let encrypted: Bool - let status: MessageStatus + var status: MessageStatus var id: String { "\(accountId)|\(dbId)" @@ -29,6 +30,7 @@ struct Message: Identifiable { accountId = accId participantJid = obj.participantJid self.dbId = dbId + messageId = obj.messageId stanzaId = obj.stanzaId timestamp = obj.timestamp body = obj.messageText @@ -50,7 +52,5 @@ struct Message: Identifiable { } } } - - // print("AAAAAA", body, status, "\n", "orig: ", obj.hasBeenReceived, obj.hasBeenSent, " id:", dbId, " stanza:", stanzaId) } } diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index 307b47f..cb29150 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -28,7 +28,8 @@ final class WrapperChat: ObservableObject { guard let monalContact = db.contactList() .first(where: { - $0.accountID.intValue == with.ownerId + $0.accountID.intValue == with.ownerId && + $0.contactJid == with.contactJid }) else { return nil } self.monalContact = monalContact @@ -46,7 +47,8 @@ final class WrapperChat: ObservableObject { guard let monalContact = db.contactList() .first(where: { - $0.accountID.intValue == with.accountId + $0.accountID.intValue == with.accountId && + $0.contactJid == with.participantJid }) else { return nil } self.monalContact = monalContact @@ -57,23 +59,6 @@ final class WrapperChat: ObservableObject { subscribe() reloadMessages() - - // guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil } - // - // var contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) - // if contact == nil { - // let semaphore = DispatchSemaphore(value: 0) - // Task { - // await addContact(contactJid: with.participantJid, forAccountID: with.accountId) - // semaphore.signal() - // } - // semaphore.wait() - // contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) - // } - // - // guard let contact else { return nil } - // let chatModel = WrapperChat(account: account, contact: contact, db: db, xmpp: xmpp) - // return chatModel } deinit { @@ -100,7 +85,6 @@ final class WrapperChat: ObservableObject { if let newMlMessage = db.message(forHistoryID: histId), let message = Message(newMlMessage) { messages.insert(message, at: 0) } - print("CCCCC", newMessageId) xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId) } @@ -109,13 +93,16 @@ final class WrapperChat: ObservableObject { if mamRequestInProgress { return } mamRequestInProgress = true - let lastStanzaId = messages.last?.stanzaId // last here because list is reversed + let lastStanzaId = messages.last?.messageId // last here because list is reversed ?? db.lastStanzaId(forAccount: NSNumber(value: contact.ownerId)) acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in self?.mamRequestInProgress = false - print(msgs ?? []) + if let msgs, !msgs.isEmpty { + print(msgs) + self?.reloadMessages() + } } } } @@ -142,8 +129,10 @@ private extension WrapperChat { switch notification.name.rawValue { case kMonalSentMessageNotice: if let stanza = notification.userInfo?["message"] as? XMPPStanza, let id = stanza.id { - if let index = messages.firstIndex(where: { $0.stanzaId == id }) { - print(index) + if let index = messages.firstIndex(where: { $0.messageId == id }) { + var message = messages[index] + message.status = .sent + messages[index] = message } }