From 5a2596e9deb6c960a103e4be75170c4705a390d1 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 6 Dec 2021 03:22:10 +0200 Subject: [PATCH 1/2] Attempt to optimize sponsored message handling By reducing number of QVariantMap lookups and various run-time conversions to/from QString --- src/chatmodel.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index c5368f9..fc7bc19 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -49,6 +49,8 @@ namespace { const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo"); const QString INTERACTION_INFO("interaction_info"); const QString VIEW_COUNT("view_count"); + + const QString TYPE_SPONSORED_MESSAGE("sponsoredMessage"); } class ChatModel::MessageData @@ -93,6 +95,7 @@ public: public: QVariantMap messageData; const qlonglong messageId; + QString messageType; QString messageContentType; int viewCount; }; @@ -100,6 +103,7 @@ public: ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) : messageData(data), messageId(msgid), + messageType(data.value(_TYPE).toString()), messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()), viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt()) { @@ -159,6 +163,7 @@ QVector ChatModel::MessageData::diff(const MessageData *message) const uint ChatModel::MessageData::updateMessageData(const QVariantMap &data) { messageData = data; + messageType = data.value(_TYPE).toString(); return RoleFlagDisplay | updateContentType(data.value(CONTENT).toMap()) | updateViewCount(data.value(INTERACTION_INFO).toMap()); @@ -221,8 +226,8 @@ QVector ChatModel::MessageData::setInteractionInfo(const QVariantMap &info) bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2) { - bool message1Sponsored = message1->messageData.value("@type") == "sponsoredMessage"; - bool message2Sponsored = message2->messageData.value("@type") == "sponsoredMessage"; + bool message1Sponsored = message1->messageType == TYPE_SPONSORED_MESSAGE; + bool message2Sponsored = message2->messageType == TYPE_SPONSORED_MESSAGE; if (message1Sponsored && message2Sponsored) { return message1->messageId < message2->messageId; } @@ -692,11 +697,10 @@ void ChatModel::insertMessages(const QList newMessages) } else if (!newMessages.isEmpty()) { // There is only an append or a prepend, tertium non datur! (probably ;)) qlonglong lastKnownId = -1; - for (int i = (messages.size() - 1); i >=0; i-- ) { - if (messages.at(i)->messageData.value("@type").toString() == "sponsoredMessage") { - continue; - } else { - lastKnownId = messages.at(i)->messageId; + for (int i = (messages.size() - 1); i >= 0; i-- ) { + const MessageData* message = messages.at(i); + if (message->messageType != TYPE_SPONSORED_MESSAGE) { + lastKnownId = message->messageId; } } const qlonglong firstNewId = newMessages.first()->messageId; From 84cfcb51505fc888e1c4130bb930c7e27ccad6f5 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 6 Dec 2021 04:29:40 +0200 Subject: [PATCH 2/2] Contigurable mess There's no UI for that, you have to configure your mess manually)) Also, made sure that chats don't handle another char's mess. --- src/appsettings.cpp | 16 ++++++++++++++++ src/appsettings.h | 13 ++++++++++++- src/chatmodel.cpp | 8 ++++---- src/chatmodel.h | 2 +- src/tdlibreceiver.cpp | 5 +++-- src/tdlibreceiver.h | 2 +- src/tdlibwrapper.cpp | 23 +++++++++++++++++++++-- src/tdlibwrapper.h | 3 ++- 8 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/appsettings.cpp b/src/appsettings.cpp index 50d4e34..9e68a6d 100644 --- a/src/appsettings.cpp +++ b/src/appsettings.cpp @@ -36,6 +36,7 @@ namespace { const QString KEY_ONLINE_ONLY_MODE("onlineOnlyMode"); const QString KEY_DELAY_MESSAGE_READ("delayMessageRead"); const QString KEY_FOCUS_TEXTAREA_ON_CHAT_OPEN("focusTextAreaOnChatOpen"); + const QString KEY_SPONSORED_MESS("sponsoredMess"); } AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings") @@ -252,3 +253,18 @@ void AppSettings::setFocusTextAreaOnChatOpen(bool focusTextAreaOnChatOpen) emit focusTextAreaOnChatOpenChanged(); } } + +AppSettings::SponsoredMess AppSettings::getSponsoredMess() const +{ + return (SponsoredMess) settings.value(KEY_SPONSORED_MESS, (int) + AppSettings::SponsoredMessHandle).toInt(); +} + +void AppSettings::setSponsoredMess(SponsoredMess sponsoredMess) +{ + if (getSponsoredMess() != sponsoredMess) { + LOG(KEY_SPONSORED_MESS << sponsoredMess); + settings.setValue(KEY_SPONSORED_MESS, sponsoredMess); + emit sponsoredMessChanged(); + } +} diff --git a/src/appsettings.h b/src/appsettings.h index 61febbb..cc5d176 100644 --- a/src/appsettings.h +++ b/src/appsettings.h @@ -38,9 +38,16 @@ class AppSettings : public QObject { Q_PROPERTY(bool onlineOnlyMode READ onlineOnlyMode WRITE setOnlineOnlyMode NOTIFY onlineOnlyModeChanged) Q_PROPERTY(bool delayMessageRead READ delayMessageRead WRITE setDelayMessageRead NOTIFY delayMessageReadChanged) Q_PROPERTY(bool focusTextAreaOnChatOpen READ getFocusTextAreaOnChatOpen WRITE setFocusTextAreaOnChatOpen NOTIFY focusTextAreaOnChatOpenChanged) - + Q_PROPERTY(SponsoredMess sponsoredMess READ getSponsoredMess WRITE setSponsoredMess NOTIFY sponsoredMessChanged) public: + enum SponsoredMess { + SponsoredMessHandle, + SponsoredMessAutoView, + SponsoredMessIgnore + }; + Q_ENUM(SponsoredMess) + enum NotificationFeedback { NotificationFeedbackNone, NotificationFeedbackNew, @@ -96,6 +103,9 @@ public: bool getFocusTextAreaOnChatOpen() const; void setFocusTextAreaOnChatOpen(bool focusTextAreaOnChatOpen); + SponsoredMess getSponsoredMess() const; + void setSponsoredMess(SponsoredMess sponsoredMess); + signals: void sendByEnterChanged(); void focusTextAreaAfterSendChanged(); @@ -112,6 +122,7 @@ signals: void onlineOnlyModeChanged(); void delayMessageReadChanged(); void focusTextAreaOnChatOpenChanged(); + void sponsoredMessChanged(); private: QSettings settings; diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index fc7bc19..5a516bd 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -248,7 +248,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) : { this->tdLibWrapper = tdLibWrapper; connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList, int)), this, SLOT(handleMessagesReceived(QVariantList, int))); - connect(this->tdLibWrapper, SIGNAL(sponsoredMessagesReceived(QVariantList)), this, SLOT(handleSponsoredMessagesReceived(QVariantList))); + connect(this->tdLibWrapper, SIGNAL(sponsoredMessagesReceived(qlonglong, QVariantList)), this, SLOT(handleSponsoredMessagesReceived(qlonglong, QVariantList))); connect(this->tdLibWrapper, SIGNAL(newMessageReceived(qlonglong, QVariantMap)), this, SLOT(handleNewMessageReceived(qlonglong, QVariantMap))); connect(this->tdLibWrapper, SIGNAL(receivedMessage(qlonglong, qlonglong, QVariantMap)), this, SLOT(handleMessageReceived(qlonglong, qlonglong, QVariantMap))); connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); @@ -479,10 +479,10 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo } -void ChatModel::handleSponsoredMessagesReceived(const QVariantList &sponsoredMessages) +void ChatModel::handleSponsoredMessagesReceived(qlonglong chatId, const QVariantList &sponsoredMessages) { - LOG("Handling sponsored messages:" < 0) { + if (chatId == this->chatId && sponsoredMessages.size() > 0) { + LOG("Handling sponsored messages:" < messagesToBeAdded; for (QVariant sponsoredMessage: sponsoredMessages) { QVariantMap sponsoredMessageData = sponsoredMessage.toMap(); diff --git a/src/chatmodel.h b/src/chatmodel.h index 55b3c14..2c12bf4 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -63,7 +63,7 @@ signals: private slots: void handleMessagesReceived(const QVariantList &messages, int totalCount); - void handleSponsoredMessagesReceived(const QVariantList &sponsoredMessages); + void handleSponsoredMessagesReceived(qlonglong chatId, const QVariantList &sponsoredMessages); void handleNewMessageReceived(qlonglong chatId, const QVariantMap &message); void handleMessageReceived(qlonglong chatId, qlonglong messageId, const QVariantMap &message); void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount); diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index 4909ba4..63b764b 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -359,8 +359,9 @@ void TDLibReceiver::processMessages(const QVariantMap &receivedInformation) void TDLibReceiver::processSponsoredMessages(const QVariantMap &receivedInformation) { - LOG("Received sponsored messages"); - emit sponsoredMessagesReceived(receivedInformation.value("messages").toList()); + const qlonglong chatId = receivedInformation.value(EXTRA).toLongLong(); // See TDLibWrapper::getChatSponsoredMessages + LOG("Received sponsored messages for chat" << chatId); + emit sponsoredMessagesReceived(chatId, receivedInformation.value(MESSAGES).toList()); } void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation) diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 84164e8..d0917ca 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -56,7 +56,7 @@ signals: void superGroupUpdated(qlonglong groupId, const QVariantMap &groupInformation); void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount); void messagesReceived(const QVariantList &messages, int totalCount); - void sponsoredMessagesReceived(const QVariantList &sponsoredMessages); + void sponsoredMessagesReceived(qlonglong chatId, const QVariantList &messages); void newMessageReceived(qlonglong chatId, const QVariantMap &message); void messageInformation(qlonglong chatId, qlonglong messageId, const QVariantMap &message); void messageSendSucceeded(qlonglong messageId, qlonglong oldMessageId, const QVariantMap &message); diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 1f0c5ef..c1dbe59 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -118,7 +118,7 @@ void TDLibWrapper::initializeTDLibReciever() { connect(this->tdLibReceiver, SIGNAL(superGroupUpdated(qlonglong, QVariantMap)), this, SLOT(handleSuperGroupUpdated(qlonglong, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(chatOnlineMemberCountUpdated(QString, int)), this, SIGNAL(chatOnlineMemberCountUpdated(QString, int))); connect(this->tdLibReceiver, SIGNAL(messagesReceived(QVariantList, int)), this, SIGNAL(messagesReceived(QVariantList, int))); - connect(this->tdLibReceiver, SIGNAL(sponsoredMessagesReceived(QVariantList)), this, SIGNAL(sponsoredMessagesReceived(QVariantList))); + connect(this->tdLibReceiver, SIGNAL(sponsoredMessagesReceived(qlonglong, QVariantList)), this, SLOT(handleSponsoredMess(qlonglong, QVariantList))); connect(this->tdLibReceiver, SIGNAL(newMessageReceived(qlonglong, QVariantMap)), this, SIGNAL(newMessageReceived(qlonglong, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(messageInformation(qlonglong, qlonglong, QVariantMap)), this, SLOT(handleMessageInformation(qlonglong, qlonglong, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(messageSendSucceeded(qlonglong, qlonglong, QVariantMap)), this, SIGNAL(messageSendSucceeded(qlonglong, qlonglong, QVariantMap))); @@ -673,7 +673,7 @@ void TDLibWrapper::getChatSponsoredMessages(qlonglong chatId) QVariantMap requestObject; requestObject.insert(_TYPE, "getChatSponsoredMessages"); requestObject.insert(CHAT_ID, chatId); - requestObject.insert(_EXTRA, "getChatSponsoredMessages:" + QString::number(chatId)); + requestObject.insert(_EXTRA, chatId); // see TDLibReceiver::processSponsoredMessages this->sendRequest(requestObject); } @@ -1857,6 +1857,25 @@ void TDLibWrapper::handleUpdatedUserPrivacySettingRules(const QVariantMap &updat } } +void TDLibWrapper::handleSponsoredMess(qlonglong chatId, const QVariantList &messages) +{ + switch (appSettings->getSponsoredMess()) { + case AppSettings::SponsoredMessHandle: + emit sponsoredMessagesReceived(chatId, messages); + break; + case AppSettings::SponsoredMessAutoView: + LOG("Auto-viewing sponsored mess"); + for (int i = 0; i < messages.count(); i++) { + const QVariantMap mess(messages.at(i).toMap()); + viewSponsoredMessage(chatId, mess.value(ID).toULongLong()); + } + break; + case AppSettings::SponsoredMessIgnore: + LOG("Ignoring sponsored mess"); + break; + } +} + void TDLibWrapper::setInitialParameters() { LOG("Sending initial parameters to TD Lib"); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 6aba8d2..004403b 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -262,7 +262,7 @@ signals: void superGroupUpdated(qlonglong groupId); void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount); void messagesReceived(const QVariantList &messages, int totalCount); - void sponsoredMessagesReceived(const QVariantList &sponsoredMessages); + void sponsoredMessagesReceived(qlonglong chatId, const QVariantList &messages); void newMessageReceived(qlonglong chatId, const QVariantMap &message); void copyToDownloadsSuccessful(const QString &fileName, const QString &filePath); void copyToDownloadsError(const QString &fileName, const QString &filePath); @@ -335,6 +335,7 @@ public slots: void handleMessageIsPinnedUpdated(qlonglong chatId, qlonglong messageId, bool isPinned); void handleUserPrivacySettingRules(const QVariantMap &rules); void handleUpdatedUserPrivacySettingRules(const QVariantMap &updatedRules); + void handleSponsoredMess(qlonglong chatId, const QVariantList &messages); private: void setOption(const QString &name, const QString &type, const QVariant &value);