Respect peer_types in the switch inline button.

Fixes #26274.
This commit is contained in:
John Preston 2023-05-24 19:36:58 +04:00
parent 8a99de16f6
commit 7877463468
7 changed files with 70 additions and 10 deletions

View File

@ -279,11 +279,11 @@ void SendBotCallbackDataWithPassword(
bool SwitchInlineBotButtonReceived( bool SwitchInlineBotButtonReceived(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
const QString &query, const QByteArray &queryWithPeerTypes,
UserData *samePeerBot, UserData *samePeerBot,
MsgId samePeerReplyTo) { MsgId samePeerReplyTo) {
return controller->content()->notify_switchInlineBotButtonReceived( return controller->content()->notify_switchInlineBotButtonReceived(
query, QString::fromUtf8(queryWithPeerTypes),
samePeerBot, samePeerBot,
samePeerReplyTo); samePeerReplyTo);
} }
@ -441,14 +441,14 @@ void ActivateBotCommand(ClickHandlerContext context, int row, int column) {
if (samePeer) { if (samePeer) {
SwitchInlineBotButtonReceived( SwitchInlineBotButtonReceived(
controller, controller,
QString::fromUtf8(button->data), button->data,
bot, bot,
item->id); item->id);
return true; return true;
} else if (bot->isBot() && bot->botInfo->inlineReturnTo.key) { } else if (bot->isBot() && bot->botInfo->inlineReturnTo.key) {
const auto switched = SwitchInlineBotButtonReceived( const auto switched = SwitchInlineBotButtonReceived(
controller, controller,
QString::fromUtf8(button->data)); button->data);
if (switched) { if (switched) {
return true; return true;
} }
@ -466,7 +466,9 @@ void ActivateBotCommand(ClickHandlerContext context, int row, int column) {
Window::ShowChooseRecipientBox( Window::ShowChooseRecipientBox(
controller, controller,
chosen, chosen,
tr::lng_inline_switch_choose()); tr::lng_inline_switch_choose(),
nullptr,
button->peerTypes);
} }
} }
} break; } break;

View File

@ -30,7 +30,7 @@ void SendBotCallbackDataWithPassword(
bool SwitchInlineBotButtonReceived( bool SwitchInlineBotButtonReceived(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
const QString &query, const QByteArray &queryWithPeerTypes,
UserData *samePeerBot = nullptr, UserData *samePeerBot = nullptr,
MsgId samePeerReplyTo = 0); MsgId samePeerReplyTo = 0);

View File

@ -117,7 +117,7 @@ void CheckForSwitchInlineButton(not_null<HistoryItem*> item) {
if (!windows.empty()) { if (!windows.empty()) {
Api::SwitchInlineBotButtonReceived( Api::SwitchInlineBotButtonReceived(
windows.front(), windows.front(),
QString::fromUtf8(button.data)); button.data);
} }
return; return;
} }

View File

@ -10,9 +10,32 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h" #include "data/data_session.h"
#include "history/history_item.h" #include "history/history_item.h"
#include "history/history_item_components.h" #include "history/history_item_components.h"
#include "inline_bots/bot_attach_web_view.h"
namespace { namespace {
[[nodiscard]] InlineBots::PeerTypes PeerTypesFromMTP(
const MTPvector<MTPInlineQueryPeerType> &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( [[nodiscard]] RequestPeerQuery RequestPeerQueryFromTL(
const MTPRequestPeerType &query) { const MTPRequestPeerType &query) {
using Type = RequestPeerQuery::Type; using Type = RequestPeerQuery::Type;
@ -134,6 +157,9 @@ void HistoryMessageMarkupData::fillRows(
// Optimization flag. // Optimization flag.
// Fast check on all new messages if there is a switch button to auto-click it. // Fast check on all new messages if there is a switch button to auto-click it.
flags |= ReplyMarkupFlag::HasSwitchInlineButton; flags |= ReplyMarkupFlag::HasSwitchInlineButton;
if (const auto types = data.vpeer_types()) {
row.back().peerTypes = PeerTypesFromMTP(*types);
}
} }
}, [&](const MTPDkeyboardButtonGame &data) { }, [&](const MTPDkeyboardButtonGame &data) {
row.emplace_back(Type::Game, qs(data.vtext())); row.emplace_back(Type::Game, qs(data.vtext()));

View File

@ -14,6 +14,11 @@ namespace Data {
class Session; class Session;
} // namespace Data } // namespace Data
namespace InlineBots {
enum class PeerType : uint8;
using PeerTypes = base::flags<PeerType>;
} // namespace InlineBots
enum class ReplyMarkupFlag : uint32 { enum class ReplyMarkupFlag : uint32 {
None = (1U << 0), None = (1U << 0),
ForceReply = (1U << 1), ForceReply = (1U << 1),
@ -89,6 +94,7 @@ struct HistoryMessageMarkupButton {
QString text, forwardText; QString text, forwardText;
QByteArray data; QByteArray data;
int64 buttonId = 0; int64 buttonId = 0;
InlineBots::PeerTypes peerTypes = 0;
mutable mtpRequestId requestId = 0; mutable mtpRequestId requestId = 0;
}; };

View File

@ -32,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/peers/edit_forum_topic_box.h" #include "boxes/peers/edit_forum_topic_box.h"
#include "boxes/peers/edit_contact_box.h" #include "boxes/peers/edit_contact_box.h"
#include "calls/calls_instance.h" #include "calls/calls_instance.h"
#include "inline_bots/bot_attach_web_view.h" // InlineBots::PeerType.
#include "ui/boxes/report_box.h" #include "ui/boxes/report_box.h"
#include "ui/toast/toast.h" #include "ui/toast/toast.h"
#include "ui/text/format_values.h" #include "ui/text/format_values.h"
@ -1602,7 +1603,8 @@ QPointer<Ui::BoxContent> ShowChooseRecipientBox(
not_null<Window::SessionNavigation*> navigation, not_null<Window::SessionNavigation*> navigation,
FnMut<bool(not_null<Data::Thread*>)> &&chosen, FnMut<bool(not_null<Data::Thread*>)> &&chosen,
rpl::producer<QString> titleOverride, rpl::producer<QString> titleOverride,
FnMut<void()> &&successCallback) { FnMut<void()> &&successCallback,
InlineBots::PeerTypes typesRestriction) {
const auto weak = std::make_shared<QPointer<Ui::BoxContent>>(); const auto weak = std::make_shared<QPointer<Ui::BoxContent>>();
auto callback = [ auto callback = [
chosen = std::move(chosen), chosen = std::move(chosen),
@ -1618,6 +1620,23 @@ QPointer<Ui::BoxContent> ShowChooseRecipientBox(
success(); success();
} }
}; };
auto filter = typesRestriction
? [=](not_null<Data::Thread*> 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<bool(not_null<Data::Thread*>)>();
auto initBox = [=](not_null<PeerListBox*> box) { auto initBox = [=](not_null<PeerListBox*> box) {
box->addButton(tr::lng_cancel(), [box] { box->addButton(tr::lng_cancel(), [box] {
box->closeBox(); box->closeBox();
@ -1629,7 +1648,8 @@ QPointer<Ui::BoxContent> ShowChooseRecipientBox(
*weak = navigation->parentController()->show(Box<PeerListBox>( *weak = navigation->parentController()->show(Box<PeerListBox>(
std::make_unique<ChooseRecipientBoxController>( std::make_unique<ChooseRecipientBoxController>(
&navigation->session(), &navigation->session(),
std::move(callback)), std::move(callback),
std::move(filter)),
std::move(initBox))); std::move(initBox)));
return weak->data(); return weak->data();
} }

View File

@ -38,6 +38,11 @@ namespace ChatHelpers {
class Show; class Show;
} // namespace ChatHelpers } // namespace ChatHelpers
namespace InlineBots {
enum class PeerType : uint8;
using PeerTypes = base::flags<PeerType>;
} // namespace InlineBots
namespace Window { namespace Window {
class Controller; class Controller;
@ -122,7 +127,8 @@ QPointer<Ui::BoxContent> ShowChooseRecipientBox(
not_null<Window::SessionNavigation*> navigation, not_null<Window::SessionNavigation*> navigation,
FnMut<bool(not_null<Data::Thread*>)> &&chosen, FnMut<bool(not_null<Data::Thread*>)> &&chosen,
rpl::producer<QString> titleOverride = nullptr, rpl::producer<QString> titleOverride = nullptr,
FnMut<void()> &&successCallback = nullptr); FnMut<void()> &&successCallback = nullptr,
InlineBots::PeerTypes typesRestriction = 0);
QPointer<Ui::BoxContent> ShowForwardMessagesBox( QPointer<Ui::BoxContent> ShowForwardMessagesBox(
std::shared_ptr<ChatHelpers::Show> show, std::shared_ptr<ChatHelpers::Show> show,
Data::ForwardDraft &&draft, Data::ForwardDraft &&draft,