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_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();

View file

@ -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<Main::Session*> session,
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
AttachWebView::AttachWebView(not_null<Main::Session*> session)
: _session(session) {
}
AttachWebView::~AttachWebView() = default;
AttachWebView::~AttachWebView() {
ActiveWebViews().remove(this);
}
void AttachWebView::request(
not_null<PeerData*> 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<void()> callback) {
@ -267,7 +316,7 @@ void AttachWebView::toggleInMenu(bool enabled, Fn<void()> callback) {
_requestId = _session->api().request(MTPmessages_ToggleBotInAttachMenu(
_bot->inputUser,
MTP_bool(enabled)
)).done([=](const MTPBool &result) {
)).done([=] {
_requestId = 0;
callback();
}).fail([=] {

View file

@ -42,8 +42,11 @@ public:
not_null<UserData*> 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<void()> callback);
void started(uint64 queryId);
const not_null<Main::Session*> _session;
@ -65,8 +69,10 @@ private:
UserData *_bot = nullptr;
QString _botUsername;
QPointer<Ui::GenericBox> _confirmAddBox;
MsgId _replyToMsgId;
mtpRequestId _requestId = 0;
mtpRequestId _prolongId = 0;
std::unique_ptr<Ui::BotWebView::Panel> _panel;

View file

@ -68,6 +68,7 @@ Panel::Progress::Progress(QWidget *parent, Fn<QRect()> rect)
Panel::Panel(
const QString &userDataPath,
rpl::producer<QString> title,
Fn<void(QByteArray)> sendData,
Fn<void()> close,
Fn<QByteArray()> 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<Panel> Show(Args &&args) {
auto result = std::make_unique<Panel>(
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;
}

View file

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