another.im-ios/Monal/another.im/XMPP/Wrappers/WrapperChat.swift

112 lines
3.7 KiB
Swift
Raw Normal View History

2024-12-02 15:50:23 +00:00
import Foundation
import monalxmpp
final class WrapperChat: ObservableObject {
@Published private(set) var messages: [Message] = []
@Published var replyText: String = ""
@Published private(set) var mamRequestInProgress = false
@Published var isOmemoEnabled: Bool {
didSet {
toggleOmemo(isOmemoEnabled)
}
}
let contact: Contact
private let monalContact: MLContact
private let account: Account
private let xmpp: MLXMPPManager
private let db: DataLayer
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
// swiftlint:disable:next force_unwrapping
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) }
}
var chatTitle: String {
contact.name
}
func sendText(_ text: String) {
let newMessageId = UUID().uuidString
_ = db.addMessageHistory(
to: contact.contactJid,
forAccount: monalContact.accountID,
withMessage: text,
actuallyFrom: account.jid,
withId: newMessageId,
encrypted: monalContact.isEncrypted,
messageType: kMessageTypeText,
mimeType: nil,
size: nil
)
print(newMessageId)
xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId)
}
func requestMAM() {
if mamRequestInProgress { return }
mamRequestInProgress = true
guard let acc = xmpp.getEnabledAccount(forID: NSNumber(value: account.id)) else { return }
let lastStanzaId = messages.last?.stanzaId // last here because list is reversed
?? db.lastStanzaId(forAccount: NSNumber(value: account.id))
acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.mamRequestInProgress = false
if !(msgs ?? []).isEmpty {
self?.refreshMessages()
}
}
}
}
}
private extension WrapperChat {
func subscribe() {
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(newMsg)
let sentMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalSentMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(sentMsg)
}
func refreshMessages() {
let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
.compactMap { obj -> Message? in
guard let message = obj as? MLMessage else { return nil }
return Message(message)
}
.sorted { $0.timestamp > $1.timestamp }
self.messages = messages
}
func toggleOmemo(_ new: Bool) {
if monalContact.isEncrypted != new {
monalContact.toggleEncryption(new)
}
}
}