From c12743925e5776ee57e48723c17a10e24a532950 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 15 Sep 2023 15:34:43 +0400 Subject: [PATCH] Support ?startapp=value start_param passing. --- .../SourceFiles/core/local_url_handlers.cpp | 2 + .../inline_bots/bot_attach_web_view.cpp | 50 +++++++++++++------ .../inline_bots/bot_attach_web_view.h | 8 +-- .../window/window_session_controller.cpp | 14 +++--- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/core/local_url_handlers.cpp b/Telegram/SourceFiles/core/local_url_handlers.cpp index 8d39fafb6..fbf4d1a39 100644 --- a/Telegram/SourceFiles/core/local_url_handlers.cpp +++ b/Telegram/SourceFiles/core/local_url_handlers.cpp @@ -435,6 +435,8 @@ bool ResolveUsernameOrPhone( .attachBotUsername = params.value(u"attach"_q), .attachBotToggleCommand = (params.contains(u"startattach"_q) ? params.value(u"startattach"_q) + : (appname.isEmpty() && params.contains(u"startapp"_q)) + ? params.value(u"startapp"_q) : std::optional()), .attachBotMenuOpen = (appname.isEmpty() && params.contains(u"startapp"_q)), diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index c8c9ec316..af40fd949 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -878,7 +878,10 @@ void AttachWebView::cancel() { _startCommand = QString(); } -void AttachWebView::requestBots() { +void AttachWebView::requestBots(Fn callback) { + if (callback) { + _botsRequestCallbacks.push_back(std::move(callback)); + } if (_botsRequestId) { return; } @@ -899,8 +902,14 @@ void AttachWebView::requestBots() { } _attachBotsUpdates.fire({}); }); + for (const auto callback : base::take(_botsRequestCallbacks)) { + callback(); + } }).fail([=] { _botsRequestId = 0; + for (const auto callback : base::take(_botsRequestCallbacks)) { + callback(); + } }).send(); } @@ -998,8 +1007,12 @@ void AttachWebView::requestAddToMenu( return true; } } else if (v::is(open)) { + const auto &openMenu = v::get(open); _bot = bot; - requestSimple(strong, bot, { .fromMainMenu = true }); + requestSimple(strong, bot, { + .startCommand = openMenu.startCommand, + .fromMainMenu = true, + }); return true; } else if (const auto useTypes = chooseTypes & types) { const auto done = [=](not_null thread) { @@ -1127,9 +1140,7 @@ void AttachWebView::requestSimple( _context->fromSwitch = button.fromSwitch; _context->fromMainMenu = button.fromMainMenu; if (button.fromMainMenu) { - acceptDisclaimer(controller, [=] { - requestSimple(button); - }); + acceptMainMenuDisclaimer(controller, button); } else { confirmOpen(controller, [=] { requestSimple(button); @@ -1141,11 +1152,16 @@ void AttachWebView::requestSimple(const WebViewButton &button) { using Flag = MTPmessages_RequestSimpleWebView::Flag; _requestId = _session->api().request(MTPmessages_RequestSimpleWebView( MTP_flags(Flag::f_theme_params - | (button.fromMainMenu ? Flag::f_from_side_menu : Flag::f_url) + | (button.fromMainMenu + ? (Flag::f_from_side_menu + | (button.startCommand.isEmpty() + ? Flag() + : Flag::f_start_param)) + : Flag::f_url) | (button.fromSwitch ? Flag::f_from_switch_webview : Flag())), _bot->inputUser, MTP_bytes(button.url), - MTP_string(""), // start_param + MTP_string(button.startCommand), MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)), MTP_string("tdesktop") )).done([=](const MTPSimpleWebViewResult &result) { @@ -1360,9 +1376,11 @@ void AttachWebView::confirmOpen( })); } -void AttachWebView::acceptDisclaimer( +void AttachWebView::acceptMainMenuDisclaimer( not_null controller, - Fn done) { + const WebViewButton &button) { + Expects(button.fromMainMenu); + const auto local = _bot ? &_bot->session().local() : nullptr; if (!local) { return; @@ -1375,10 +1393,12 @@ void AttachWebView::acceptDisclaimer( _attachBotsUpdates.fire({}); return; } else if (i->inactive) { - requestAddToMenu(_bot, AddToMenuOpenMenu(), controller, {}); + requestAddToMenu(_bot, AddToMenuOpenMenu{ + .startCommand = button.startCommand, + }, controller, {}); return; } else if (!i->disclaimerRequired || disclaimerAccepted(*i)) { - done(); + requestSimple(button); return; } @@ -1386,7 +1406,7 @@ void AttachWebView::acceptDisclaimer( controller->show(Box(FillDisclaimerBox, crl::guard(this, [=] { _disclaimerAccepted.emplace(_bot); _attachBotsUpdates.fire({}); - done(); + requestSimple(button); }))); } @@ -1593,10 +1613,8 @@ void AttachWebView::toggleInMenu( MTP_bool(state != ToggledState::Removed) )).done([=] { _requestId = 0; - requestBots(); - if (callback) { - callback(); - } + _session->api().request(base::take(_botsRequestId)).cancel(); + requestBots(std::move(callback)); }).fail([=] { cancel(); }).send(); diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h index f08f68027..04fbd06f4 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h @@ -75,6 +75,7 @@ struct AddToMenuOpenAttach { PeerTypes chooseTypes; }; struct AddToMenuOpenMenu { + QString startCommand; }; struct AddToMenuOpenApp { not_null app; @@ -127,7 +128,7 @@ public: void cancel(); - void requestBots(); + void requestBots(Fn callback = nullptr); [[nodiscard]] const std::vector &attachBots() const { return _attachBots; } @@ -196,9 +197,9 @@ private: void confirmOpen( not_null controller, Fn done); - void acceptDisclaimer( + void acceptMainMenuDisclaimer( not_null controller, - Fn done); + const WebViewButton &button); enum class ToggledState { Removed, @@ -251,6 +252,7 @@ private: uint64 _botsHash = 0; mtpRequestId _botsRequestId = 0; + std::vector> _botsRequestCallbacks; std::unique_ptr _addToMenuContext; UserData *_addToMenuBot = nullptr; diff --git a/Telegram/SourceFiles/window/window_session_controller.cpp b/Telegram/SourceFiles/window/window_session_controller.cpp index 98d74822a..3a45ff8f7 100644 --- a/Telegram/SourceFiles/window/window_session_controller.cpp +++ b/Telegram/SourceFiles/window/window_session_controller.cpp @@ -578,6 +578,14 @@ void SessionNavigation::showPeerByLinkResolved( attachBotUsername, info.attachBotToggleCommand.value_or(QString())); }); + } else if (bot && info.attachBotMenuOpen) { + const auto startCommand = info.attachBotToggleCommand.value_or( + QString()); + bot->session().attachWebView().requestAddToMenu( + bot, + InlineBots::AddToMenuOpenMenu{ startCommand }, + parentController(), + std::optional()); } else if (bot && info.attachBotToggleCommand) { const auto itemId = info.clickFromMessageId; const auto item = _session->data().message(itemId); @@ -598,12 +606,6 @@ void SessionNavigation::showPeerByLinkResolved( ? Api::SendAction( contextUser->owner().history(contextUser)) : std::optional())); - } else if (bot && info.attachBotMenuOpen) { - bot->session().attachWebView().requestAddToMenu( - bot, - InlineBots::AddToMenuOpenMenu(), - parentController(), - std::optional()); } else { crl::on_main(this, [=] { showPeerHistory(peer, params, msgId);