From 7877463468c6f44fa3b1688960950cd4154bf2b4 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 24 May 2023 19:36:58 +0400 Subject: [PATCH] Respect peer_types in the switch inline button. Fixes #26274. --- Telegram/SourceFiles/api/api_bot.cpp | 12 +++++---- Telegram/SourceFiles/api/api_bot.h | 2 +- Telegram/SourceFiles/data/data_session.cpp | 2 +- .../history/history_item_reply_markup.cpp | 26 +++++++++++++++++++ .../history/history_item_reply_markup.h | 6 +++++ .../SourceFiles/window/window_peer_menu.cpp | 24 +++++++++++++++-- .../SourceFiles/window/window_peer_menu.h | 8 +++++- 7 files changed, 70 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index 8fa7f884c..165402d7e 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -279,11 +279,11 @@ void SendBotCallbackDataWithPassword( bool SwitchInlineBotButtonReceived( not_null controller, - const QString &query, + const QByteArray &queryWithPeerTypes, UserData *samePeerBot, MsgId samePeerReplyTo) { return controller->content()->notify_switchInlineBotButtonReceived( - query, + QString::fromUtf8(queryWithPeerTypes), samePeerBot, samePeerReplyTo); } @@ -441,14 +441,14 @@ void ActivateBotCommand(ClickHandlerContext context, int row, int column) { if (samePeer) { SwitchInlineBotButtonReceived( controller, - QString::fromUtf8(button->data), + button->data, bot, item->id); return true; } else if (bot->isBot() && bot->botInfo->inlineReturnTo.key) { const auto switched = SwitchInlineBotButtonReceived( controller, - QString::fromUtf8(button->data)); + button->data); if (switched) { return true; } @@ -466,7 +466,9 @@ void ActivateBotCommand(ClickHandlerContext context, int row, int column) { Window::ShowChooseRecipientBox( controller, chosen, - tr::lng_inline_switch_choose()); + tr::lng_inline_switch_choose(), + nullptr, + button->peerTypes); } } } break; diff --git a/Telegram/SourceFiles/api/api_bot.h b/Telegram/SourceFiles/api/api_bot.h index 198ad926b..7e26dd103 100644 --- a/Telegram/SourceFiles/api/api_bot.h +++ b/Telegram/SourceFiles/api/api_bot.h @@ -30,7 +30,7 @@ void SendBotCallbackDataWithPassword( bool SwitchInlineBotButtonReceived( not_null controller, - const QString &query, + const QByteArray &queryWithPeerTypes, UserData *samePeerBot = nullptr, MsgId samePeerReplyTo = 0); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 59bf0886d..bb1fc659f 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -117,7 +117,7 @@ void CheckForSwitchInlineButton(not_null item) { if (!windows.empty()) { Api::SwitchInlineBotButtonReceived( windows.front(), - QString::fromUtf8(button.data)); + button.data); } return; } diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.cpp b/Telegram/SourceFiles/history/history_item_reply_markup.cpp index fecb3598e..c15381e92 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.cpp +++ b/Telegram/SourceFiles/history/history_item_reply_markup.cpp @@ -10,9 +10,32 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "history/history_item.h" #include "history/history_item_components.h" +#include "inline_bots/bot_attach_web_view.h" namespace { +[[nodiscard]] InlineBots::PeerTypes PeerTypesFromMTP( + const MTPvector &types) { + using namespace InlineBots; + auto result = PeerTypes(0); + for (const auto &type : types.v) { + result |= type.match([&](const MTPDinlineQueryPeerTypePM &data) { + return PeerType::User; + }, [&](const MTPDinlineQueryPeerTypeChat &data) { + return PeerType::Group; + }, [&](const MTPDinlineQueryPeerTypeMegagroup &data) { + return PeerType::Group; + }, [&](const MTPDinlineQueryPeerTypeBroadcast &data) { + return PeerType::Broadcast; + }, [&](const MTPDinlineQueryPeerTypeBotPM &data) { + return PeerType::Bot; + }, [&](const MTPDinlineQueryPeerTypeSameBotPM &data) { + return PeerType(); + }); + } + return result; +} + [[nodiscard]] RequestPeerQuery RequestPeerQueryFromTL( const MTPRequestPeerType &query) { using Type = RequestPeerQuery::Type; @@ -134,6 +157,9 @@ void HistoryMessageMarkupData::fillRows( // Optimization flag. // Fast check on all new messages if there is a switch button to auto-click it. flags |= ReplyMarkupFlag::HasSwitchInlineButton; + if (const auto types = data.vpeer_types()) { + row.back().peerTypes = PeerTypesFromMTP(*types); + } } }, [&](const MTPDkeyboardButtonGame &data) { row.emplace_back(Type::Game, qs(data.vtext())); diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.h b/Telegram/SourceFiles/history/history_item_reply_markup.h index 719711d53..828432113 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.h +++ b/Telegram/SourceFiles/history/history_item_reply_markup.h @@ -14,6 +14,11 @@ namespace Data { class Session; } // namespace Data +namespace InlineBots { +enum class PeerType : uint8; +using PeerTypes = base::flags; +} // namespace InlineBots + enum class ReplyMarkupFlag : uint32 { None = (1U << 0), ForceReply = (1U << 1), @@ -89,6 +94,7 @@ struct HistoryMessageMarkupButton { QString text, forwardText; QByteArray data; int64 buttonId = 0; + InlineBots::PeerTypes peerTypes = 0; mutable mtpRequestId requestId = 0; }; diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 77055b6bf..3921a21cb 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -32,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/edit_forum_topic_box.h" #include "boxes/peers/edit_contact_box.h" #include "calls/calls_instance.h" +#include "inline_bots/bot_attach_web_view.h" // InlineBots::PeerType. #include "ui/boxes/report_box.h" #include "ui/toast/toast.h" #include "ui/text/format_values.h" @@ -1602,7 +1603,8 @@ QPointer ShowChooseRecipientBox( not_null navigation, FnMut)> &&chosen, rpl::producer titleOverride, - FnMut &&successCallback) { + FnMut &&successCallback, + InlineBots::PeerTypes typesRestriction) { const auto weak = std::make_shared>(); auto callback = [ chosen = std::move(chosen), @@ -1618,6 +1620,23 @@ QPointer ShowChooseRecipientBox( success(); } }; + auto filter = typesRestriction + ? [=](not_null thread) -> bool { + using namespace InlineBots; + const auto peer = thread->peer(); + if (const auto user = peer->asUser()) { + if (user->isBot()) { + return (typesRestriction & PeerType::Bot); + } else { + return (typesRestriction & PeerType::User); + } + } else if (peer->isBroadcast()) { + return (typesRestriction & PeerType::Broadcast); + } else { + return (typesRestriction & PeerType::Group); + } + } + : Fn)>(); auto initBox = [=](not_null box) { box->addButton(tr::lng_cancel(), [box] { box->closeBox(); @@ -1629,7 +1648,8 @@ QPointer ShowChooseRecipientBox( *weak = navigation->parentController()->show(Box( std::make_unique( &navigation->session(), - std::move(callback)), + std::move(callback), + std::move(filter)), std::move(initBox))); return weak->data(); } diff --git a/Telegram/SourceFiles/window/window_peer_menu.h b/Telegram/SourceFiles/window/window_peer_menu.h index 44e3bd90f..7fde1f407 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.h +++ b/Telegram/SourceFiles/window/window_peer_menu.h @@ -38,6 +38,11 @@ namespace ChatHelpers { class Show; } // namespace ChatHelpers +namespace InlineBots { +enum class PeerType : uint8; +using PeerTypes = base::flags; +} // namespace InlineBots + namespace Window { class Controller; @@ -122,7 +127,8 @@ QPointer ShowChooseRecipientBox( not_null navigation, FnMut)> &&chosen, rpl::producer titleOverride = nullptr, - FnMut &&successCallback = nullptr); + FnMut &&successCallback = nullptr, + InlineBots::PeerTypes typesRestriction = 0); QPointer ShowForwardMessagesBox( std::shared_ptr show, Data::ForwardDraft &&draft,