From 73c5988e7eb1845ff1f87fa17105746302fc1f21 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 5 Apr 2022 21:14:59 +0400 Subject: [PATCH] Allow opening external pages from attach bots. --- .../SourceFiles/core/local_url_handlers.cpp | 22 ++++++++++++++++++- .../inline_bots/bot_attach_web_view.cpp | 17 ++++++++++++-- .../payments/ui/payments_panel.cpp | 4 +++- .../ui/chat/attach/attach_bot_webview.cpp | 12 +++++++--- .../ui/chat/attach/attach_bot_webview.h | 3 +++ Telegram/lib_webview | 2 +- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Telegram/SourceFiles/core/local_url_handlers.cpp b/Telegram/SourceFiles/core/local_url_handlers.cpp index d8c4f4b92..d54f2cb71 100644 --- a/Telegram/SourceFiles/core/local_url_handlers.cpp +++ b/Telegram/SourceFiles/core/local_url_handlers.cpp @@ -62,6 +62,7 @@ bool JoinGroupByHash( return false; } Api::CheckChatInvite(controller, match->captured(1)); + controller->window().activate(); return true; } @@ -76,6 +77,7 @@ bool ShowStickerSet( controller->show(Box( controller, StickerSetIdentifier{ .shortName = match->captured(1) })); + controller->window().activate(); return true; } @@ -92,6 +94,7 @@ bool ShowTheme( &controller->window(), match->captured(1), fromMessageId); + controller->window().activate(); return true; } @@ -110,6 +113,9 @@ bool SetLanguage( const auto languageId = match->captured(2); Lang::CurrentCloudManager().switchWithWarning(languageId); } + if (controller) { + controller->window().activate(); + } return true; } @@ -128,6 +134,7 @@ bool ShareUrl( return false; } else { controller->content()->shareUrlLayer(url, params.value("text")); + controller->window().activate(); return true; } return false; @@ -152,6 +159,7 @@ bool ConfirmPhone( controller, phone, hash); + controller->window().activate(); return true; } @@ -166,6 +174,7 @@ bool ShareGameScore( match->captured(1), qthelp::UrlParamNameTransform::ToLower); ShareGameScoreByHash(controller, params.value(qsl("hash"))); + controller->window().activate(); return true; } @@ -179,6 +188,9 @@ bool ApplySocksProxy( ProxiesBoxController::ShowApplyConfirmation( MTP::ProxyData::Type::Socks5, params); + if (controller) { + controller->window().activate(); + } return true; } @@ -192,6 +204,9 @@ bool ApplyMtprotoProxy( ProxiesBoxController::ShowApplyConfirmation( MTP::ProxyData::Type::Mtproto, params); + if (controller) { + controller->window().activate(); + } return true; } @@ -243,7 +258,7 @@ bool ShowWallPaper( const auto bg = params.value("bg_color"); const auto color = params.value("color"); const auto gradient = params.value("gradient"); - return BackgroundPreviewBox::Start( + const auto result = BackgroundPreviewBox::Start( controller, (!color.isEmpty() ? color @@ -251,6 +266,8 @@ bool ShowWallPaper( ? gradient : params.value(qsl("slug"))), params); + controller->window().activate(); + return result; } [[nodiscard]] ChatAdminRights ParseRequestedAdminRights( @@ -376,6 +393,7 @@ bool ResolveUsernameOrPhone( : std::nullopt), .clickFromMessageId = fromMessageId, }); + controller->window().activate(); return true; } @@ -415,6 +433,7 @@ bool ResolvePrivatePost( : Navigation::RepliesByLinkInfo{ v::null }, .clickFromMessageId = fromMessageId, }); + controller->window().activate(); return true; } @@ -442,6 +461,7 @@ bool ResolveSettings( ? ::Settings::Sessions::Id() : ::Settings::Main::Id(); controller->showSettings(type); + controller->window().activate(); return true; } diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index ed77e63a7..47295de39 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -27,6 +27,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_controller.h" #include "window/window_session_controller.h" #include "core/application.h" +#include "core/local_url_handlers.h" +#include "ui/basic_click_handlers.h" #include "history/history.h" #include "history/history_item.h" #include "lang/lang_keys.h" @@ -566,8 +568,7 @@ void AttachWebView::show( cancel(); }); const auto sendData = crl::guard(this, [=](QByteArray data) { - if (_peer != _bot) { - cancel(); + if (_peer != _bot || queryId) { return; } const auto randomId = base::RandomValue(); @@ -581,6 +582,17 @@ void AttachWebView::show( }).send(); cancel(); }); + const auto handleLocalUri = [close](QString uri) { + const auto local = Core::TryConvertUrlToLocal(uri); + if (uri == local || Core::InternalPassportLink(local)) { + return local.startsWith(qstr("tg://")); + } else if (!local.startsWith(qstr("tg://"), Qt::CaseInsensitive)) { + return false; + } + UrlClickHandler::Open(local, {}); + crl::on_main(close); + return true; + }; auto title = Info::Profile::NameValue( _bot ) | rpl::map([](const TextWithEntities &value) { @@ -592,6 +604,7 @@ void AttachWebView::show( .userDataPath = _session->domain().local().webviewDataPath(), .title = std::move(title), .bottom = rpl::single('@' + _bot->username), + .handleLocalUri = handleLocalUri, .sendData = sendData, .close = close, .themeParams = [] { return Window::Theme::WebViewParams(); }, diff --git a/Telegram/SourceFiles/payments/ui/payments_panel.cpp b/Telegram/SourceFiles/payments/ui/payments_panel.cpp index 0aa5e85b3..c2a48d3d8 100644 --- a/Telegram/SourceFiles/payments/ui/payments_panel.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_panel.cpp @@ -541,9 +541,11 @@ bool Panel::createWebview() { _delegate->panelWebviewMessage(message, save); }); - raw->setNavigationStartHandler([=](const QString &uri) { + raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) { if (!_delegate->panelWebviewNavigationAttempt(uri)) { return false; + } else if (newWindow) { + return false; } showWebviewProgress(); return true; diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp index b6bd651eb..99a51cea5 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp @@ -300,10 +300,12 @@ Panel::Progress::Progress(QWidget *parent, Fn rect) Panel::Panel( const QString &userDataPath, rpl::producer title, + Fn handleLocalUri, Fn sendData, Fn close, Fn themeParams) : _userDataPath(userDataPath) +, _handleLocalUri(std::move(handleLocalUri)) , _sendData(std::move(sendData)) , _close(std::move(close)) , _widget(std::make_unique()) { @@ -553,12 +555,15 @@ bool Panel::createWebview() { sendDataMessage(list.at(1)); } else if (command == "web_app_setup_main_button") { processMainButtonMessage(list.at(1)); - } else if (command == "web_app_request_viewport") { - } }); - raw->setNavigationStartHandler([=](const QString &uri) { + raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) { + if (_handleLocalUri(uri)) { + return false; + } else if (newWindow) { + return true; + } showWebviewProgress(); return true; }); @@ -794,6 +799,7 @@ std::unique_ptr Show(Args &&args) { auto result = std::make_unique( args.userDataPath, std::move(args.title), + std::move(args.handleLocalUri), std::move(args.sendData), std::move(args.close), std::move(args.themeParams)); diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h index 681cdfb1c..c8feb45d4 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h @@ -32,6 +32,7 @@ public: Panel( const QString &userDataPath, rpl::producer title, + Fn handleLocalUri, Fn sendData, Fn close, Fn themeParams); @@ -75,6 +76,7 @@ private: void setupProgressGeometry(); QString _userDataPath; + Fn _handleLocalUri; Fn _sendData; Fn _close; std::unique_ptr _widget; @@ -95,6 +97,7 @@ struct Args { QString userDataPath; rpl::producer title; rpl::producer bottom; + Fn handleLocalUri; Fn sendData; Fn close; Fn themeParams; diff --git a/Telegram/lib_webview b/Telegram/lib_webview index d08b8cc84..cf52486be 160000 --- a/Telegram/lib_webview +++ b/Telegram/lib_webview @@ -1 +1 @@ -Subproject commit d08b8cc84054938cd6d694136709fcad32d5e6ce +Subproject commit cf52486beb79d23d6ad6302eb7a0b03e71761e94