This commit is contained in:
Woit 2024-12-06 08:55:59 +01:00
parent 006ae2a612
commit c8cf403d15
2 changed files with 51 additions and 35 deletions

View file

@ -13,28 +13,62 @@ final class WrapperChat: ObservableObject {
let contact: Contact
private let monalContact: MLContact
private let account: Account
private let xmpp: MLXMPPManager
private let db: DataLayer
private let monalContact: MLContact
// private let account: Account
private var notificationObservers: [AnyObject] = []
init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
self.contact = contact
self.account = account
self.db = db
self.xmpp = xmpp
init?(with: Contact) {
contact = with
xmpp = MLXMPPManager.sharedInstance()
db = DataLayer.sharedInstance()
// swiftlint:disable:next force_unwrapping
monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }!
guard
let monalContact = db.contactList()
.first(where: {
$0.accountID.intValue == with.ownerId
})
else { return nil }
self.monalContact = monalContact
isOmemoEnabled = monalContact.isEncrypted
subscribe()
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
//
}
init?(with _: Chat) {
// 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
nil
}
// init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
// self.contact = contact
// self.account = account
// self.db = db
// self.xmpp = xmpp
//
// monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }!
// isOmemoEnabled = monalContact.isEncrypted
//
// subscribe()
// NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
//
// //
// }
deinit {
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
}

View file

@ -56,30 +56,12 @@ extension WrapperXMPP {
}
}
func chat(with: Contact) -> WrapperChat {
// swiftlint:disable:next force_unwrapping
let account = accounts.first { $0.id == with.ownerId }!
let chatModel = WrapperChat(account: account, contact: with, db: db, xmpp: xmpp)
return chatModel
func chat(with: Contact) -> WrapperChat? {
WrapperChat(with: with)
}
func chat(with: Chat) -> WrapperChat? {
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
WrapperChat(with: with)
}
}