diff --git a/Telegram/SourceFiles/data/data_peer_bot_command.h b/Telegram/SourceFiles/data/data_peer_bot_command.h index 6b87c1fd4..3ee5ebee3 100644 --- a/Telegram/SourceFiles/data/data_peer_bot_command.h +++ b/Telegram/SourceFiles/data/data_peer_bot_command.h @@ -13,13 +13,9 @@ struct BotCommand final { QString command; QString description; - inline bool operator==(const BotCommand &other) const { - return (command == other.command) - && (description == other.description); - } - inline bool operator!=(const BotCommand &other) const { - return !(*this == other); - } + friend inline bool operator==( + const BotCommand &, + const BotCommand &) = default; }; [[nodiscard]] BotCommand BotCommandFromTL(const MTPBotCommand &result); diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index 3f549edb1..0e7980378 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_document.h" #include "data/data_document_media.h" +#include "data/data_peer_bot_command.h" #include "data/data_session.h" #include "data/data_web_page.h" #include "main/main_app_config.h" @@ -1216,7 +1217,7 @@ void WebViewInstance::botHandleMenuButton( } break; case Button::RemoveFromMenu: - case Button::RemoveFromMainMenu: + case Button::RemoveFromMainMenu: { const auto &bots = _session->attachWebView().attachBots(); const auto attached = ranges::find( bots, @@ -1248,7 +1249,19 @@ void WebViewInstance::botHandleMenuButton( Ui::Text::WithEntities), done, })); - break; + } break; + case Button::ShareGame: { + const auto itemId = v::is(_source) + ? v::get(_source).messageId + : FullMsgId(); + if (!_panel || !itemId) { + return; + } else if (const auto item = _session->data().message(itemId)) { + FastShareMessage(uiShow(), item); + } else { + _panel->showToast({ tr::lng_message_not_found(tr::now) }); + } + } break; } } @@ -1383,16 +1396,57 @@ void WebViewInstance::botInvokeCustomMethod( }).send(); } -void WebViewInstance::botShareGameScore() { - const auto itemId = v::is(_source) - ? v::get(_source).messageId - : FullMsgId(); - if (!_panel || !itemId) { - return; - } else if (const auto item = _session->data().message(itemId)) { - FastShareMessage(uiShow(), item); - } else { - _panel->showToast({ tr::lng_message_not_found(tr::now) }); +void WebViewInstance::botOpenPrivacyPolicy() { + const auto bot = _bot; + const auto weak = _context.controller; + const auto command = u"privacy"_q; + const auto findCommand = [=] { + if (!bot->isBot()) { + return QString(); + } + for (const auto &data : bot->botInfo->commands) { + const auto isSame = !data.command.compare( + command, + Qt::CaseInsensitive); + if (isSame) { + return data.command; + } + } + return QString(); + }; + const auto makeOtherContext = [=](bool forceWindow) { + return QVariant::fromValue(ClickHandlerContext{ + .sessionWindow = (forceWindow + ? WindowForThread(weak, bot->owner().history(bot)) + : weak), + .peer = bot, + }); + }; + const auto sendCommand = [=] { + const auto original = findCommand(); + if (original.isEmpty()) { + return false; + } + BotCommandClickHandler('/' + original).onClick(ClickContext{ + Qt::LeftButton, + makeOtherContext(true) + }); + return true; + }; + const auto openUrl = [=](const QString &url) { + Core::App().iv().openWithIvPreferred( + &_bot->session(), + url, + makeOtherContext(false)); + }; + if (const auto info = _bot->botInfo.get()) { + if (!info->privacyPolicyUrl.isEmpty()) { + openUrl(info->privacyPolicyUrl); + return; + } + } + if (!sendCommand()) { + openUrl(tr::lng_profile_bot_privacy_url(tr::now)); } } diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h index 4a4f5ca08..e9622fd02 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h @@ -275,7 +275,7 @@ private: void botSharePhone(Fn callback) override; void botInvokeCustomMethod( Ui::BotWebView::CustomMethodRequest request) override; - void botShareGameScore() override; + void botOpenPrivacyPolicy() override; void botClose() override; const std::shared_ptr _parentShow; diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp index 7b57464f7..6a083f41e 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp @@ -538,12 +538,15 @@ bool Panel::showWebview( }, &st::menuIconRestore); if (_menuButtons & MenuButton::ShareGame) { callback(tr::lng_iv_share(tr::now), [=] { - _delegate->botShareGameScore(); + _delegate->botHandleMenuButton(MenuButton::ShareGame); }, &st::menuIconShare); } else { callback(tr::lng_bot_terms(tr::now), [=] { File::OpenUrl(tr::lng_mini_apps_tos_url(tr::now)); }, &st::menuIconGroupLog); + callback(tr::lng_profile_bot_privacy(tr::now), [=] { + _delegate->botOpenPrivacyPolicy(); + }, &st::menuIconAntispam); } const auto main = (_menuButtons & MenuButton::RemoveFromMainMenu); if (main || (_menuButtons & MenuButton::RemoveFromMenu)) { @@ -709,7 +712,7 @@ bool Panel::createWebview(const Webview::ThemeParams ¶ms) { } else if (command == "web_app_set_header_color") { processHeaderColor(arguments); } else if (command == "share_score") { - _delegate->botShareGameScore(); + _delegate->botHandleMenuButton(MenuButton::ShareGame); } }); diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h index f687c24e6..33ad0ad23 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h @@ -72,7 +72,7 @@ public: virtual void botAllowWriteAccess(Fn callback) = 0; virtual void botSharePhone(Fn callback) = 0; virtual void botInvokeCustomMethod(CustomMethodRequest request) = 0; - virtual void botShareGameScore() = 0; + virtual void botOpenPrivacyPolicy() = 0; virtual void botClose() = 0; };