Support ?startapp=value start_param passing.

This commit is contained in:
John Preston 2023-09-15 15:34:43 +04:00
parent b17b806d91
commit c12743925e
4 changed files with 49 additions and 25 deletions

View File

@ -435,6 +435,8 @@ bool ResolveUsernameOrPhone(
.attachBotUsername = params.value(u"attach"_q), .attachBotUsername = params.value(u"attach"_q),
.attachBotToggleCommand = (params.contains(u"startattach"_q) .attachBotToggleCommand = (params.contains(u"startattach"_q)
? params.value(u"startattach"_q) ? params.value(u"startattach"_q)
: (appname.isEmpty() && params.contains(u"startapp"_q))
? params.value(u"startapp"_q)
: std::optional<QString>()), : std::optional<QString>()),
.attachBotMenuOpen = (appname.isEmpty() .attachBotMenuOpen = (appname.isEmpty()
&& params.contains(u"startapp"_q)), && params.contains(u"startapp"_q)),

View File

@ -878,7 +878,10 @@ void AttachWebView::cancel() {
_startCommand = QString(); _startCommand = QString();
} }
void AttachWebView::requestBots() { void AttachWebView::requestBots(Fn<void()> callback) {
if (callback) {
_botsRequestCallbacks.push_back(std::move(callback));
}
if (_botsRequestId) { if (_botsRequestId) {
return; return;
} }
@ -899,8 +902,14 @@ void AttachWebView::requestBots() {
} }
_attachBotsUpdates.fire({}); _attachBotsUpdates.fire({});
}); });
for (const auto callback : base::take(_botsRequestCallbacks)) {
callback();
}
}).fail([=] { }).fail([=] {
_botsRequestId = 0; _botsRequestId = 0;
for (const auto callback : base::take(_botsRequestCallbacks)) {
callback();
}
}).send(); }).send();
} }
@ -998,8 +1007,12 @@ void AttachWebView::requestAddToMenu(
return true; return true;
} }
} else if (v::is<AddToMenuOpenMenu>(open)) { } else if (v::is<AddToMenuOpenMenu>(open)) {
const auto &openMenu = v::get<AddToMenuOpenMenu>(open);
_bot = bot; _bot = bot;
requestSimple(strong, bot, { .fromMainMenu = true }); requestSimple(strong, bot, {
.startCommand = openMenu.startCommand,
.fromMainMenu = true,
});
return true; return true;
} else if (const auto useTypes = chooseTypes & types) { } else if (const auto useTypes = chooseTypes & types) {
const auto done = [=](not_null<Data::Thread*> thread) { const auto done = [=](not_null<Data::Thread*> thread) {
@ -1127,9 +1140,7 @@ void AttachWebView::requestSimple(
_context->fromSwitch = button.fromSwitch; _context->fromSwitch = button.fromSwitch;
_context->fromMainMenu = button.fromMainMenu; _context->fromMainMenu = button.fromMainMenu;
if (button.fromMainMenu) { if (button.fromMainMenu) {
acceptDisclaimer(controller, [=] { acceptMainMenuDisclaimer(controller, button);
requestSimple(button);
});
} else { } else {
confirmOpen(controller, [=] { confirmOpen(controller, [=] {
requestSimple(button); requestSimple(button);
@ -1141,11 +1152,16 @@ void AttachWebView::requestSimple(const WebViewButton &button) {
using Flag = MTPmessages_RequestSimpleWebView::Flag; using Flag = MTPmessages_RequestSimpleWebView::Flag;
_requestId = _session->api().request(MTPmessages_RequestSimpleWebView( _requestId = _session->api().request(MTPmessages_RequestSimpleWebView(
MTP_flags(Flag::f_theme_params 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())), | (button.fromSwitch ? Flag::f_from_switch_webview : Flag())),
_bot->inputUser, _bot->inputUser,
MTP_bytes(button.url), MTP_bytes(button.url),
MTP_string(""), // start_param MTP_string(button.startCommand),
MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)), MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)),
MTP_string("tdesktop") MTP_string("tdesktop")
)).done([=](const MTPSimpleWebViewResult &result) { )).done([=](const MTPSimpleWebViewResult &result) {
@ -1360,9 +1376,11 @@ void AttachWebView::confirmOpen(
})); }));
} }
void AttachWebView::acceptDisclaimer( void AttachWebView::acceptMainMenuDisclaimer(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
Fn<void()> done) { const WebViewButton &button) {
Expects(button.fromMainMenu);
const auto local = _bot ? &_bot->session().local() : nullptr; const auto local = _bot ? &_bot->session().local() : nullptr;
if (!local) { if (!local) {
return; return;
@ -1375,10 +1393,12 @@ void AttachWebView::acceptDisclaimer(
_attachBotsUpdates.fire({}); _attachBotsUpdates.fire({});
return; return;
} else if (i->inactive) { } else if (i->inactive) {
requestAddToMenu(_bot, AddToMenuOpenMenu(), controller, {}); requestAddToMenu(_bot, AddToMenuOpenMenu{
.startCommand = button.startCommand,
}, controller, {});
return; return;
} else if (!i->disclaimerRequired || disclaimerAccepted(*i)) { } else if (!i->disclaimerRequired || disclaimerAccepted(*i)) {
done(); requestSimple(button);
return; return;
} }
@ -1386,7 +1406,7 @@ void AttachWebView::acceptDisclaimer(
controller->show(Box(FillDisclaimerBox, crl::guard(this, [=] { controller->show(Box(FillDisclaimerBox, crl::guard(this, [=] {
_disclaimerAccepted.emplace(_bot); _disclaimerAccepted.emplace(_bot);
_attachBotsUpdates.fire({}); _attachBotsUpdates.fire({});
done(); requestSimple(button);
}))); })));
} }
@ -1593,10 +1613,8 @@ void AttachWebView::toggleInMenu(
MTP_bool(state != ToggledState::Removed) MTP_bool(state != ToggledState::Removed)
)).done([=] { )).done([=] {
_requestId = 0; _requestId = 0;
requestBots(); _session->api().request(base::take(_botsRequestId)).cancel();
if (callback) { requestBots(std::move(callback));
callback();
}
}).fail([=] { }).fail([=] {
cancel(); cancel();
}).send(); }).send();

View File

@ -75,6 +75,7 @@ struct AddToMenuOpenAttach {
PeerTypes chooseTypes; PeerTypes chooseTypes;
}; };
struct AddToMenuOpenMenu { struct AddToMenuOpenMenu {
QString startCommand;
}; };
struct AddToMenuOpenApp { struct AddToMenuOpenApp {
not_null<BotAppData*> app; not_null<BotAppData*> app;
@ -127,7 +128,7 @@ public:
void cancel(); void cancel();
void requestBots(); void requestBots(Fn<void()> callback = nullptr);
[[nodiscard]] const std::vector<AttachWebViewBot> &attachBots() const { [[nodiscard]] const std::vector<AttachWebViewBot> &attachBots() const {
return _attachBots; return _attachBots;
} }
@ -196,9 +197,9 @@ private:
void confirmOpen( void confirmOpen(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
Fn<void()> done); Fn<void()> done);
void acceptDisclaimer( void acceptMainMenuDisclaimer(
not_null<Window::SessionController*> controller, not_null<Window::SessionController*> controller,
Fn<void()> done); const WebViewButton &button);
enum class ToggledState { enum class ToggledState {
Removed, Removed,
@ -251,6 +252,7 @@ private:
uint64 _botsHash = 0; uint64 _botsHash = 0;
mtpRequestId _botsRequestId = 0; mtpRequestId _botsRequestId = 0;
std::vector<Fn<void()>> _botsRequestCallbacks;
std::unique_ptr<Context> _addToMenuContext; std::unique_ptr<Context> _addToMenuContext;
UserData *_addToMenuBot = nullptr; UserData *_addToMenuBot = nullptr;

View File

@ -578,6 +578,14 @@ void SessionNavigation::showPeerByLinkResolved(
attachBotUsername, attachBotUsername,
info.attachBotToggleCommand.value_or(QString())); 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<Api::SendAction>());
} else if (bot && info.attachBotToggleCommand) { } else if (bot && info.attachBotToggleCommand) {
const auto itemId = info.clickFromMessageId; const auto itemId = info.clickFromMessageId;
const auto item = _session->data().message(itemId); const auto item = _session->data().message(itemId);
@ -598,12 +606,6 @@ void SessionNavigation::showPeerByLinkResolved(
? Api::SendAction( ? Api::SendAction(
contextUser->owner().history(contextUser)) contextUser->owner().history(contextUser))
: std::optional<Api::SendAction>())); : std::optional<Api::SendAction>()));
} else if (bot && info.attachBotMenuOpen) {
bot->session().attachWebView().requestAddToMenu(
bot,
InlineBots::AddToMenuOpenMenu(),
parentController(),
std::optional<Api::SendAction>());
} else { } else {
crl::on_main(this, [=] { crl::on_main(this, [=] {
showPeerHistory(peer, params, msgId); showPeerHistory(peer, params, msgId);