Merge pull request #455 from monich/optimal-mess

Attempt to optimize sponsored message handling
This commit is contained in:
Sebastian Wolf 2021-12-06 22:27:31 +01:00 committed by GitHub
commit fa59ca0bb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 19 deletions

View file

@ -36,6 +36,7 @@ namespace {
const QString KEY_ONLINE_ONLY_MODE("onlineOnlyMode"); const QString KEY_ONLINE_ONLY_MODE("onlineOnlyMode");
const QString KEY_DELAY_MESSAGE_READ("delayMessageRead"); const QString KEY_DELAY_MESSAGE_READ("delayMessageRead");
const QString KEY_FOCUS_TEXTAREA_ON_CHAT_OPEN("focusTextAreaOnChatOpen"); 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") AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings")
@ -252,3 +253,18 @@ void AppSettings::setFocusTextAreaOnChatOpen(bool focusTextAreaOnChatOpen)
emit focusTextAreaOnChatOpenChanged(); 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();
}
}

View file

@ -38,9 +38,16 @@ class AppSettings : public QObject {
Q_PROPERTY(bool onlineOnlyMode READ onlineOnlyMode WRITE setOnlineOnlyMode NOTIFY onlineOnlyModeChanged) Q_PROPERTY(bool onlineOnlyMode READ onlineOnlyMode WRITE setOnlineOnlyMode NOTIFY onlineOnlyModeChanged)
Q_PROPERTY(bool delayMessageRead READ delayMessageRead WRITE setDelayMessageRead NOTIFY delayMessageReadChanged) Q_PROPERTY(bool delayMessageRead READ delayMessageRead WRITE setDelayMessageRead NOTIFY delayMessageReadChanged)
Q_PROPERTY(bool focusTextAreaOnChatOpen READ getFocusTextAreaOnChatOpen WRITE setFocusTextAreaOnChatOpen NOTIFY focusTextAreaOnChatOpenChanged) Q_PROPERTY(bool focusTextAreaOnChatOpen READ getFocusTextAreaOnChatOpen WRITE setFocusTextAreaOnChatOpen NOTIFY focusTextAreaOnChatOpenChanged)
Q_PROPERTY(SponsoredMess sponsoredMess READ getSponsoredMess WRITE setSponsoredMess NOTIFY sponsoredMessChanged)
public: public:
enum SponsoredMess {
SponsoredMessHandle,
SponsoredMessAutoView,
SponsoredMessIgnore
};
Q_ENUM(SponsoredMess)
enum NotificationFeedback { enum NotificationFeedback {
NotificationFeedbackNone, NotificationFeedbackNone,
NotificationFeedbackNew, NotificationFeedbackNew,
@ -96,6 +103,9 @@ public:
bool getFocusTextAreaOnChatOpen() const; bool getFocusTextAreaOnChatOpen() const;
void setFocusTextAreaOnChatOpen(bool focusTextAreaOnChatOpen); void setFocusTextAreaOnChatOpen(bool focusTextAreaOnChatOpen);
SponsoredMess getSponsoredMess() const;
void setSponsoredMess(SponsoredMess sponsoredMess);
signals: signals:
void sendByEnterChanged(); void sendByEnterChanged();
void focusTextAreaAfterSendChanged(); void focusTextAreaAfterSendChanged();
@ -112,6 +122,7 @@ signals:
void onlineOnlyModeChanged(); void onlineOnlyModeChanged();
void delayMessageReadChanged(); void delayMessageReadChanged();
void focusTextAreaOnChatOpenChanged(); void focusTextAreaOnChatOpenChanged();
void sponsoredMessChanged();
private: private:
QSettings settings; QSettings settings;

View file

@ -49,6 +49,8 @@ namespace {
const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo"); const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo");
const QString INTERACTION_INFO("interaction_info"); const QString INTERACTION_INFO("interaction_info");
const QString VIEW_COUNT("view_count"); const QString VIEW_COUNT("view_count");
const QString TYPE_SPONSORED_MESSAGE("sponsoredMessage");
} }
class ChatModel::MessageData class ChatModel::MessageData
@ -93,6 +95,7 @@ public:
public: public:
QVariantMap messageData; QVariantMap messageData;
const qlonglong messageId; const qlonglong messageId;
QString messageType;
QString messageContentType; QString messageContentType;
int viewCount; int viewCount;
}; };
@ -100,6 +103,7 @@ public:
ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) : ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) :
messageData(data), messageData(data),
messageId(msgid), messageId(msgid),
messageType(data.value(_TYPE).toString()),
messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()), messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()),
viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt()) viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt())
{ {
@ -159,6 +163,7 @@ QVector<int> ChatModel::MessageData::diff(const MessageData *message) const
uint ChatModel::MessageData::updateMessageData(const QVariantMap &data) uint ChatModel::MessageData::updateMessageData(const QVariantMap &data)
{ {
messageData = data; messageData = data;
messageType = data.value(_TYPE).toString();
return RoleFlagDisplay | return RoleFlagDisplay |
updateContentType(data.value(CONTENT).toMap()) | updateContentType(data.value(CONTENT).toMap()) |
updateViewCount(data.value(INTERACTION_INFO).toMap()); updateViewCount(data.value(INTERACTION_INFO).toMap());
@ -221,8 +226,8 @@ QVector<int> ChatModel::MessageData::setInteractionInfo(const QVariantMap &info)
bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2) bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2)
{ {
bool message1Sponsored = message1->messageData.value("@type") == "sponsoredMessage"; bool message1Sponsored = message1->messageType == TYPE_SPONSORED_MESSAGE;
bool message2Sponsored = message2->messageData.value("@type") == "sponsoredMessage"; bool message2Sponsored = message2->messageType == TYPE_SPONSORED_MESSAGE;
if (message1Sponsored && message2Sponsored) { if (message1Sponsored && message2Sponsored) {
return message1->messageId < message2->messageId; return message1->messageId < message2->messageId;
} }
@ -243,7 +248,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) :
{ {
this->tdLibWrapper = tdLibWrapper; this->tdLibWrapper = tdLibWrapper;
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList, int)), this, SLOT(handleMessagesReceived(QVariantList, int))); 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(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(receivedMessage(qlonglong, qlonglong, QVariantMap)), this, SLOT(handleMessageReceived(qlonglong, qlonglong, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int)));
@ -474,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:" <<sponsoredMessages.size()); if (chatId == this->chatId && sponsoredMessages.size() > 0) {
if (sponsoredMessages.size() > 0) { LOG("Handling sponsored messages:" <<sponsoredMessages.size());
QList<MessageData*> messagesToBeAdded; QList<MessageData*> messagesToBeAdded;
for (QVariant sponsoredMessage: sponsoredMessages) { for (QVariant sponsoredMessage: sponsoredMessages) {
QVariantMap sponsoredMessageData = sponsoredMessage.toMap(); QVariantMap sponsoredMessageData = sponsoredMessage.toMap();
@ -692,11 +697,10 @@ void ChatModel::insertMessages(const QList<MessageData*> newMessages)
} else if (!newMessages.isEmpty()) { } else if (!newMessages.isEmpty()) {
// There is only an append or a prepend, tertium non datur! (probably ;)) // There is only an append or a prepend, tertium non datur! (probably ;))
qlonglong lastKnownId = -1; qlonglong lastKnownId = -1;
for (int i = (messages.size() - 1); i >=0; i-- ) { for (int i = (messages.size() - 1); i >= 0; i-- ) {
if (messages.at(i)->messageData.value("@type").toString() == "sponsoredMessage") { const MessageData* message = messages.at(i);
continue; if (message->messageType != TYPE_SPONSORED_MESSAGE) {
} else { lastKnownId = message->messageId;
lastKnownId = messages.at(i)->messageId;
} }
} }
const qlonglong firstNewId = newMessages.first()->messageId; const qlonglong firstNewId = newMessages.first()->messageId;

View file

@ -63,7 +63,7 @@ signals:
private slots: private slots:
void handleMessagesReceived(const QVariantList &messages, int totalCount); 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 handleNewMessageReceived(qlonglong chatId, const QVariantMap &message);
void handleMessageReceived(qlonglong chatId, qlonglong messageId, const QVariantMap &message); void handleMessageReceived(qlonglong chatId, qlonglong messageId, const QVariantMap &message);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount); void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);

View file

@ -359,8 +359,9 @@ void TDLibReceiver::processMessages(const QVariantMap &receivedInformation)
void TDLibReceiver::processSponsoredMessages(const QVariantMap &receivedInformation) void TDLibReceiver::processSponsoredMessages(const QVariantMap &receivedInformation)
{ {
LOG("Received sponsored messages"); const qlonglong chatId = receivedInformation.value(EXTRA).toLongLong(); // See TDLibWrapper::getChatSponsoredMessages
emit sponsoredMessagesReceived(receivedInformation.value("messages").toList()); LOG("Received sponsored messages for chat" << chatId);
emit sponsoredMessagesReceived(chatId, receivedInformation.value(MESSAGES).toList());
} }
void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation)

View file

@ -56,7 +56,7 @@ signals:
void superGroupUpdated(qlonglong groupId, const QVariantMap &groupInformation); void superGroupUpdated(qlonglong groupId, const QVariantMap &groupInformation);
void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount); void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount);
void messagesReceived(const QVariantList &messages, int totalCount); 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 newMessageReceived(qlonglong chatId, const QVariantMap &message);
void messageInformation(qlonglong chatId, qlonglong messageId, const QVariantMap &message); void messageInformation(qlonglong chatId, qlonglong messageId, const QVariantMap &message);
void messageSendSucceeded(qlonglong messageId, qlonglong oldMessageId, const QVariantMap &message); void messageSendSucceeded(qlonglong messageId, qlonglong oldMessageId, const QVariantMap &message);

View file

@ -118,7 +118,7 @@ void TDLibWrapper::initializeTDLibReciever() {
connect(this->tdLibReceiver, SIGNAL(superGroupUpdated(qlonglong, QVariantMap)), this, SLOT(handleSuperGroupUpdated(qlonglong, QVariantMap))); 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(chatOnlineMemberCountUpdated(QString, int)), this, SIGNAL(chatOnlineMemberCountUpdated(QString, int)));
connect(this->tdLibReceiver, SIGNAL(messagesReceived(QVariantList, int)), this, SIGNAL(messagesReceived(QVariantList, 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(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(messageInformation(qlonglong, qlonglong, QVariantMap)), this, SLOT(handleMessageInformation(qlonglong, qlonglong, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(messageSendSucceeded(qlonglong, qlonglong, QVariantMap)), this, SIGNAL(messageSendSucceeded(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; QVariantMap requestObject;
requestObject.insert(_TYPE, "getChatSponsoredMessages"); requestObject.insert(_TYPE, "getChatSponsoredMessages");
requestObject.insert(CHAT_ID, chatId); requestObject.insert(CHAT_ID, chatId);
requestObject.insert(_EXTRA, "getChatSponsoredMessages:" + QString::number(chatId)); requestObject.insert(_EXTRA, chatId); // see TDLibReceiver::processSponsoredMessages
this->sendRequest(requestObject); 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() void TDLibWrapper::setInitialParameters()
{ {
LOG("Sending initial parameters to TD Lib"); LOG("Sending initial parameters to TD Lib");

View file

@ -262,7 +262,7 @@ signals:
void superGroupUpdated(qlonglong groupId); void superGroupUpdated(qlonglong groupId);
void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount); void chatOnlineMemberCountUpdated(const QString &chatId, int onlineMemberCount);
void messagesReceived(const QVariantList &messages, int totalCount); 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 newMessageReceived(qlonglong chatId, const QVariantMap &message);
void copyToDownloadsSuccessful(const QString &fileName, const QString &filePath); void copyToDownloadsSuccessful(const QString &fileName, const QString &filePath);
void copyToDownloadsError(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 handleMessageIsPinnedUpdated(qlonglong chatId, qlonglong messageId, bool isPinned);
void handleUserPrivacySettingRules(const QVariantMap &rules); void handleUserPrivacySettingRules(const QVariantMap &rules);
void handleUpdatedUserPrivacySettingRules(const QVariantMap &updatedRules); void handleUpdatedUserPrivacySettingRules(const QVariantMap &updatedRules);
void handleSponsoredMess(qlonglong chatId, const QVariantList &messages);
private: private:
void setOption(const QString &name, const QString &type, const QVariant &value); void setOption(const QString &name, const QString &type, const QVariant &value);