fix: reduce CPU usage for TTL messages & refactor

This commit is contained in:
AlexeyZavar 2025-02-05 22:49:33 +03:00
parent 3243f57fd5
commit 4327fd4c58
7 changed files with 38 additions and 31 deletions

View file

@ -44,6 +44,8 @@ void map(not_null<HistoryItem*> 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();

View file

@ -702,7 +702,7 @@ void InnerWidget::addMessages(Direction direction, const std::vector<AyuMessageB
? message.fakeId // viewing edited history
: message.messageId; // viewing deleted messages
if (_messageIds.find(id) != _messageIds.end()) {
return;
continue;
}
auto count = 0;
const auto addOne = [&](

View file

@ -36,6 +36,7 @@
#include "ayu/ayu_settings.h"
#include "ayu/ayu_state.h"
#include "ayu/data/messages_storage.h"
Main::Session *getSession(ID userId) {
for (const auto &[index, account] : Core::App().domain().accounts()) {
@ -503,6 +504,28 @@ int getScheduleTime(int64 sumSize) {
return time;
}
bool isMessageSavable(const not_null<HistoryItem *> 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<HistoryItem*> 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()) {

View file

@ -48,6 +48,9 @@ QString getPeerDC(not_null<PeerData*> peer);
int getScheduleTime(int64 sumSize);
bool isMessageSavable(not_null<HistoryItem *> item);
void processMessageDelete(not_null<HistoryItem *> item);
void searchById(ID userId, Main::Session *session, bool retry, const Callback &callback);
void searchById(ID userId, Main::Session *session, const Callback &callback);

View file

@ -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<HistoryItem*> item) {
double(std::numeric_limits<int>::max())));
}
bool NeedSaveMessage(not_null<HistoryItem *> 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<Main::Session*> 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<MTPint> &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);

View file

@ -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<HistoryServiceData>();
const auto postfix = QString(" (%1)").arg(hint);
if (!_text.text.endsWith(postfix)) { // fix stacking for TTL messages

View file

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