mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Provided window session controller to api functions for bots.
This commit is contained in:
parent
4add87e7a9
commit
e25b0e791d
4 changed files with 62 additions and 37 deletions
|
@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "history/history_item.h"
|
#include "history/history_item.h"
|
||||||
#include "history/history_item_components.h"
|
#include "history/history_item_components.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
#include "window/window_session_controller.h"
|
||||||
#include "ui/toast/toast.h"
|
#include "ui/toast/toast.h"
|
||||||
#include "ui/layers/generic_box.h"
|
#include "ui/layers/generic_box.h"
|
||||||
#include "ui/text/text_utilities.h"
|
#include "ui/text/text_utilities.h"
|
||||||
|
@ -31,6 +32,7 @@ namespace Api {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void SendBotCallbackData(
|
void SendBotCallbackData(
|
||||||
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
int row,
|
int row,
|
||||||
int column,
|
int column,
|
||||||
|
@ -73,6 +75,8 @@ void SendBotCallbackData(
|
||||||
if (withPassword) {
|
if (withPassword) {
|
||||||
flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_password;
|
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(
|
button->requestId = api->request(MTPmessages_GetBotCallbackAnswer(
|
||||||
MTP_flags(flags),
|
MTP_flags(flags),
|
||||||
history->peer->input,
|
history->peer->input,
|
||||||
|
@ -100,12 +104,12 @@ void SendBotCallbackData(
|
||||||
|
|
||||||
if (!message.isEmpty()) {
|
if (!message.isEmpty()) {
|
||||||
if (showAlert) {
|
if (showAlert) {
|
||||||
Ui::show(Ui::MakeInformBox(message));
|
show->showBox(Ui::MakeInformBox(message));
|
||||||
} else {
|
} else {
|
||||||
if (withPassword) {
|
if (withPassword) {
|
||||||
Ui::hideLayer();
|
show->hideLayer();
|
||||||
}
|
}
|
||||||
Ui::Toast::Show(message);
|
Ui::Toast::Show(show->toastParent(), message);
|
||||||
}
|
}
|
||||||
} else if (!link.isEmpty()) {
|
} else if (!link.isEmpty()) {
|
||||||
if (!isGame) {
|
if (!isGame) {
|
||||||
|
@ -116,12 +120,18 @@ void SendBotCallbackData(
|
||||||
session,
|
session,
|
||||||
link,
|
link,
|
||||||
item->fullId());
|
item->fullId());
|
||||||
BotGameUrlClickHandler(bot, scoreLink).onClick({});
|
BotGameUrlClickHandler(bot, scoreLink).onClick({
|
||||||
|
Qt::LeftButton,
|
||||||
|
QVariant::fromValue(ClickHandlerContext{
|
||||||
|
.itemId = item->fullId(),
|
||||||
|
.sessionWindow = weak,
|
||||||
|
}),
|
||||||
|
});
|
||||||
session->sendProgressManager().update(
|
session->sendProgressManager().update(
|
||||||
history,
|
history,
|
||||||
Api::SendProgressType::PlayGame);
|
Api::SendProgressType::PlayGame);
|
||||||
} else if (withPassword) {
|
} else if (withPassword) {
|
||||||
Ui::hideLayer();
|
show->hideLayer();
|
||||||
}
|
}
|
||||||
}).fail([=](const MTP::Error &error) {
|
}).fail([=](const MTP::Error &error) {
|
||||||
const auto item = owner->message(fullId);
|
const auto item = owner->message(fullId);
|
||||||
|
@ -147,13 +157,15 @@ void SendBotCallbackData(
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void SendBotCallbackData(
|
void SendBotCallbackData(
|
||||||
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
int row,
|
int row,
|
||||||
int column) {
|
int column) {
|
||||||
SendBotCallbackData(item, row, column, std::nullopt);
|
SendBotCallbackData(controller, item, row, column, std::nullopt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendBotCallbackDataWithPassword(
|
void SendBotCallbackDataWithPassword(
|
||||||
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
int row,
|
int row,
|
||||||
int column) {
|
int column) {
|
||||||
|
@ -177,7 +189,9 @@ void SendBotCallbackDataWithPassword(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
api->cloudPassword().reload();
|
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(
|
auto box = PrePasswordErrorBox(
|
||||||
error,
|
error,
|
||||||
session,
|
session,
|
||||||
|
@ -185,7 +199,7 @@ void SendBotCallbackDataWithPassword(
|
||||||
tr::now,
|
tr::now,
|
||||||
Ui::Text::WithEntities));
|
Ui::Text::WithEntities));
|
||||||
if (box) {
|
if (box) {
|
||||||
Ui::show(std::move(box));
|
show->showBox(std::move(box), Ui::LayerOption::CloseOther);
|
||||||
} else {
|
} else {
|
||||||
auto lifetime = std::make_shared<rpl::lifetime>();
|
auto lifetime = std::make_shared<rpl::lifetime>();
|
||||||
button->requestId = -1;
|
button->requestId = -1;
|
||||||
|
@ -219,14 +233,20 @@ void SendBotCallbackDataWithPassword(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (const auto item = owner->message(fullId)) {
|
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) {
|
if (*box) {
|
||||||
(*box)->handleCustomCheckError(error);
|
(*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);
|
}, *lifetime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,14 +9,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
class HistoryItem;
|
class HistoryItem;
|
||||||
|
|
||||||
|
namespace Window {
|
||||||
|
class SessionController;
|
||||||
|
} // namespace Window
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
|
|
||||||
void SendBotCallbackData(
|
void SendBotCallbackData(
|
||||||
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
int row,
|
int row,
|
||||||
int column);
|
int column);
|
||||||
|
|
||||||
void SendBotCallbackDataWithPassword(
|
void SendBotCallbackDataWithPassword(
|
||||||
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
int row,
|
int row,
|
||||||
int column);
|
int column);
|
||||||
|
|
|
@ -72,7 +72,7 @@ bool insertBotCommand(const QString &cmd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void activateBotCommand(
|
void activateBotCommand(
|
||||||
Window::SessionController *sessionController,
|
not_null<Window::SessionController*> sessionController,
|
||||||
not_null<const HistoryItem*> msg,
|
not_null<const HistoryItem*> msg,
|
||||||
int row,
|
int row,
|
||||||
int column) {
|
int column) {
|
||||||
|
@ -90,20 +90,19 @@ void activateBotCommand(
|
||||||
case ButtonType::Default: {
|
case ButtonType::Default: {
|
||||||
// Copy string before passing it to the sending method
|
// Copy string before passing it to the sending method
|
||||||
// because the original button can be destroyed inside.
|
// because the original button can be destroyed inside.
|
||||||
if (sessionController) {
|
const auto replyTo = msg->isRegular() ? msg->id : 0;
|
||||||
MsgId replyTo = msg->isRegular() ? msg->id : 0;
|
sessionController->content()->sendBotCommand({
|
||||||
sessionController->content()->sendBotCommand({
|
.peer = msg->history()->peer,
|
||||||
.peer = msg->history()->peer,
|
.command = QString(button->text),
|
||||||
.command = QString(button->text),
|
.context = msg->fullId(),
|
||||||
.context = msg->fullId(),
|
.replyTo = replyTo,
|
||||||
.replyTo = replyTo,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ButtonType::Callback:
|
case ButtonType::Callback:
|
||||||
case ButtonType::Game: {
|
case ButtonType::Game: {
|
||||||
Api::SendBotCallbackData(
|
Api::SendBotCallbackData(
|
||||||
|
sessionController,
|
||||||
const_cast<HistoryItem*>(msg.get()),
|
const_cast<HistoryItem*>(msg.get()),
|
||||||
row,
|
row,
|
||||||
column);
|
column);
|
||||||
|
@ -111,6 +110,7 @@ void activateBotCommand(
|
||||||
|
|
||||||
case ButtonType::CallbackWithPassword: {
|
case ButtonType::CallbackWithPassword: {
|
||||||
Api::SendBotCallbackDataWithPassword(
|
Api::SendBotCallbackDataWithPassword(
|
||||||
|
sessionController,
|
||||||
const_cast<HistoryItem*>(msg.get()),
|
const_cast<HistoryItem*>(msg.get()),
|
||||||
row,
|
row,
|
||||||
column);
|
column);
|
||||||
|
@ -120,7 +120,9 @@ void activateBotCommand(
|
||||||
Payments::CheckoutProcess::Start(
|
Payments::CheckoutProcess::Start(
|
||||||
msg,
|
msg,
|
||||||
Payments::Mode::Payment,
|
Payments::Mode::Payment,
|
||||||
crl::guard(App::wnd(), [](auto) { App::wnd()->activate(); }));
|
crl::guard(sessionController, [=](auto) {
|
||||||
|
sessionController->widget()->activate();
|
||||||
|
}));
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ButtonType::Url: {
|
case ButtonType::Url: {
|
||||||
|
@ -140,14 +142,15 @@ void activateBotCommand(
|
||||||
|
|
||||||
case ButtonType::RequestLocation: {
|
case ButtonType::RequestLocation: {
|
||||||
hideSingleUseKeyboard(msg);
|
hideSingleUseKeyboard(msg);
|
||||||
Ui::show(Ui::MakeInformBox(tr::lng_bot_share_location_unavailable()));
|
sessionController->show(
|
||||||
|
Ui::MakeInformBox(tr::lng_bot_share_location_unavailable()));
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ButtonType::RequestPhone: {
|
case ButtonType::RequestPhone: {
|
||||||
hideSingleUseKeyboard(msg);
|
hideSingleUseKeyboard(msg);
|
||||||
const auto msgId = msg->id;
|
const auto msgId = msg->id;
|
||||||
const auto history = msg->history();
|
const auto history = msg->history();
|
||||||
Ui::show(Ui::MakeConfirmBox({
|
sessionController->show(Ui::MakeConfirmBox({
|
||||||
.text = tr::lng_bot_share_phone(),
|
.text = tr::lng_bot_share_phone(),
|
||||||
.confirmed = [=] {
|
.confirmed = [=] {
|
||||||
Ui::showPeerHistory(history, ShowAtTheEndMsgId);
|
Ui::showPeerHistory(history, ShowAtTheEndMsgId);
|
||||||
|
@ -224,24 +227,20 @@ void activateBotCommand(
|
||||||
|
|
||||||
case ButtonType::WebView: {
|
case ButtonType::WebView: {
|
||||||
if (const auto bot = msg->getMessageBot()) {
|
if (const auto bot = msg->getMessageBot()) {
|
||||||
if (sessionController) {
|
bot->session().attachWebView().request(
|
||||||
bot->session().attachWebView().request(
|
sessionController,
|
||||||
sessionController,
|
bot,
|
||||||
bot,
|
bot,
|
||||||
bot,
|
{ .text = button->text, .url = button->data });
|
||||||
{ .text = button->text, .url = button->data });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ButtonType::SimpleWebView: {
|
case ButtonType::SimpleWebView: {
|
||||||
if (const auto bot = msg->getMessageBot()) {
|
if (const auto bot = msg->getMessageBot()) {
|
||||||
if (sessionController) {
|
bot->session().attachWebView().requestSimple(
|
||||||
bot->session().attachWebView().requestSimple(
|
sessionController,
|
||||||
sessionController,
|
bot,
|
||||||
bot,
|
{ .text = button->text, .url = button->data });
|
||||||
{ .text = button->text, .url = button->data });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ template <typename Guard, typename Lambda>
|
||||||
|
|
||||||
bool insertBotCommand(const QString &cmd);
|
bool insertBotCommand(const QString &cmd);
|
||||||
void activateBotCommand(
|
void activateBotCommand(
|
||||||
Window::SessionController *sessionController,
|
not_null<Window::SessionController*> sessionController,
|
||||||
not_null<const HistoryItem*> msg,
|
not_null<const HistoryItem*> msg,
|
||||||
int row,
|
int row,
|
||||||
int column);
|
int column);
|
||||||
|
|
Loading…
Add table
Reference in a new issue