Skip to content

Commit

Permalink
Provided window session controller to api functions for bots.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Jun 9, 2022
1 parent 4add87e commit e25b0e7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
40 changes: 30 additions & 10 deletions Telegram/SourceFiles/api/api_bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ For license and copyright information please follow this link:
#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"
Expand All @@ -31,6 +32,7 @@ namespace Api {
namespace {

void SendBotCallbackData(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int row,
int column,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -177,15 +189,17 @@ 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,
tr::lng_bots_password_confirm_check_about(
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;
Expand Down Expand Up @@ -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);
}
});
Expand Down
6 changes: 6 additions & 0 deletions Telegram/SourceFiles/api/api_bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ For license and copyright information please follow this link:

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);
Expand Down
51 changes: 25 additions & 26 deletions Telegram/SourceFiles/facades.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -90,27 +90,27 @@ 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);
} break;

case ButtonType::CallbackWithPassword: {
Api::SendBotCallbackDataWithPassword(
sessionController,
const_cast<HistoryItem*>(msg.get()),
row,
column);
Expand All @@ -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: {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/facades.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e25b0e7

Please sign in to comment.