From 084967354b82384998182eafe4c48280380686a8 Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Fri, 1 Dec 2023 20:58:00 +0300 Subject: [PATCH] feat: rework `sendReadMessages` fix: send read packet manually fix: send read packet after sending media --- Telegram/SourceFiles/api/api_polls.cpp | 6 +-- Telegram/SourceFiles/ayu/ayu_state.cpp | 13 ------- Telegram/SourceFiles/ayu/ayu_state.h | 4 -- .../ayu/ui/context_menu/context_menu.cpp | 5 +-- .../ayu/utils/telegram_helpers.cpp | 38 ++++++++++++++++++- .../SourceFiles/ayu/utils/telegram_helpers.h | 2 + Telegram/SourceFiles/data/data_histories.cpp | 25 ++++++------ .../data/data_message_reactions.cpp | 6 +-- .../SourceFiles/history/history_widget.cpp | 14 +++++-- 9 files changed, 69 insertions(+), 44 deletions(-) diff --git a/Telegram/SourceFiles/api/api_polls.cpp b/Telegram/SourceFiles/api/api_polls.cpp index 9f1aef8d0..b4464019d 100644 --- a/Telegram/SourceFiles/api/api_polls.cpp +++ b/Telegram/SourceFiles/api/api_polls.cpp @@ -21,7 +21,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" // AyuGram includes -#include "ayu/ayu_state.h" +#include "ayu/ayu_settings.h" +#include "ayu/utils/telegram_helpers.h" namespace Api { @@ -156,8 +157,7 @@ void Polls::sendVotes( const auto settings = &AyuSettings::getInstance(); if (!settings->sendReadMessages && settings->markReadAfterPoll && item) { - AyuState::setAllowSendReadPacket(true); - item->history()->session().data().histories().readInboxOnNewMessage(item); + readHistory(item); } }).fail([=] { _pollVotesRequestIds.erase(itemId); diff --git a/Telegram/SourceFiles/ayu/ayu_state.cpp b/Telegram/SourceFiles/ayu/ayu_state.cpp index 302dce2f9..c8e84144a 100644 --- a/Telegram/SourceFiles/ayu/ayu_state.cpp +++ b/Telegram/SourceFiles/ayu/ayu_state.cpp @@ -8,17 +8,4 @@ namespace AyuState { - -void setAllowSendReadPacket(bool val, int resetAfter) -{ - allowSendReadPacket.val = val; - allowSendReadPacket.resetAfter = resetAfter; -} - -bool getAllowSendPacket() -{ - auto settings = &AyuSettings::getInstance(); - return settings->sendReadMessages || processVariable(allowSendReadPacket); -} - } diff --git a/Telegram/SourceFiles/ayu/ayu_state.h b/Telegram/SourceFiles/ayu/ayu_state.h index 44a6a0e71..dc890b91e 100644 --- a/Telegram/SourceFiles/ayu/ayu_state.h +++ b/Telegram/SourceFiles/ayu/ayu_state.h @@ -41,8 +41,4 @@ bool processVariable(AyuStateVariable &variable) } -void setAllowSendReadPacket(bool val, int resetAfter = 1); - -bool getAllowSendPacket(); - } diff --git a/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp b/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp index 9cbca8d98..0da62ac92 100644 --- a/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp +++ b/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp @@ -16,6 +16,7 @@ #include "ui/widgets/popup_menu.h" #include "ayu/ui/sections/edited/edited_log_section.h" +#include "ayu/utils/telegram_helpers.h" namespace AyuUi @@ -53,11 +54,9 @@ void AddReadUntilAction(not_null menu, HistoryItem *item) return; } - const auto history = item->history(); menu->addAction(tr::ayu_ReadUntilMenuText(tr::now), [=]() { - AyuState::setAllowSendReadPacket(true); - history->session().data().histories().readInboxOnNewMessage(item); + readHistory(item); }, &st::menuIconShowInChat); } diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp index 9e0d1838f..40082c70b 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp @@ -10,6 +10,7 @@ #include "api/api_text_entities.h" +#include "data/data_channel.h" #include "lang_auto.h" #include "apiwrap.h" #include "data/data_forum.h" @@ -23,6 +24,7 @@ #include "history/history.h" #include "history/history_item.h" #include "history/history_unread_things.h" +#include "data/data_histories.h" // https://github.com/AyuGram/AyuGram4AX/blob/rewrite/TMessagesProj/src/main/java/com/radolyn/ayugram/AyuConstants.java std::unordered_set ayugram_channels = { @@ -32,6 +34,7 @@ std::unordered_set ayugram_channels = { 1947958814, // @ayugramfun 1815864846, // @ayugramfcm }; + std::unordered_set ayugram_devs = { 139303278, // @alexeyzavar 778327202, // @sharapagorg @@ -53,6 +56,7 @@ std::unordered_set extera_channels = { 1516526055, // @moexci 1622008530, // @moe_chat }; + std::unordered_set extera_devs = { 963080346, 1282540315, @@ -153,7 +157,8 @@ std::pair serializeTextWithEntities(not_null(buff.data()), buff.size())); } -ID getBareID(not_null peer) { +ID getBareID(not_null peer) +{ return peerIsUser(peer->id) ? peerToUser(peer->id).bare : peerIsChat(peer->id) @@ -288,7 +293,36 @@ void MarkAsReadThread(not_null thread) } } -QString formatTTL(int time) { +void readHistory(not_null message) +{ + const auto history = message->history(); + const auto tillId = message->id; + + history->session().data().histories() + .sendRequest(history, Data::Histories::RequestType::ReadInbox, [=](Fn finish) + { + if (const auto channel = history->peer->asChannel()) { + return history->session().api().request(MTPchannels_ReadHistory( + channel->inputChannel, + MTP_int(tillId) + )).send(); + } + else { + return history->session().api().request(MTPmessages_ReadHistory( + history->peer->input, + MTP_int(tillId) + )).done([=](const MTPmessages_AffectedMessages &result) + { + history->session().api().applyAffectedMessages(history->peer, result); + }).fail([=] + { + }).send(); + } + }); +} + +QString formatTTL(int time) +{ if (time == 0x7FFFFFFF) { return QString("👀 %1").arg(tr::ayu_OneViewTTL(tr::now)); } diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h index 167394ed2..e0088a7ac 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h @@ -29,4 +29,6 @@ bool isExteraRelated(ID peerId); void MarkAsReadChatList(not_null list); void MarkAsReadThread(not_null thread); +void readHistory(not_null message); + QString formatTTL(int time); diff --git a/Telegram/SourceFiles/data/data_histories.cpp b/Telegram/SourceFiles/data/data_histories.cpp index 3f3c4aa2f..a60fdee48 100644 --- a/Telegram/SourceFiles/data/data_histories.cpp +++ b/Telegram/SourceFiles/data/data_histories.cpp @@ -249,16 +249,6 @@ void Histories::readInboxTill( AyuSync::getInstance().syncRead(history, tillId); - // AyuGram sendReadMessages - const auto settings = &AyuSettings::getInstance(); - auto allow = settings->sendReadMessages; - auto reallyAllow = AyuState::getAllowSendPacket(); // will return true if `allow` - if (!reallyAllow) - { - DEBUG_LOG(("[AyuGram] Don't read messages")); - return; - } - const auto needsRequest = history->readInboxTillNeedsRequest(tillId); if (!needsRequest && !force) { DEBUG_LOG(("Reading: readInboxTill finish 1.")); @@ -280,9 +270,8 @@ void Histories::readInboxTill( sendPendingReadInbox(history); } return; - } else if (!needsRequest && (allow != reallyAllow && !force) - && (!maybeState || !maybeState->willReadTill)) - { + } else if (!needsRequest + && (!maybeState || !maybeState->willReadTill)) { return; } const auto stillUnread = history->countStillUnreadLocal(tillId); @@ -587,6 +576,16 @@ void Histories::sendPendingReadInbox(not_null history) { void Histories::sendReadRequests() { DEBUG_LOG(("Reading: send requests with count %1.").arg(_states.size())); + + // AyuGram sendReadMessages + const auto settings = &AyuSettings::getInstance(); + if (!settings->sendReadMessages) + { + DEBUG_LOG(("[AyuGram] Don't read messages")); + _states.clear(); + return; + } + if (_states.empty()) { return; } diff --git a/Telegram/SourceFiles/data/data_message_reactions.cpp b/Telegram/SourceFiles/data/data_message_reactions.cpp index 62fe31f56..9ff9205f7 100644 --- a/Telegram/SourceFiles/data/data_message_reactions.cpp +++ b/Telegram/SourceFiles/data/data_message_reactions.cpp @@ -31,7 +31,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_chat.h" // AyuGram includes -#include "ayu/ayu_state.h" +#include "ayu/ayu_settings.h" +#include "ayu/utils/telegram_helpers.h" namespace Data { @@ -838,8 +839,7 @@ void Reactions::send(not_null item, bool addToRecent) { const auto settings = &AyuSettings::getInstance(); if (!settings->sendReadMessages && settings->markReadAfterReaction && item) { - AyuState::setAllowSendReadPacket(true); - item->history()->session().data().histories().readInboxOnNewMessage(item); + readHistory(item); } }).fail([=](const MTP::Error &error) { _sentRequests.remove(id); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index d1bc30507..99242b90f 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -170,7 +170,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL // AyuGram includes #include "ayu/ayu_settings.h" -#include "ayu/ayu_state.h" +#include "ayu/utils/telegram_helpers.h" namespace { @@ -1067,6 +1067,15 @@ void HistoryWidget::initTabbedSelector() { Data::InsertCustomEmoji(_field.data(), data.document); } } else { + const auto settings = &AyuSettings::getInstance(); + if (!settings->sendReadMessages && settings->markReadAfterSend) { + const auto lastMessage = history()->lastMessage(); + + if (lastMessage) { + readHistory(lastMessage); + } + } + controller()->sendingAnimation().appendSending( data.messageSendingFrom); const auto localId = data.messageSendingFrom.localId; @@ -3969,8 +3978,7 @@ void HistoryWidget::send(Api::SendOptions options) { auto lastMessage = _history->lastMessage(); if (!settings->sendReadMessages && settings->markReadAfterSend && lastMessage) { - AyuState::setAllowSendReadPacket(true); - _history->session().data().histories().readInboxOnNewMessage(lastMessage); + readHistory(lastMessage); } if (!_history) {