Prolong bot attach webview.

This commit is contained in:
John Preston 2022-03-29 14:34:57 +04:00
parent a219cc43ce
commit 273f2f7ce9
5 changed files with 67 additions and 5 deletions

View file

@ -44,6 +44,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_cloud_manager.h" #include "lang/lang_cloud_manager.h"
#include "lang/lang_hardcoded.h" #include "lang/lang_hardcoded.h"
#include "lang/lang_instance.h" #include "lang/lang_instance.h"
#include "inline_bots/bot_attach_web_view.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "core/file_utilities.h" #include "core/file_utilities.h"
#include "core/click_handler_types.h" // ClickHandlerContext. #include "core/click_handler_types.h" // ClickHandlerContext.
@ -191,6 +192,7 @@ Application::~Application() {
// For example Domain::removeRedundantAccounts() is called from // For example Domain::removeRedundantAccounts() is called from
// Domain::finish() and there is a violation on Ensures(started()). // Domain::finish() and there is a violation on Ensures(started()).
Payments::CheckoutProcess::ClearAll(); Payments::CheckoutProcess::ClearAll();
InlineBots::AttachWebView::ClearAll();
_domain->finish(); _domain->finish();

View file

@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "main/main_session.h" #include "main/main_session.h"
#include "main/main_domain.h" #include "main/main_domain.h"
#include "storage/storage_domain.h" #include "storage/storage_domain.h"
#include "info/profile/info_profile_values.h"
#include "ui/boxes/confirm_box.h" #include "ui/boxes/confirm_box.h"
#include "ui/toasts/common_toasts.h" #include "ui/toasts/common_toasts.h"
#include "ui/chat/attach/attach_bot_webview.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 "core/application.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "base/random.h" #include "base/random.h"
#include "base/timer_rpl.h"
#include "apiwrap.h" #include "apiwrap.h"
namespace InlineBots { namespace InlineBots {
namespace { namespace {
constexpr auto kProlongTimeout = 60 * crl::time(1000);
[[nodiscard]] UserData *ParseAttachBot( [[nodiscard]] UserData *ParseAttachBot(
not_null<Main::Session*> session, not_null<Main::Session*> session,
const MTPAttachMenuBot &bot) { const MTPAttachMenuBot &bot) {
@ -36,13 +40,20 @@ namespace {
}); });
} }
[[nodiscard]] base::flat_set<not_null<AttachWebView*>> &ActiveWebViews() {
static auto result = base::flat_set<not_null<AttachWebView*>>();
return result;
}
} // namespace } // namespace
AttachWebView::AttachWebView(not_null<Main::Session*> session) AttachWebView::AttachWebView(not_null<Main::Session*> session)
: _session(session) { : _session(session) {
} }
AttachWebView::~AttachWebView() = default; AttachWebView::~AttachWebView() {
ActiveWebViews().remove(this);
}
void AttachWebView::request( void AttachWebView::request(
not_null<PeerData*> peer, not_null<PeerData*> peer,
@ -118,7 +129,9 @@ void AttachWebView::request(const WebViewButton &button) {
} }
void AttachWebView::cancel() { void AttachWebView::cancel() {
ActiveWebViews().remove(this);
_session->api().request(base::take(_requestId)).cancel(); _session->api().request(base::take(_requestId)).cancel();
_session->api().request(base::take(_prolongId)).cancel();
_panel = nullptr; _panel = nullptr;
_peer = _bot = nullptr; _peer = _bot = nullptr;
_botUsername = QString(); _botUsername = QString();
@ -199,6 +212,12 @@ void AttachWebView::requestSimple(
}).send(); }).send();
} }
void AttachWebView::ClearAll() {
while (!ActiveWebViews().empty()) {
ActiveWebViews().front()->cancel();
}
}
void AttachWebView::show( void AttachWebView::show(
uint64 queryId, uint64 queryId,
const QString &url, const QString &url,
@ -224,13 +243,26 @@ void AttachWebView::show(
}).send(); }).send();
cancel(); cancel();
}); });
auto title = Info::Profile::NameValue(
_bot
) | rpl::map([](const TextWithEntities &value) {
return value.text;
});
ActiveWebViews().emplace(this);
_panel = Ui::BotWebView::Show({ _panel = Ui::BotWebView::Show({
.url = url, .url = url,
.userDataPath = _session->domain().local().webviewDataPath(), .userDataPath = _session->domain().local().webviewDataPath(),
.title = std::move(title),
.bottom = rpl::single('@' + _bot->username),
.sendData = sendData, .sendData = sendData,
.close = close, .close = close,
.themeParams = [] { return Window::Theme::WebViewParams(); }, .themeParams = [] { return Window::Theme::WebViewParams(); },
}); });
started(queryId);
}
void AttachWebView::started(uint64 queryId) {
Expects(_peer != nullptr && _bot != nullptr);
_session->data().webViewResultSent( _session->data().webViewResultSent(
) | rpl::filter([=](const Data::Session::WebViewResultSent &sent) { ) | rpl::filter([=](const Data::Session::WebViewResultSent &sent) {
@ -240,6 +272,23 @@ void AttachWebView::show(
}) | rpl::start_with_next([=] { }) | rpl::start_with_next([=] {
cancel(); cancel();
}, _panel->lifetime()); }, _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<void()> callback) { void AttachWebView::requestAddToMenu(Fn<void()> callback) {
@ -267,7 +316,7 @@ void AttachWebView::toggleInMenu(bool enabled, Fn<void()> callback) {
_requestId = _session->api().request(MTPmessages_ToggleBotInAttachMenu( _requestId = _session->api().request(MTPmessages_ToggleBotInAttachMenu(
_bot->inputUser, _bot->inputUser,
MTP_bool(enabled) MTP_bool(enabled)
)).done([=](const MTPBool &result) { )).done([=] {
_requestId = 0; _requestId = 0;
callback(); callback();
}).fail([=] { }).fail([=] {

View file

@ -42,8 +42,11 @@ public:
not_null<UserData*> bot, not_null<UserData*> bot,
const WebViewButton &button); const WebViewButton &button);
private:
void cancel(); void cancel();
static void ClearAll();
private:
void resolve(); void resolve();
void request(const WebViewButton &button = WebViewButton()); void request(const WebViewButton &button = WebViewButton());
void requestByUsername(); void requestByUsername();
@ -58,6 +61,7 @@ private:
const QString &url, const QString &url,
const QString &buttonText = QString()); const QString &buttonText = QString());
void requestAddToMenu(Fn<void()> callback); void requestAddToMenu(Fn<void()> callback);
void started(uint64 queryId);
const not_null<Main::Session*> _session; const not_null<Main::Session*> _session;
@ -65,8 +69,10 @@ private:
UserData *_bot = nullptr; UserData *_bot = nullptr;
QString _botUsername; QString _botUsername;
QPointer<Ui::GenericBox> _confirmAddBox; QPointer<Ui::GenericBox> _confirmAddBox;
MsgId _replyToMsgId;
mtpRequestId _requestId = 0; mtpRequestId _requestId = 0;
mtpRequestId _prolongId = 0;
std::unique_ptr<Ui::BotWebView::Panel> _panel; std::unique_ptr<Ui::BotWebView::Panel> _panel;

View file

@ -68,6 +68,7 @@ Panel::Progress::Progress(QWidget *parent, Fn<QRect()> rect)
Panel::Panel( Panel::Panel(
const QString &userDataPath, const QString &userDataPath,
rpl::producer<QString> title,
Fn<void(QByteArray)> sendData, Fn<void(QByteArray)> sendData,
Fn<void()> close, Fn<void()> close,
Fn<QByteArray()> themeParams) Fn<QByteArray()> themeParams)
@ -95,7 +96,7 @@ Panel::Panel(
}); });
}, _widget->lifetime()); }, _widget->lifetime());
setTitle(rpl::single(u"Title"_q)); setTitle(std::move(title));
} }
Panel::~Panel() { Panel::~Panel() {
@ -473,10 +474,11 @@ rpl::lifetime &Panel::lifetime() {
std::unique_ptr<Panel> Show(Args &&args) { std::unique_ptr<Panel> Show(Args &&args) {
auto result = std::make_unique<Panel>( auto result = std::make_unique<Panel>(
args.userDataPath, args.userDataPath,
std::move(args.title),
std::move(args.sendData), std::move(args.sendData),
std::move(args.close), std::move(args.close),
std::move(args.themeParams)); std::move(args.themeParams));
result->showWebview(args.url, rpl::single(u"smth"_q)); result->showWebview(args.url, std::move(args.bottom));
return result; return result;
} }

View file

@ -25,6 +25,7 @@ class Panel final {
public: public:
Panel( Panel(
const QString &userDataPath, const QString &userDataPath,
rpl::producer<QString> title,
Fn<void(QByteArray)> sendData, Fn<void(QByteArray)> sendData,
Fn<void()> close, Fn<void()> close,
Fn<QByteArray()> themeParams); Fn<QByteArray()> themeParams);
@ -76,6 +77,8 @@ private:
struct Args { struct Args {
QString url; QString url;
QString userDataPath; QString userDataPath;
rpl::producer<QString> title;
rpl::producer<QString> bottom;
Fn<void(QByteArray)> sendData; Fn<void(QByteArray)> sendData;
Fn<void()> close; Fn<void()> close;
Fn<QByteArray()> themeParams; Fn<QByteArray()> themeParams;