diff --git a/Telegram/Resources/tl/api.tl b/Telegram/Resources/tl/api.tl index a3c6a1e85..9d5dbca5c 100644 --- a/Telegram/Resources/tl/api.tl +++ b/Telegram/Resources/tl/api.tl @@ -1342,7 +1342,7 @@ attachMenuBotIconColor#4576f3f0 name:string color:int = AttachMenuBotIconColor; attachMenuBotIcon#b2a7386b flags:# name:string icon:Document colors:flags.0?Vector = AttachMenuBotIcon; -attachMenuBot#e93cb772 flags:# inactive:flags.0?true bot_id:long short_name:string icons:Vector = AttachMenuBot; +attachMenuBot#c8aa2cd2 flags:# inactive:flags.0?true bot_id:long short_name:string peer_types:Vector icons:Vector = AttachMenuBot; attachMenuBotsNotModified#f1d88a5c = AttachMenuBots; attachMenuBots#3c4301c0 hash:long bots:Vector users:Vector = AttachMenuBots; @@ -1370,6 +1370,12 @@ notificationSoundRingtone#ff6c8049 id:long = NotificationSound; account.savedRingtone#b7263f6d = account.SavedRingtone; account.savedRingtoneConverted#1f307eb7 document:Document = account.SavedRingtone; +attachMenuPeerTypeSameBotPM#7d6be90e = AttachMenuPeerType; +attachMenuPeerTypeBotPM#c32bfa1a = AttachMenuPeerType; +attachMenuPeerTypePM#f146d31f = AttachMenuPeerType; +attachMenuPeerTypeChat#509113f = AttachMenuPeerType; +attachMenuPeerTypeBroadcast#7bfbdefc = AttachMenuPeerType; + ---functions--- invokeAfterMsg#cb9f372d {X:Type} msg_id:long query:!X = X; @@ -1665,8 +1671,8 @@ messages.searchSentMedia#107e31a0 q:string filter:MessagesFilter limit:int = mes messages.getAttachMenuBots#16fcc2cb hash:long = AttachMenuBots; messages.getAttachMenuBot#77216192 bot:InputUser = AttachMenuBotsBot; messages.toggleBotInAttachMenu#1aee33af bot:InputUser enabled:Bool = Bool; -messages.requestWebView#fa04dff flags:# from_bot_menu:flags.4?true silent:flags.5?true peer:InputPeer bot:InputUser url:flags.1?string start_param:flags.3?string theme_params:flags.2?DataJSON reply_to_msg_id:flags.0?int = WebViewResult; -messages.prolongWebView#d22ad148 flags:# silent:flags.5?true peer:InputPeer bot:InputUser query_id:long reply_to_msg_id:flags.0?int = Bool; +messages.requestWebView#91b15831 flags:# from_bot_menu:flags.4?true silent:flags.5?true peer:InputPeer bot:InputUser url:flags.1?string start_param:flags.3?string theme_params:flags.2?DataJSON reply_to_msg_id:flags.0?int send_as:flags.13?InputPeer = WebViewResult; +messages.prolongWebView#ea5fbcce flags:# silent:flags.5?true peer:InputPeer bot:InputUser query_id:long reply_to_msg_id:flags.0?int send_as:flags.13?InputPeer = Bool; messages.requestSimpleWebView#6abb2f73 flags:# bot:InputUser url:string theme_params:flags.0?DataJSON = SimpleWebViewResult; messages.sendWebViewResultMessage#a4314f5 bot_query_id:string result:InputBotInlineResult = WebViewMessageSent; messages.sendWebViewData#dc0242c8 bot:InputUser random_id:long button_text:string data:string = Updates; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 1c4750fd7..b711f1ee9 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -484,36 +484,7 @@ HistoryWidget::HistoryWidget( session().attachWebView().requestBots(); session().attachWebView().attachBotsUpdates( ) | rpl::start_with_next([=] { - if (session().attachWebView().attachBots().empty()) { - _attachBotsMenu = nullptr; - return; - } else if (!_attachBotsMenu) { - const auto forceShown = [=](bool shown) { - if (shown) { - _attachBotsMenu->setAutoHiding(false); - } else { - _attachBotsMenu->hideAnimated(); - _attachBotsMenu->setAutoHiding(true); - } - }; - const auto attach = [=](bool compress) { - chooseAttach(compress); - }; - _attachBotsMenu = InlineBots::MakeAttachBotsMenu( - this, - controller, - forceShown, - attach); - _attachBotsMenu->setOrigin( - Ui::PanelAnimation::Origin::BottomLeft); - if (_history && _history->peer->isUser()) { - _attachToggle->installEventFilter(_attachBotsMenu.get()); - } - _attachBotsMenu->heightValue( - ) | rpl::start_with_next([=] { - moveFieldControls(); - }, _attachBotsMenu->lifetime()); - } + refreshAttachBotsMenu(); }, lifetime()); _botKeyboardShow->addClickHandler([=] { toggleKeyboard(); }); @@ -2419,6 +2390,28 @@ void HistoryWidget::setHistory(History *history) { _migrated = _history ? _history->migrateFrom() : nullptr; registerDraftSource(); } + refreshAttachBotsMenu(); +} + +void HistoryWidget::refreshAttachBotsMenu() { + _attachBotsMenu = nullptr; + if (!_history) { + return; + } + _attachBotsMenu = InlineBots::MakeAttachBotsMenu( + this, + _history->peer, + [=](bool compress) { chooseAttach(compress); }); + if (!_attachBotsMenu) { + return; + } + _attachBotsMenu->setOrigin( + Ui::PanelAnimation::Origin::BottomLeft); + _attachToggle->installEventFilter(_attachBotsMenu.get()); + _attachBotsMenu->heightValue( + ) | rpl::start_with_next([=] { + moveFieldControls(); + }, _attachBotsMenu->lifetime()); } void HistoryWidget::unregisterDraftSources() { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 1f97fe943..db1085ff0 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -631,6 +631,7 @@ private: void refreshScheduledToggle(); void setupSendAsToggle(); void refreshSendAsToggle(); + void refreshAttachBotsMenu(); bool kbWasHidden() const; diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index 24349919a..3a42643e3 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -71,6 +71,25 @@ struct ParsedBot { return nullptr; } +[[nodiscard]] PeerTypes ResolvePeerTypes( + const QVector &types) { + auto result = PeerTypes(); + for (const auto &type : types) { + result |= type.match([&](const MTPDattachMenuPeerTypeSameBotPM &) { + return PeerType::SameBot; + }, [&](const MTPDattachMenuPeerTypeBotPM &) { + return PeerType::Bot; + }, [&](const MTPDattachMenuPeerTypePM &) { + return PeerType::User; + }, [&](const MTPDattachMenuPeerTypeChat &) { + return PeerType::Group; + }, [&](const MTPDattachMenuPeerTypeBroadcast &) { + return PeerType::Broadcast; + }); + } + return result; +} + [[nodiscard]] std::optional ParseAttachBot( not_null session, const MTPAttachMenuBot &bot) { @@ -84,6 +103,7 @@ struct ParsedBot { .user = user, .icon = ResolveIcon(session, data), .name = qs(data.vshort_name()), + .types = ResolvePeerTypes(data.vpeer_types().v), .inactive = data.is_inactive(), } : std::optional(); }); @@ -295,6 +315,22 @@ void BotAction::handleKeyPress(not_null e) { } // namespace +bool PeerMatchesTypes( + not_null peer, + not_null bot, + PeerTypes types) { + if (const auto user = peer->asUser()) { + return (user == bot) + ? (types & PeerType::SameBot) + : user->isBot() + ? (types & PeerType::Bot) + : (types & PeerType::User); + } else if (peer->isBroadcast()) { + return (types & PeerType::Broadcast); + } + return (types & PeerType::Group); +} + AttachWebView::AttachWebView(not_null session) : _session(session) { } @@ -368,7 +404,8 @@ void AttachWebView::request(const WebViewButton &button) { MTP_bytes(button.url), MTP_string(_startCommand), MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)), - MTPint() // reply_to_msg_id + MTPint(), // reply_to_msg_id + MTPInputPeer() // send_as )).done([=](const MTPWebViewResult &result) { _requestId = 0; result.match([&](const MTPDwebViewResultUrl &data) { @@ -589,9 +626,10 @@ void AttachWebView::requestMenu( _bot->input, _bot->inputUser, MTP_string(url), - MTPstring(), + MTPstring(), // url MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)), - MTPint() + MTPint(), // reply_to_msg_id + MTPInputPeer() // send_as )).done([=](const MTPWebViewResult &result) { _requestId = 0; result.match([&](const MTPDwebViewResultUrl &data) { @@ -713,7 +751,8 @@ void AttachWebView::started(uint64 queryId) { _peer->input, _bot->inputUser, MTP_long(queryId), - MTP_int(_replyToMsgId.bare) + MTP_int(_replyToMsgId.bare), + MTPInputPeer() // send_as )).done([=] { _prolongId = 0; }).send(); @@ -764,45 +803,42 @@ void AttachWebView::toggleInMenu( std::unique_ptr MakeAttachBotsMenu( not_null parent, - not_null controller, - Fn forceShown, + not_null peer, Fn attach) { auto result = std::make_unique( parent, st::dropdownMenuWithIcons); - const auto bots = &controller->session().attachWebView(); + const auto bots = &peer->session().attachWebView(); const auto raw = result.get(); - const auto refresh = [=] { - raw->clearActions(); - raw->addAction(tr::lng_attach_photo_or_video(tr::now), [=] { - attach(true); - }, &st::menuIconPhoto); - raw->addAction(tr::lng_attach_document(tr::now), [=] { - attach(false); - }, &st::menuIconFile); - for (const auto &bot : bots->attachBots()) { - const auto callback = [=] { - const auto active = controller->activeChatCurrent(); - if (const auto history = active.history()) { - bots->request(nullptr, history->peer, bot.user, {}); - } - }; - auto action = base::make_unique_q( - raw, - raw->menu()->st(), - bot, - callback); - action->forceShown( - ) | rpl::start_with_next([=](bool shown) { - forceShown(shown); - }, action->lifetime()); - raw->addAction(std::move(action)); + raw->addAction(tr::lng_attach_photo_or_video(tr::now), [=] { + attach(true); + }, &st::menuIconPhoto); + raw->addAction(tr::lng_attach_document(tr::now), [=] { + attach(false); + }, &st::menuIconFile); + for (const auto &bot : bots->attachBots()) { + if (!PeerMatchesTypes(peer, bot.user, bot.types)) { + continue; } - }; - refresh(); - bots->attachBotsUpdates( - ) | rpl::start_with_next(refresh, raw->lifetime()); - + auto action = base::make_unique_q( + raw, + raw->menu()->st(), + bot, + [=] { bots->request(nullptr, peer, bot.user, {}); }); + action->forceShown( + ) | rpl::start_with_next([=](bool shown) { + if (shown) { + raw->setAutoHiding(false); + } else { + raw->hideAnimated(); + raw->setAutoHiding(true); + } + }, action->lifetime()); + raw->addAction(std::move(action)); + } + if (raw->actions().size() < 3) { + return nullptr; + } return result; } diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h index 425d88505..7a2094aac 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/sender.h" #include "base/weak_ptr.h" +#include "base/flags.h" namespace Ui { class GenericBox; @@ -33,11 +34,26 @@ class DocumentMedia; namespace InlineBots { +enum class PeerType : uint8 { + SameBot = 0x01, + Bot = 0x02, + User = 0x04, + Group = 0x08, + Broadcast = 0x10, +}; +using PeerTypes = base::flags; + +[[nodiscard]] bool PeerMatchesTypes( + not_null peer, + not_null bot, + PeerTypes types); + struct AttachWebViewBot { not_null user; DocumentData *icon = nullptr; std::shared_ptr media; QString name; + PeerTypes types = 0; bool inactive = false; }; @@ -141,8 +157,7 @@ private: [[nodiscard]] std::unique_ptr MakeAttachBotsMenu( not_null parent, - not_null controller, - Fn forceShown, + not_null peer, Fn attach); } // namespace InlineBots