Follow-up from "Send encrypted messages for inactive chats"
The following discussion from !852 (merged) should be addressed:
-
@lnj started a discussion: Re:
MessageHandler::send()
QFutureInterface<QXmpp::SendResult> interface(QFutureInterfaceBase::Started); const auto recipientJid = message.to(); auto sendEncrypted = [=, this]() mutable { await(m_client->send(std::move(message)), this, [=](QXmpp::SendResult result) mutable { reportFinishedResult(interface, result); }); }; auto sendUnencrypted = [=, this]() mutable { await(m_client->sendUnencrypted(std::move(message)), this, [=](QXmpp::SendResult result) mutable { reportFinishedResult(interface, result); }); }; // If the message is sent for the current chat, its information is used to determine whether to // send encrypted. // Otherwise, that information is retrieved from the database. runOnThread(MessageModel::instance(), [accountJid = AccountManager::instance()->jid(), recipientJid]() { return MessageModel::instance()->isChatCurrentChat(accountJid, recipientJid); }, this, [=, this](bool isChatCurrentChat) mutable { if (isChatCurrentChat) { runOnThread(MessageModel::instance(), []() { return MessageModel::instance()->isOmemoEncryptionEnabled(); }, this, [=](bool isOmemoEncryptionEnabled) mutable { if (isOmemoEncryptionEnabled) { sendEncrypted(); } else { sendUnencrypted(); } }); } else { runOnThread(RosterModel::instance(), [accountJid = AccountManager::instance()->jid(), recipientJid]() { return RosterModel::instance()->itemEncryption(accountJid, recipientJid).value_or(Encryption::NoEncryption); }, this, [=, this](Encryption::Enum activeEncryption) mutable { if (activeEncryption == Encryption::Omemo2) { auto future = m_clientWorker->omemoManager()->hasUsableDevices({ recipientJid }); await(future, this, [=](bool hasUsableDevices) mutable { const auto isOmemoEncryptionEnabled = activeEncryption == Encryption::Omemo2 && hasUsableDevices; if (isOmemoEncryptionEnabled) { sendEncrypted(); } else { sendUnencrypted(); } }); } else { sendUnencrypted(); } }); } }); return interface.future();
This is complex and has too many thread jumps and generates another QFuture just for sending a simple message.
Ideas from me how to fix this:
- cache the usable devices property
- use a parameter here and decide whether to encrypt when calling this (e.g. already in the MessageModel, it seems to be much easier there)
Solution would be to use a proper Cache
Edited by Linus Jahn