This commit is contained in:
Woit 2024-12-07 00:30:27 +01:00
parent b4a708ffad
commit cc2207ba82
2 changed files with 16 additions and 27 deletions

View file

@ -13,12 +13,13 @@ struct Message: Identifiable {
let accountId: Int let accountId: Int
let participantJid: String let participantJid: String
let dbId: Int let dbId: Int
let messageId: String
let stanzaId: String let stanzaId: String
let timestamp: Date let timestamp: Date
let body: String let body: String
let isInbound: Bool let isInbound: Bool
let encrypted: Bool let encrypted: Bool
let status: MessageStatus var status: MessageStatus
var id: String { var id: String {
"\(accountId)|\(dbId)" "\(accountId)|\(dbId)"
@ -29,6 +30,7 @@ struct Message: Identifiable {
accountId = accId accountId = accId
participantJid = obj.participantJid participantJid = obj.participantJid
self.dbId = dbId self.dbId = dbId
messageId = obj.messageId
stanzaId = obj.stanzaId stanzaId = obj.stanzaId
timestamp = obj.timestamp timestamp = obj.timestamp
body = obj.messageText body = obj.messageText
@ -50,7 +52,5 @@ struct Message: Identifiable {
} }
} }
} }
// print("AAAAAA", body, status, "\n", "orig: ", obj.hasBeenReceived, obj.hasBeenSent, " id:", dbId, " stanza:", stanzaId)
} }
} }

View file

@ -28,7 +28,8 @@ final class WrapperChat: ObservableObject {
guard guard
let monalContact = db.contactList() let monalContact = db.contactList()
.first(where: { .first(where: {
$0.accountID.intValue == with.ownerId $0.accountID.intValue == with.ownerId &&
$0.contactJid == with.contactJid
}) })
else { return nil } else { return nil }
self.monalContact = monalContact self.monalContact = monalContact
@ -46,7 +47,8 @@ final class WrapperChat: ObservableObject {
guard guard
let monalContact = db.contactList() let monalContact = db.contactList()
.first(where: { .first(where: {
$0.accountID.intValue == with.accountId $0.accountID.intValue == with.accountId &&
$0.contactJid == with.participantJid
}) })
else { return nil } else { return nil }
self.monalContact = monalContact self.monalContact = monalContact
@ -57,23 +59,6 @@ final class WrapperChat: ObservableObject {
subscribe() subscribe()
reloadMessages() 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 { deinit {
@ -100,7 +85,6 @@ final class WrapperChat: ObservableObject {
if let newMlMessage = db.message(forHistoryID: histId), let message = Message(newMlMessage) { if let newMlMessage = db.message(forHistoryID: histId), let message = Message(newMlMessage) {
messages.insert(message, at: 0) messages.insert(message, at: 0)
} }
print("CCCCC", newMessageId)
xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId) xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId)
} }
@ -109,13 +93,16 @@ final class WrapperChat: ObservableObject {
if mamRequestInProgress { return } if mamRequestInProgress { return }
mamRequestInProgress = true 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)) ?? db.lastStanzaId(forAccount: NSNumber(value: contact.ownerId))
acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.mamRequestInProgress = false 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 { switch notification.name.rawValue {
case kMonalSentMessageNotice: case kMonalSentMessageNotice:
if let stanza = notification.userInfo?["message"] as? XMPPStanza, let id = stanza.id { if let stanza = notification.userInfo?["message"] as? XMPPStanza, let id = stanza.id {
if let index = messages.firstIndex(where: { $0.stanzaId == id }) { if let index = messages.firstIndex(where: { $0.messageId == id }) {
print(index) var message = messages[index]
message.status = .sent
messages[index] = message
} }
} }