Provided window session controller to api functions for bots.

This commit is contained in:
23rd 2022-06-09 05:51:09 +03:00
parent 4add87e7a9
commit e25b0e791d
4 changed files with 62 additions and 37 deletions

View File

@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history_item.h"
#include "history/history_item_components.h"
#include "main/main_session.h"
#include "window/window_session_controller.h"
#include "ui/toast/toast.h"
#include "ui/layers/generic_box.h"
#include "ui/text/text_utilities.h"
@ -31,6 +32,7 @@ namespace Api {
namespace {
void SendBotCallbackData(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column,
@ -73,6 +75,8 @@ void SendBotCallbackData(
if (withPassword) {
flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_password;
}
const auto weak = base::make_weak(controller.get());
const auto show = std::make_shared<Window::Show>(controller);
button->requestId = api->request(MTPmessages_GetBotCallbackAnswer(
MTP_flags(flags),
history->peer->input,
@ -100,12 +104,12 @@ void SendBotCallbackData(
if (!message.isEmpty()) {
if (showAlert) {
Ui::show(Ui::MakeInformBox(message));
show->showBox(Ui::MakeInformBox(message));
} else {
if (withPassword) {
Ui::hideLayer();
show->hideLayer();
}
Ui::Toast::Show(message);
Ui::Toast::Show(show->toastParent(), message);
}
} else if (!link.isEmpty()) {
if (!isGame) {
@ -116,12 +120,18 @@ void SendBotCallbackData(
session,
link,
item->fullId());
BotGameUrlClickHandler(bot, scoreLink).onClick({});
BotGameUrlClickHandler(bot, scoreLink).onClick({
Qt::LeftButton,
QVariant::fromValue(ClickHandlerContext{
.itemId = item->fullId(),
.sessionWindow = weak,
}),
});
session->sendProgressManager().update(
history,
Api::SendProgressType::PlayGame);
} else if (withPassword) {
Ui::hideLayer();
show->hideLayer();
}
}).fail([=](const MTP::Error &error) {
const auto item = owner->message(fullId);
@ -147,13 +157,15 @@ void SendBotCallbackData(
} // namespace
void SendBotCallbackData(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column) {
SendBotCallbackData(item, row, column, std::nullopt);
SendBotCallbackData(controller, item, row, column, std::nullopt);
}
void SendBotCallbackDataWithPassword(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column) {
@ -177,7 +189,9 @@ void SendBotCallbackDataWithPassword(
return;
}
api->cloudPassword().reload();
SendBotCallbackData(item, row, column, std::nullopt, [=](const QString &error) {
const auto weak = base::make_weak(controller.get());
const auto show = std::make_shared<Window::Show>(controller);
SendBotCallbackData(controller, item, row, column, std::nullopt, [=](const QString &error) {
auto box = PrePasswordErrorBox(
error,
session,
@ -185,7 +199,7 @@ void SendBotCallbackDataWithPassword(
tr::now,
Ui::Text::WithEntities));
if (box) {
Ui::show(std::move(box));
show->showBox(std::move(box), Ui::LayerOption::CloseOther);
} else {
auto lifetime = std::make_shared<rpl::lifetime>();
button->requestId = -1;
@ -219,14 +233,20 @@ void SendBotCallbackDataWithPassword(
return;
}
if (const auto item = owner->message(fullId)) {
SendBotCallbackData(item, row, column, result, [=](const QString &error) {
const auto strongController = weak.get();
if (!strongController) {
return;
}
SendBotCallbackData(strongController, item, row, column, result, [=](const QString &error) {
if (*box) {
(*box)->handleCustomCheckError(error);
}
});
}
};
*box = Ui::show(Box<PasscodeBox>(session, fields));
auto object = Box<PasscodeBox>(session, fields);
*box = Ui::MakeWeak(object.data());
show->showBox(std::move(object), Ui::LayerOption::CloseOther);
}, *lifetime);
}
});

View File

@ -9,14 +9,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
class HistoryItem;
namespace Window {
class SessionController;
} // namespace Window
namespace Api {
void SendBotCallbackData(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column);
void SendBotCallbackDataWithPassword(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column);

View File

@ -72,7 +72,7 @@ bool insertBotCommand(const QString &cmd) {
}
void activateBotCommand(
Window::SessionController *sessionController,
not_null<Window::SessionController*> sessionController,
not_null<const HistoryItem*> msg,
int row,
int column) {
@ -90,20 +90,19 @@ void activateBotCommand(
case ButtonType::Default: {
// Copy string before passing it to the sending method
// because the original button can be destroyed inside.
if (sessionController) {
MsgId replyTo = msg->isRegular() ? msg->id : 0;
sessionController->content()->sendBotCommand({
.peer = msg->history()->peer,
.command = QString(button->text),
.context = msg->fullId(),
.replyTo = replyTo,
});
}
const auto replyTo = msg->isRegular() ? msg->id : 0;
sessionController->content()->sendBotCommand({
.peer = msg->history()->peer,
.command = QString(button->text),
.context = msg->fullId(),
.replyTo = replyTo,
});
} break;
case ButtonType::Callback:
case ButtonType::Game: {
Api::SendBotCallbackData(
sessionController,
const_cast<HistoryItem*>(msg.get()),
row,
column);
@ -111,6 +110,7 @@ void activateBotCommand(
case ButtonType::CallbackWithPassword: {
Api::SendBotCallbackDataWithPassword(
sessionController,
const_cast<HistoryItem*>(msg.get()),
row,
column);
@ -120,7 +120,9 @@ void activateBotCommand(
Payments::CheckoutProcess::Start(
msg,
Payments::Mode::Payment,
crl::guard(App::wnd(), [](auto) { App::wnd()->activate(); }));
crl::guard(sessionController, [=](auto) {
sessionController->widget()->activate();
}));
} break;
case ButtonType::Url: {
@ -140,14 +142,15 @@ void activateBotCommand(
case ButtonType::RequestLocation: {
hideSingleUseKeyboard(msg);
Ui::show(Ui::MakeInformBox(tr::lng_bot_share_location_unavailable()));
sessionController->show(
Ui::MakeInformBox(tr::lng_bot_share_location_unavailable()));
} break;
case ButtonType::RequestPhone: {
hideSingleUseKeyboard(msg);
const auto msgId = msg->id;
const auto history = msg->history();
Ui::show(Ui::MakeConfirmBox({
sessionController->show(Ui::MakeConfirmBox({
.text = tr::lng_bot_share_phone(),
.confirmed = [=] {
Ui::showPeerHistory(history, ShowAtTheEndMsgId);
@ -224,24 +227,20 @@ void activateBotCommand(
case ButtonType::WebView: {
if (const auto bot = msg->getMessageBot()) {
if (sessionController) {
bot->session().attachWebView().request(
sessionController,
bot,
bot,
{ .text = button->text, .url = button->data });
}
bot->session().attachWebView().request(
sessionController,
bot,
bot,
{ .text = button->text, .url = button->data });
}
} break;
case ButtonType::SimpleWebView: {
if (const auto bot = msg->getMessageBot()) {
if (sessionController) {
bot->session().attachWebView().requestSimple(
sessionController,
bot,
{ .text = button->text, .url = button->data });
}
bot->session().attachWebView().requestSimple(
sessionController,
bot,
{ .text = button->text, .url = button->data });
}
} break;
}

View File

@ -36,7 +36,7 @@ template <typename Guard, typename Lambda>
bool insertBotCommand(const QString &cmd);
void activateBotCommand(
Window::SessionController *sessionController,
not_null<Window::SessionController*> sessionController,
not_null<const HistoryItem*> msg,
int row,
int column);