From bd8e7fdddde04699010ab2195df1e549de508138 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 2 Dec 2022 14:07:12 +0300 Subject: [PATCH] Moved out sending method of many messages to many peers to single place. --- Telegram/SourceFiles/boxes/share_box.cpp | 156 +++++++++++------------ Telegram/SourceFiles/boxes/share_box.h | 9 +- 2 files changed, 86 insertions(+), 79 deletions(-) diff --git a/Telegram/SourceFiles/boxes/share_box.cpp b/Telegram/SourceFiles/boxes/share_box.cpp index 40009beb29..95d9978a28 100644 --- a/Telegram/SourceFiles/boxes/share_box.cpp +++ b/Telegram/SourceFiles/boxes/share_box.cpp @@ -210,8 +210,6 @@ ShareBox::ShareBox(QWidget*, Descriptor &&descriptor) } void ShareBox::prepareCommentField() { - using namespace rpl::mappers; - _comment->hide(anim::type::instant); rpl::combine( @@ -476,7 +474,7 @@ void ShareBox::showMenu(not_null parent) { auto item = base::make_unique_q( _menu->menu(), st::popupMenuWithIcons.menu, - new QAction(QString(), _menu->menu()), + Ui::CreateChild(_menu->menu().get()), nullptr, nullptr); std::move( @@ -1343,75 +1341,23 @@ QString AppendShareGameScoreUrl( return url + shareComponent; } -void FastShareMessage( - not_null controller, - not_null item) { - struct ShareData { - ShareData(not_null peer, MessageIdsList &&ids) - : peer(peer) - , msgIds(std::move(ids)) { - } - not_null peer; - MessageIdsList msgIds; +ShareBox::SubmitCallback ShareBox::DefaultForwardCallback( + std::shared_ptr show, + not_null history, + MessageIdsList msgIds) { + struct State final { base::flat_set requests; }; - const auto show = std::make_shared(controller); - const auto history = item->history(); - const auto owner = &history->owner(); - const auto session = &history->session(); - const auto data = std::make_shared( - history->peer, - owner->itemOrItsGroup(item)); - const auto isGame = item->getMessageBot() - && item->media() - && (item->media()->game() != nullptr); - const auto canCopyLink = item->hasDirectLink() || isGame; - - const auto items = owner->idsToItems(data->msgIds); - const auto hasCaptions = ranges::any_of(items, [](auto item) { - return item->media() - && !item->originalText().text.isEmpty() - && item->media()->allowsEditCaption(); - }); - const auto hasOnlyForcedForwardedInfo = hasCaptions - ? false - : ranges::all_of(items, [](auto item) { - return item->media() && item->media()->forceForwardedInfo(); - }); - - auto copyCallback = [=, toastParent = show->toastParent()] { - const auto item = owner->message(data->msgIds[0]); - if (!item) { - return; - } - if (item->hasDirectLink()) { - using namespace HistoryView; - CopyPostLink(controller, item->fullId(), Context::History); - } else if (const auto bot = item->getMessageBot()) { - if (const auto media = item->media()) { - if (const auto game = media->game()) { - const auto link = session->createInternalLinkFull( - bot->username() + u"?game="_q + game->shortName); - - QGuiApplication::clipboard()->setText(link); - - Ui::Toast::Show( - toastParent, - tr::lng_share_game_link_copied(tr::now)); - } - } - } - }; - - auto submitCallback = [=]( + const auto state = std::make_shared(); + return [=]( std::vector> &&result, TextWithTags &&comment, Api::SendOptions options, Data::ForwardOptions forwardOptions) { - if (!data->requests.empty()) { + if (!state->requests.empty()) { return; // Share clicked already. } - auto items = history->owner().idsToItems(data->msgIds); + auto items = history->owner().idsToItems(msgIds); if (items.empty() || result.empty()) { return; } @@ -1451,20 +1397,20 @@ void FastShareMessage( | ((forwardOptions == Data::ForwardOptions::NoNamesAndCaptions) ? Flag::f_drop_media_captions : Flag(0)); - auto msgIds = QVector(); - msgIds.reserve(data->msgIds.size()); - for (const auto &fullId : data->msgIds) { - msgIds.push_back(MTP_int(fullId.msg)); + auto mtpMsgIds = QVector(); + mtpMsgIds.reserve(msgIds.size()); + for (const auto &fullId : msgIds) { + mtpMsgIds.push_back(MTP_int(fullId.msg)); } const auto generateRandom = [&] { - auto result = QVector(data->msgIds.size()); + auto result = QVector(msgIds.size()); for (auto &value : result) { value = base::RandomValue(); } return result; }; - auto &api = owner->session().api(); - auto &histories = owner->histories(); + auto &api = history->owner().session().api(); + auto &histories = history->owner().histories(); const auto requestType = Data::Histories::RequestType::Send; for (const auto thread : result) { if (!comment.text.isEmpty()) { @@ -1486,8 +1432,8 @@ void FastShareMessage( history->sendRequestId = api.request( MTPmessages_ForwardMessages( MTP_flags(sendFlags), - data->peer->input, - MTP_vector(msgIds), + history->peer->input, + MTP_vector(mtpMsgIds), MTP_vector(generateRandom()), peer->input, MTP_int(topicRootId), @@ -1495,8 +1441,8 @@ void FastShareMessage( MTP_inputPeerEmpty() // send_as )).done([=](const MTPUpdates &updates, mtpRequestId reqId) { history->session().api().applyUpdates(updates); - data->requests.remove(reqId); - if (data->requests.empty()) { + state->requests.remove(reqId); + if (state->requests.empty()) { if (show->valid()) { Ui::Toast::Show( show->toastParent(), @@ -1520,9 +1466,60 @@ void FastShareMessage( }).afterRequest(history->sendRequestId).send(); return history->sendRequestId; }); - data->requests.insert(history->sendRequestId); + state->requests.insert(history->sendRequestId); } }; +} + +void FastShareMessage( + not_null controller, + not_null item) { + const auto show = std::make_shared(controller); + const auto history = item->history(); + const auto owner = &history->owner(); + const auto session = &history->session(); + const auto msgIds = owner->itemOrItsGroup(item); + const auto isGame = item->getMessageBot() + && item->media() + && (item->media()->game() != nullptr); + const auto canCopyLink = item->hasDirectLink() || isGame; + + const auto items = owner->idsToItems(msgIds); + const auto hasCaptions = ranges::any_of(items, [](auto item) { + return item->media() + && !item->originalText().text.isEmpty() + && item->media()->allowsEditCaption(); + }); + const auto hasOnlyForcedForwardedInfo = hasCaptions + ? false + : ranges::all_of(items, [](auto item) { + return item->media() && item->media()->forceForwardedInfo(); + }); + + auto copyCallback = [=, toastParent = show->toastParent()] { + const auto item = owner->message(msgIds[0]); + if (!item) { + return; + } + if (item->hasDirectLink()) { + using namespace HistoryView; + CopyPostLink(controller, item->fullId(), Context::History); + } else if (const auto bot = item->getMessageBot()) { + if (const auto media = item->media()) { + if (const auto game = media->game()) { + const auto link = session->createInternalLinkFull( + bot->username() + u"?game="_q + game->shortName); + + QGuiApplication::clipboard()->setText(link); + + Ui::Toast::Show( + toastParent, + tr::lng_share_game_link_copied(tr::now)); + } + } + } + }; + auto filterCallback = [isGame](not_null thread) { return thread->canWrite() && (!isGame || !thread->peer()->isBroadcast()); @@ -1534,10 +1531,13 @@ void FastShareMessage( Box(ShareBox::Descriptor{ .session = session, .copyCallback = std::move(copyLinkCallback), - .submitCallback = std::move(submitCallback), + .submitCallback = ShareBox::DefaultForwardCallback( + show, + history, + msgIds), .filterCallback = std::move(filterCallback), .forwardOptions = { - .messagesCount = int(data->msgIds.size()), + .messagesCount = int(msgIds.size()), .show = !hasOnlyForcedForwardedInfo, .hasCaptions = hasCaptions, }, diff --git a/Telegram/SourceFiles/boxes/share_box.h b/Telegram/SourceFiles/boxes/share_box.h index 5babab6cb3..f61669efc2 100644 --- a/Telegram/SourceFiles/boxes/share_box.h +++ b/Telegram/SourceFiles/boxes/share_box.h @@ -15,6 +15,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/effects/round_checkbox.h" #include "mtproto/sender.h" +class History; + namespace style { struct MultiSelect; struct InputField; @@ -74,9 +76,14 @@ public: std::vector>&&, TextWithTags&&, Api::SendOptions, - Data::ForwardOptions option)>; + Data::ForwardOptions)>; using FilterCallback = Fn)>; + [[nodiscard]] static SubmitCallback DefaultForwardCallback( + std::shared_ptr show, + not_null history, + MessageIdsList msgIds); + struct Descriptor { not_null session; CopyCallback copyCallback;