From 273f2f7ce90405a2a84303a36c10b99fc46407c0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 29 Mar 2022 14:34:57 +0400 Subject: [PATCH] Prolong bot attach webview. --- Telegram/SourceFiles/core/application.cpp | 2 + .../inline_bots/bot_attach_web_view.cpp | 53 ++++++++++++++++++- .../inline_bots/bot_attach_web_view.h | 8 ++- .../ui/chat/attach/attach_bot_webview.cpp | 6 ++- .../ui/chat/attach/attach_bot_webview.h | 3 ++ 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index e54d196a2..eb2c21604 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -44,6 +44,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_cloud_manager.h" #include "lang/lang_hardcoded.h" #include "lang/lang_instance.h" +#include "inline_bots/bot_attach_web_view.h" #include "mainwidget.h" #include "core/file_utilities.h" #include "core/click_handler_types.h" // ClickHandlerContext. @@ -191,6 +192,7 @@ Application::~Application() { // For example Domain::removeRedundantAccounts() is called from // Domain::finish() and there is a violation on Ensures(started()). Payments::CheckoutProcess::ClearAll(); + InlineBots::AttachWebView::ClearAll(); _domain->finish(); diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index a3a860f00..81842e025 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "main/main_domain.h" #include "storage/storage_domain.h" +#include "info/profile/info_profile_values.h" #include "ui/boxes/confirm_box.h" #include "ui/toasts/common_toasts.h" #include "ui/chat/attach/attach_bot_webview.h" @@ -20,11 +21,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "lang/lang_keys.h" #include "base/random.h" +#include "base/timer_rpl.h" #include "apiwrap.h" namespace InlineBots { namespace { +constexpr auto kProlongTimeout = 60 * crl::time(1000); + [[nodiscard]] UserData *ParseAttachBot( not_null session, const MTPAttachMenuBot &bot) { @@ -36,13 +40,20 @@ namespace { }); } +[[nodiscard]] base::flat_set> &ActiveWebViews() { + static auto result = base::flat_set>(); + return result; +} + } // namespace AttachWebView::AttachWebView(not_null session) : _session(session) { } -AttachWebView::~AttachWebView() = default; +AttachWebView::~AttachWebView() { + ActiveWebViews().remove(this); +} void AttachWebView::request( not_null peer, @@ -118,7 +129,9 @@ void AttachWebView::request(const WebViewButton &button) { } void AttachWebView::cancel() { + ActiveWebViews().remove(this); _session->api().request(base::take(_requestId)).cancel(); + _session->api().request(base::take(_prolongId)).cancel(); _panel = nullptr; _peer = _bot = nullptr; _botUsername = QString(); @@ -199,6 +212,12 @@ void AttachWebView::requestSimple( }).send(); } +void AttachWebView::ClearAll() { + while (!ActiveWebViews().empty()) { + ActiveWebViews().front()->cancel(); + } +} + void AttachWebView::show( uint64 queryId, const QString &url, @@ -224,13 +243,26 @@ void AttachWebView::show( }).send(); cancel(); }); + auto title = Info::Profile::NameValue( + _bot + ) | rpl::map([](const TextWithEntities &value) { + return value.text; + }); + ActiveWebViews().emplace(this); _panel = Ui::BotWebView::Show({ .url = url, .userDataPath = _session->domain().local().webviewDataPath(), + .title = std::move(title), + .bottom = rpl::single('@' + _bot->username), .sendData = sendData, .close = close, .themeParams = [] { return Window::Theme::WebViewParams(); }, }); + started(queryId); +} + +void AttachWebView::started(uint64 queryId) { + Expects(_peer != nullptr && _bot != nullptr); _session->data().webViewResultSent( ) | rpl::filter([=](const Data::Session::WebViewResultSent &sent) { @@ -240,6 +272,23 @@ void AttachWebView::show( }) | rpl::start_with_next([=] { cancel(); }, _panel->lifetime()); + + base::timer_each( + kProlongTimeout + ) | rpl::start_with_next([=] { + using Flag = MTPmessages_ProlongWebView::Flag; + auto flags = Flag::f_reply_to_msg_id | Flag::f_silent; + _session->api().request(base::take(_prolongId)).cancel(); + _prolongId = _session->api().request(MTPmessages_ProlongWebView( + MTP_flags(flags), + _peer->input, + _bot->inputUser, + MTP_long(queryId), + MTP_int(_replyToMsgId.bare) + )).done([=] { + _prolongId = 0; + }).send(); + }, _panel->lifetime()); } void AttachWebView::requestAddToMenu(Fn callback) { @@ -267,7 +316,7 @@ void AttachWebView::toggleInMenu(bool enabled, Fn callback) { _requestId = _session->api().request(MTPmessages_ToggleBotInAttachMenu( _bot->inputUser, MTP_bool(enabled) - )).done([=](const MTPBool &result) { + )).done([=] { _requestId = 0; callback(); }).fail([=] { diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h index 148ea6de3..111deb358 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h @@ -42,8 +42,11 @@ public: not_null bot, const WebViewButton &button); -private: void cancel(); + + static void ClearAll(); + +private: void resolve(); void request(const WebViewButton &button = WebViewButton()); void requestByUsername(); @@ -58,6 +61,7 @@ private: const QString &url, const QString &buttonText = QString()); void requestAddToMenu(Fn callback); + void started(uint64 queryId); const not_null _session; @@ -65,8 +69,10 @@ private: UserData *_bot = nullptr; QString _botUsername; QPointer _confirmAddBox; + MsgId _replyToMsgId; mtpRequestId _requestId = 0; + mtpRequestId _prolongId = 0; std::unique_ptr _panel; diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp index 04944e210..b5c72e76a 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp @@ -68,6 +68,7 @@ Panel::Progress::Progress(QWidget *parent, Fn rect) Panel::Panel( const QString &userDataPath, + rpl::producer title, Fn sendData, Fn close, Fn themeParams) @@ -95,7 +96,7 @@ Panel::Panel( }); }, _widget->lifetime()); - setTitle(rpl::single(u"Title"_q)); + setTitle(std::move(title)); } Panel::~Panel() { @@ -473,10 +474,11 @@ rpl::lifetime &Panel::lifetime() { std::unique_ptr Show(Args &&args) { auto result = std::make_unique( args.userDataPath, + std::move(args.title), std::move(args.sendData), std::move(args.close), std::move(args.themeParams)); - result->showWebview(args.url, rpl::single(u"smth"_q)); + result->showWebview(args.url, std::move(args.bottom)); return result; } diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h index a5fe8e039..1cd6c5377 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h @@ -25,6 +25,7 @@ class Panel final { public: Panel( const QString &userDataPath, + rpl::producer title, Fn sendData, Fn close, Fn themeParams); @@ -76,6 +77,8 @@ private: struct Args { QString url; QString userDataPath; + rpl::producer title; + rpl::producer bottom; Fn sendData; Fn close; Fn themeParams;