diff --git a/Monal/another.im/Views/Main/Contacts/ContactsScreen.swift b/Monal/another.im/Views/Main/Contacts/ContactsScreen.swift index ed0f4b8..0cad0fa 100644 --- a/Monal/another.im/Views/Main/Contacts/ContactsScreen.swift +++ b/Monal/another.im/Views/Main/Contacts/ContactsScreen.swift @@ -127,7 +127,16 @@ private struct ContactsScreenRow: View { } private func startChat() { - let model = wrapper.chat(with: contact) + guard let model = wrapper.chat(with: contact) else { + router.showAlert( + .alert, + title: L10n.Global.Error.title, + subtitle: L10n.Conversation.startError + ) { + Button(L10n.Global.ok, role: .cancel) {} + } + return + } router.showScreen(.push) { _ in ConversationScreen() .navigationBarHidden(true) diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index b638c57..4af1d81 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -16,7 +16,6 @@ final class WrapperChat: ObservableObject { private let xmpp: MLXMPPManager private let db: DataLayer private let monalContact: MLContact - // private let account: Account private var notificationObservers: [AnyObject] = [] init?(with: Contact) { @@ -32,9 +31,30 @@ final class WrapperChat: ObservableObject { else { return nil } self.monalContact = monalContact isOmemoEnabled = monalContact.isEncrypted + + subscribe() + NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) } - init?(with _: Chat) { + init?(with: Chat) { + xmpp = MLXMPPManager.sharedInstance() + db = DataLayer.sharedInstance() + + guard + let monalContact = db.contactList() + .first(where: { + $0.accountID.intValue == with.accountId + }) + else { return nil } + self.monalContact = monalContact + isOmemoEnabled = monalContact.isEncrypted + + guard let contact = Contact(monalContact) else { return nil } + self.contact = contact + + subscribe() + NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) + // 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 }) @@ -51,7 +71,6 @@ final class WrapperChat: ObservableObject { // 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) { @@ -83,7 +102,9 @@ final class WrapperChat: ObservableObject { to: contact.contactJid, forAccount: monalContact.accountID, withMessage: text, - actuallyFrom: account.jid, + + // MARK: WARNING! + actuallyFrom: "testmon3@test.anal.company", withId: newMessageId, encrypted: monalContact.isEncrypted, messageType: kMessageTypeText, @@ -95,13 +116,12 @@ final class WrapperChat: ObservableObject { } func requestMAM() { + guard let acc = monalContact.account else { return } 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)) + ?? 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