From ffd42f043d12888b088990a2cbe6b273d2e5c303 Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Fri, 22 Sep 2023 10:31:55 +0300 Subject: [PATCH] fix: refactor read all fix: read mentions and reactions too --- .../ayu/ui/boxes/confirmation_box.cpp | 3 +- .../ayu/utils/telegram_helpers.cpp | 120 ++++++++++++++++++ .../SourceFiles/ayu/utils/telegram_helpers.h | 4 + .../SourceFiles/window/window_main_menu.cpp | 5 +- .../SourceFiles/window/window_peer_menu.cpp | 4 - .../SourceFiles/window/window_peer_menu.h | 2 - 6 files changed, 129 insertions(+), 9 deletions(-) diff --git a/Telegram/SourceFiles/ayu/ui/boxes/confirmation_box.cpp b/Telegram/SourceFiles/ayu/ui/boxes/confirmation_box.cpp index e8208b8a3a..db9f87b05a 100644 --- a/Telegram/SourceFiles/ayu/ui/boxes/confirmation_box.cpp +++ b/Telegram/SourceFiles/ayu/ui/boxes/confirmation_box.cpp @@ -8,6 +8,7 @@ #include "confirmation_box.h" #include "lang_auto.h" #include "ayu/ayu_settings.h" +#include "ayu/utils/telegram_helpers.h" #include "data/data_session.h" #include "main/main_session.h" #include "styles/style_layers.h" @@ -60,7 +61,7 @@ void ConfirmationBox::ReadAllPeers() settings->set_sendReadMessages(true); auto chats = _controller->session().data().chatsList(); - Window::MarkAsReadChatListHack(chats); + MarkAsReadChatList(chats); settings->set_sendReadMessages(prev); } diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp index 7a36d561ed..a3092a9f21 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp @@ -11,6 +11,10 @@ #include "api/api_text_entities.h" +#include "apiwrap.h" +#include "data/data_forum.h" +#include "data/data_forum_topic.h" +#include "data/data_histories.h" #include "data/data_peer_id.h" #include "ayu/sync/models.h" @@ -18,6 +22,7 @@ #include "data/data_session.h" #include "history/history.h" #include "history/history_item.h" +#include "history/history_unread_things.h" Main::Session *getSession(ID userId) @@ -104,3 +109,118 @@ std::pair serializeTextWithEntities(not_null(buff.data()), buff.size())); } + +void MarkAsReadChatList(not_null list) +{ + auto mark = std::vector>(); + for (const auto &row : list->indexed()->all()) { + if (const auto history = row->history()) { + mark.push_back(history); + } + } + ranges::for_each(mark, MarkAsReadThread); +} + +void MarkAsReadThread(not_null thread) +{ + const auto readHistory = [&](not_null history) + { + history->owner().histories().readInbox(history); + }; + const auto readMentions = [=]( + base::weak_ptr weakThread, + auto resend) -> void + { + const auto thread = weakThread.get(); + if (!thread) { + return; + } + const auto peer = thread->peer(); + const auto topic = thread->asTopic(); + const auto rootId = topic ? topic->rootId() : 0; + using Flag = MTPmessages_ReadMentions::Flag; + peer->session().api().request(MTPmessages_ReadMentions( + MTP_flags(rootId ? Flag::f_top_msg_id : Flag()), + peer->input, + MTP_int(rootId) + )).done([=](const MTPmessages_AffectedHistory &result) + { + const auto offset = peer->session().api().applyAffectedHistory( + peer, + result); + if (offset > 0) { + resend(weakThread, resend); + } + else { + peer->owner().history(peer)->clearUnreadMentionsFor(rootId); + } + }).send(); + }; + const auto sendReadMentions = [=]( + not_null thread) + { + readMentions(base::make_weak(thread), readMentions); + }; + + const auto readReactions = [=]( + base::weak_ptr weakThread, + auto resend) -> void + { + const auto thread = weakThread.get(); + if (!thread) { + return; + } + const auto topic = thread->asTopic(); + const auto peer = thread->peer(); + const auto rootId = topic ? topic->rootId() : 0; + using Flag = MTPmessages_ReadReactions::Flag; + peer->session().api().request(MTPmessages_ReadReactions( + MTP_flags(rootId ? Flag::f_top_msg_id : Flag(0)), + peer->input, + MTP_int(rootId) + )).done([=](const MTPmessages_AffectedHistory &result) + { + const auto offset = peer->session().api().applyAffectedHistory( + peer, + result); + if (offset > 0) { + resend(weakThread, resend); + } + else { + peer->owner().history(peer)->clearUnreadReactionsFor(rootId); + } + }).send(); + }; + const auto sendReadReactions = [=]( + not_null thread) + { + readReactions(base::make_weak(thread), readReactions); + }; + + if (thread->chatListBadgesState().unread) { + if (const auto forum = thread->asForum()) { + forum->enumerateTopics([]( + not_null topic) + { + MarkAsReadThread(topic); + }); + } + else if (const auto history = thread->asHistory()) { + readHistory(history); + if (const auto migrated = history->migrateSibling()) { + readHistory(migrated); + } + } + else if (const auto topic = thread->asTopic()) { + topic->readTillEnd(); + } + } + + if (thread->unreadMentions().has()) { + sendReadMentions(thread); + } + + if (thread->unreadReactions().has()) { + sendReadReactions(thread); + } +} \ No newline at end of file diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h index b4daf04319..13ef0e4121 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h @@ -12,6 +12,7 @@ #include "main/main_account.h" #include "main/main_domain.h" #include "main/main_session.h" +#include "dialogs/dialogs_main_list.h" Main::Session *getSession(ID userId); bool accountExists(ID userId); @@ -19,3 +20,6 @@ void dispatchToMainThread(std::function callback); not_null getHistoryFromDialogId(ID dialogId, Main::Session *session); ID getDialogIdFromPeer(not_null peer); std::pair serializeTextWithEntities(not_null item); + +void MarkAsReadChatList(not_null list); +void MarkAsReadThread(not_null thread); diff --git a/Telegram/SourceFiles/window/window_main_menu.cpp b/Telegram/SourceFiles/window/window_main_menu.cpp index b7077ae1c7..4306adfc99 100644 --- a/Telegram/SourceFiles/window/window_main_menu.cpp +++ b/Telegram/SourceFiles/window/window_main_menu.cpp @@ -84,9 +84,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL // AyuGram includes #include "ayu/ayu_settings.h" +#include "ayu/utils/telegram_helpers.h" #include "ayu/ui/settings/settings_ayu.h" -#include "ayu/features/streamer_mode/streamer_mode.h" #include "ayu/ui/boxes/confirmation_box.h" +#include "ayu/features/streamer_mode/streamer_mode.h" #include "styles/style_ayu_icons.h" @@ -822,7 +823,7 @@ void MainMenu::setupMenu() { settings->set_sendReadMessages(false); auto chats = controller->session().data().chatsList(); - MarkAsReadChatListHack(chats); + MarkAsReadChatList(chats); settings->set_sendReadMessages(prev); }); diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 2bafa2bccf..92025ae40b 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -1287,10 +1287,6 @@ void Filler::fillArchiveActions() { } // namespace -void MarkAsReadChatListHack(not_null list) { - MarkAsReadChatList(list); -} - void PeerMenuExportChat(not_null peer) { Core::App().exportManager().start(peer); } diff --git a/Telegram/SourceFiles/window/window_peer_menu.h b/Telegram/SourceFiles/window/window_peer_menu.h index 34dc02a899..d5808fbf55 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.h +++ b/Telegram/SourceFiles/window/window_peer_menu.h @@ -182,6 +182,4 @@ void MarkAsReadThread(not_null thread); void AddSeparatorAndShiftUp(const PeerMenuCallback &addAction); -void MarkAsReadChatListHack(not_null list); - } // namespace Window