From 4327fd4c58952a99496737cffb11bd8de0203ac7 Mon Sep 17 00:00:00 2001 From: AlexeyZavar Date: Wed, 5 Feb 2025 22:49:33 +0300 Subject: [PATCH] fix: reduce CPU usage for TTL messages & refactor --- .../SourceFiles/ayu/data/messages_storage.cpp | 2 ++ .../ayu/ui/message_history/history_inner.cpp | 2 +- .../ayu/utils/telegram_helpers.cpp | 23 +++++++++++++ .../SourceFiles/ayu/utils/telegram_helpers.h | 3 ++ Telegram/SourceFiles/data/data_session.cpp | 34 ++++--------------- Telegram/SourceFiles/history/history_item.cpp | 2 +- Telegram/SourceFiles/history/history_item.h | 3 +- 7 files changed, 38 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/ayu/data/messages_storage.cpp b/Telegram/SourceFiles/ayu/data/messages_storage.cpp index 252be36ac..22721c9be 100644 --- a/Telegram/SourceFiles/ayu/data/messages_storage.cpp +++ b/Telegram/SourceFiles/ayu/data/messages_storage.cpp @@ -44,6 +44,8 @@ void map(not_null item, AyuMessageBase &message) { message.fromId = item->from()->id.value & PeerId::kChatTypeMask; if (item->topic()) { message.topicId = item->topicRootId().bare; + } else { + message.topicId = 0; } message.messageId = item->id.bare; message.date = item->date(); diff --git a/Telegram/SourceFiles/ayu/ui/message_history/history_inner.cpp b/Telegram/SourceFiles/ayu/ui/message_history/history_inner.cpp index be7ae7a3d..8065f2d57 100644 --- a/Telegram/SourceFiles/ayu/ui/message_history/history_inner.cpp +++ b/Telegram/SourceFiles/ayu/ui/message_history/history_inner.cpp @@ -702,7 +702,7 @@ void InnerWidget::addMessages(Direction direction, const std::vector item) { + const auto settings = &AyuSettings::getInstance(); + + if (!settings->saveDeletedMessages) { + return false; + } + + if (const auto possiblyBot = item->history()->peer->asUser()) { + return !possiblyBot->isBot() || (settings->saveForBots && possiblyBot->isBot()); + } + return true; +} + +void processMessageDelete(not_null item) { + if (!isMessageSavable(item)) { + item->destroy(); + } else { + item->setDeleted(); + AyuMessages::addDeletedMessage(item); + } +} + void resolveUser(ID userId, const QString &username, Main::Session *session, const Callback &callback) { auto normalized = username.trimmed().toLower(); if (normalized.isEmpty()) { diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h index 739aa83e5..0ac82094b 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h @@ -48,6 +48,9 @@ QString getPeerDC(not_null peer); int getScheduleTime(int64 sumSize); +bool isMessageSavable(not_null item); +void processMessageDelete(not_null item); + void searchById(ID userId, Main::Session *session, bool retry, const Callback &callback); void searchById(ID userId, Main::Session *session, const Callback &callback); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 013fe104a..1d30e1b22 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -83,8 +83,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL // AyuGram includes #include "ayu/ayu_settings.h" -#include "ayu/ayu_state.h" #include "ayu/data/messages_storage.h" +#include "ayu/utils/telegram_helpers.h" namespace Data { @@ -215,19 +215,6 @@ void CheckForSwitchInlineButton(not_null item) { double(std::numeric_limits::max()))); } -bool NeedSaveMessage(not_null item) { - const auto settings = &AyuSettings::getInstance(); - - if (!settings->saveDeletedMessages) { - return false; - } - - if (const auto possiblyBot = item->history()->peer->asUser()) { - return !possiblyBot->isBot() || (settings->saveForBots && possiblyBot->isBot()); - } - return true; -} - } // namespace Session::Session(not_null session) @@ -2566,7 +2553,10 @@ void Session::checkTTLs() { return pair.second; }) | ranges::views::join; for (auto &item : toBeRemoved) { - item->setDeleted(); + // remove message from `_ttlMessages` to avoid calling this method infinitely + item->applyTTL(0); + + processMessageDelete(item); } } else { while (!_ttlMessages.empty() && _ttlMessages.begin()->first <= now) { @@ -2591,12 +2581,7 @@ void Session::processMessagesDeleted( if (list && i != list->end()) { const auto history = i->second->history(); - if (!NeedSaveMessage(i->second)) { - i->second->destroy(); - } else { - i->second->setDeleted(); - AyuMessages::addDeletedMessage(i->second); - } + processMessageDelete(i->second); if (!history->chatListMessageKnown()) { historiesToCheck.emplace(history); @@ -2616,12 +2601,7 @@ void Session::processNonChannelMessagesDeleted(const QVector &data) { if (const auto item = nonChannelMessage(messageId.v)) { const auto history = item->history(); - if (!NeedSaveMessage(item)) { - item->destroy(); - } else { - item->setDeleted(); - AyuMessages::addDeletedMessage(item); - } + processMessageDelete(item); if (!history->chatListMessageKnown()) { historiesToCheck.emplace(history); diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 8e0eafac3..f71327eeb 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -3093,7 +3093,7 @@ bool HistoryItem::isDeleted() const { void HistoryItem::setAyuHint(const QString &hint) { try { - if (isService()) { + if (isService() && !_text.empty()) { const auto data = Get(); const auto postfix = QString(" (%1)").arg(hint); if (!_text.text.endsWith(postfix)) { // fix stacking for TTL messages diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 785a00872..dd35aa65c 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -420,6 +420,7 @@ public: void setPostAuthor(const QString &author); void setDeleted(); bool isDeleted() const; + void applyTTL(TimeId destroyAt); void setAyuHint(const QString &hint); void setRealId(MsgId newId); void incrementReplyToTopCounter(); @@ -652,8 +653,6 @@ private: void applyTTL(const MTPDmessage &data); void applyTTL(const MTPDmessageService &data); - void applyTTL(TimeId destroyAt); - // For an invoice button we replace the button text with a "Receipt" key. // It should show the receipt for the payed invoice. Still let mobile apps do that. void replaceBuyWithReceiptInMarkup();