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_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();
}
}

View file

@ -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;

View file

@ -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<int> 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<int> 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;
}
@ -243,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)));
@ -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)
{
if (chatId == this->chatId && sponsoredMessages.size() > 0) {
LOG("Handling sponsored messages:" <<sponsoredMessages.size());
if (sponsoredMessages.size() > 0) {
QList<MessageData*> messagesToBeAdded;
for (QVariant sponsoredMessage: sponsoredMessages) {
QVariantMap sponsoredMessageData = sponsoredMessage.toMap();
@ -692,11 +697,10 @@ void ChatModel::insertMessages(const QList<MessageData*> 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;

View file

@ -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);

View file

@ -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)

View file

@ -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);

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(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");

View file

@ -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);