From 07f7ae42e4c91eb0fbb66ec5d5297bb288f62170 Mon Sep 17 00:00:00 2001 From: AlexeyZavar Date: Thu, 2 Jul 2026 18:53:55 +0300 Subject: [PATCH] fix: keep delete own messages logic in one place & handle errors better --- .../ayu/ui/context_menu/context_menu.cpp | 43 +++++++++++++++---- .../SourceFiles/boxes/delete_messages_box.cpp | 14 ++---- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp b/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp index 8fceff22c4..55dd93330f 100644 --- a/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp +++ b/Telegram/SourceFiles/ayu/ui/context_menu/context_menu.cpp @@ -35,6 +35,7 @@ #include "history/history_item_components.h" #include "history/view/history_view_context_menu.h" #include "history/view/history_view_element.h" +#include "main/main_session.h" #include "main/session/send_as_peers.h" #include "styles/style_ayu_icons.h" #include "styles/style_layers.h" @@ -79,6 +80,13 @@ Fn ClearDeletedMessagesHandler(not_null cont void DeleteMyMessagesAfterConfirm(not_null peer) { const auto session = &peer->session(); + if (const auto channel = peer->asChannel()) { + if (channel->isMegagroup() && channel->canDeleteMessages()) { + session->api().deleteAllFromParticipant(channel, session->user()); + return; + } + } + auto collected = std::make_shared>(); const auto removeNext = std::make_shared>(); @@ -113,9 +121,30 @@ void DeleteMyMessagesAfterConfirm(not_null peer) { }; const auto fail = [=](const MTP::Error &error) { - DEBUG_LOG(("Delete batch failed: %1").arg(error.type())); - const auto delay = crl::time(1000); - base::call_delayed(delay, [=] { (*removeNext)(index); }); + const auto type = error.type(); + DEBUG_LOG(("Delete batch %1 failed: %2").arg(batch).arg(type)); + + if (type.startsWith(u"FLOOD_WAIT_"_q) + || type.startsWith(u"FLOOD_PREMIUM_WAIT_"_q)) { + const auto underscore = type.lastIndexOf('_'); + const auto seconds = (underscore >= 0) + ? type.mid(underscore + 1).toInt() + : 0; + const auto delay = crl::time(std::max(seconds, 1) * 1000); + base::call_delayed(delay, [=] { (*removeNext)(index); }); + return; + } + + if (type == u"MESSAGE_DELETE_FORBIDDEN"_q + || type == u"MSG_ID_INVALID"_q + || type == u"MESSAGE_ID_INVALID"_q) { + DEBUG_LOG(("Skipping batch %1 (%2 ids)").arg(batch).arg(ids.size())); + const auto delay = crl::time(500 + base::RandomValue() % 500); + base::call_delayed(delay, [=] { (*removeNext)(index + ids.size()); }); + return; + } + + DEBUG_LOG(("Stopping deletion, unrecoverable error: %1").arg(type)); }; if (const auto channel = peer->asChannel()) { @@ -439,16 +468,12 @@ void AddDeleteOwnMessagesAction(PeerData *peerData, if (topic) { return; } - const auto isGroup = peerData->isChat() || peerData->isMegagroup(); - if (!isGroup) { - return; - } if (const auto chat = peerData->asChat()) { - if (!chat->amIn() || chat->amCreator() || chat->hasAdminRights()) { + if (!chat->amIn()) { return; } } else if (const auto channel = peerData->asChannel()) { - if (!channel->isMegagroup() || !channel->amIn() || channel->amCreator() || channel->hasAdminRights()) { + if (!channel->isMegagroup() || !channel->amIn()) { return; } } else { diff --git a/Telegram/SourceFiles/boxes/delete_messages_box.cpp b/Telegram/SourceFiles/boxes/delete_messages_box.cpp index 29e94379d0..7e1331901a 100644 --- a/Telegram/SourceFiles/boxes/delete_messages_box.cpp +++ b/Telegram/SourceFiles/boxes/delete_messages_box.cpp @@ -42,22 +42,14 @@ DeleteMessagesBox::DeleteMessagesBox( bool suggestModerateActions) : _session(&item->history()->session()) , _ids(1, item->fullId()) { - const auto peer = item->history()->peer; - const auto channel = peer->asChannel(); if (suggestModerateActions) { _moderateBan = item->suggestBanReport(); _moderateDeleteAll = item->suggestDeleteAllReport(); - } else if (item->out()) { - const auto chat = peer->asChat(); - if ((chat && chat->canDeleteMessages()) || - (channel && !channel->isBroadcast() && channel->canDeleteMessages())) { - _moderateDeleteAll = true; + if (_moderateBan || _moderateDeleteAll) { + _moderateFrom = item->from(); + _moderateInChannel = item->history()->peer->asChannel(); } } - if ((_moderateBan || _moderateDeleteAll) && channel) { - _moderateFrom = item->from(); - _moderateInChannel = channel; - } } DeleteMessagesBox::DeleteMessagesBox(