From 9706b84409c5c92152c76f5400de0f4a9399f105 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 28 Nov 2021 23:16:53 +0300 Subject: [PATCH] Slightly refactored api_bot. --- Telegram/SourceFiles/api/api_bot.cpp | 77 +++++++++++++++------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index 5c463ab44..4bf5b79fb 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -34,8 +34,8 @@ void SendBotCallbackData( not_null item, int row, int column, - std::optional password = std::nullopt, - Fn handleError = nullptr) { + std::optional password = std::nullopt, + Fn handleError = nullptr) { if (!item->isRegular()) { return; } @@ -78,7 +78,7 @@ void SendBotCallbackData( history->peer->input, MTP_int(item->id), MTP_bytes(sendData), - password.value_or(MTP_inputCheckPasswordEmpty()) + password ? password->result : MTP_inputCheckPasswordEmpty() )).done([=](const MTPmessages_BotCallbackAnswer &result) { const auto item = owner->message(fullId); if (!item) { @@ -88,34 +88,41 @@ void SendBotCallbackData( button->requestId = 0; owner->requestItemRepaint(item); } - result.match([&](const MTPDmessages_botCallbackAnswer &data) { - if (const auto message = data.vmessage()) { - if (data.is_alert()) { - Ui::show(Box(qs(*message))); - } else { - if (withPassword) { - Ui::hideLayer(); - } - Ui::Toast::Show(qs(*message)); - } - } else if (const auto url = data.vurl()) { - const auto link = qs(*url); - if (!isGame) { - UrlClickHandler::Open(link); - return; - } - const auto scoreLink = AppendShareGameScoreUrl( - session, - link, - item->fullId()); - BotGameUrlClickHandler(bot, scoreLink).onClick({}); - session->sendProgressManager().update( - history, - Api::SendProgressType::PlayGame); - } else if (withPassword) { - Ui::hideLayer(); - } + const auto &data = result.match([]( + const auto &data) -> const MTPDmessages_botCallbackAnswer& { + return data; }); + const auto message = data.vmessage() + ? qs(*data.vmessage()) + : QString(); + const auto link = data.vurl() ? qs(*data.vurl()) : QString(); + const auto showAlert = data.is_alert(); + + if (!message.isEmpty()) { + if (showAlert) { + Ui::show(Box(message)); + } else { + if (withPassword) { + Ui::hideLayer(); + } + Ui::Toast::Show(message); + } + } else if (!link.isEmpty()) { + if (!isGame) { + UrlClickHandler::Open(link); + return; + } + const auto scoreLink = AppendShareGameScoreUrl( + session, + link, + item->fullId()); + BotGameUrlClickHandler(bot, scoreLink).onClick({}); + session->sendProgressManager().update( + history, + Api::SendProgressType::PlayGame); + } else if (withPassword) { + Ui::hideLayer(); + } }).fail([=](const MTP::Error &error) { const auto item = owner->message(fullId); if (!item) { @@ -127,7 +134,7 @@ void SendBotCallbackData( owner->requestItemRepaint(item); } if (handleError) { - handleError(error); + handleError(error.type()); } }).send(); @@ -143,7 +150,7 @@ void SendBotCallbackData( not_null item, int row, int column) { - SendBotCallbackData(item, row, column, MTP_inputCheckPasswordEmpty()); + SendBotCallbackData(item, row, column, std::nullopt); } void SendBotCallbackDataWithPassword( @@ -170,9 +177,9 @@ void SendBotCallbackDataWithPassword( return; } api->cloudPassword().reload(); - SendBotCallbackData(item, row, column, MTP_inputCheckPasswordEmpty(), [=](const MTP::Error &error) { + SendBotCallbackData(item, row, column, std::nullopt, [=](const QString &error) { auto box = PrePasswordErrorBox( - error.type(), + error, session, tr::lng_bots_password_confirm_check_about( tr::now, @@ -212,7 +219,7 @@ void SendBotCallbackDataWithPassword( return; } if (const auto item = owner->message(fullId)) { - SendBotCallbackData(item, row, column, result.result, [=](const MTP::Error &error) { + SendBotCallbackData(item, row, column, result, [=](const QString &error) { if (*box) { (*box)->handleCustomCheckError(error); }