From 7d9b999cb0dbfdebad3cc9722e949db4ac4a8a96 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 21 Dec 2022 12:06:54 +0400 Subject: [PATCH] Support request_write_access flag in attach bots. --- .../inline_bots/bot_attach_web_view.cpp | 74 +++++++++++++------ .../inline_bots/bot_attach_web_view.h | 8 +- 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp index 7601ef5626..a999a05a27 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/boxes/confirm_box.h" #include "ui/toasts/common_toasts.h" #include "ui/chat/attach/attach_bot_webview.h" +#include "ui/widgets/checkbox.h" #include "ui/widgets/dropdown_menu.h" #include "ui/widgets/popup_menu.h" #include "ui/widgets/menu/menu_item_base.h" @@ -41,6 +42,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/random.h" #include "base/timer_rpl.h" #include "apiwrap.h" +#include "styles/style_boxes.h" #include "styles/style_menu_icons.h" #include @@ -121,6 +123,7 @@ struct ParsedBot { .types = ResolvePeerTypes(data.vpeer_types().v), .inactive = data.is_inactive(), .hasSettings = data.is_has_settings(), + .requestWriteAccess = data.is_request_write_access(), } : std::optional(); }); if (result && result->icon) { @@ -636,7 +639,7 @@ void AttachWebView::requestAddToMenu( } void AttachWebView::removeFromMenu(not_null bot) { - toggleInMenu(bot, false, [=] { + toggleInMenu(bot, ToggledState::Removed, [=] { Ui::ShowMultilineToast({ .text = { tr::lng_bot_remove_from_menu_done(tr::now) }, }); @@ -965,39 +968,66 @@ void AttachWebView::started(uint64 queryId) { void AttachWebView::confirmAddToMenu( AttachWebViewBot bot, Fn callback) { - const auto done = [=](Fn close) { - toggleInMenu(bot.user, true, [=] { - if (callback) { - callback(); - } - Ui::ShowMultilineToast({ - .text = { tr::lng_bot_add_to_menu_done(tr::now) }, - }); - }); - close(); - }; const auto active = Core::App().activeWindow(); if (!active) { return; } - _confirmAddBox = active->show(Ui::MakeConfirmBox({ - tr::lng_bot_add_to_menu( - tr::now, - lt_bot, - Ui::Text::Bold(bot.name), - Ui::Text::WithEntities), - done, + _confirmAddBox = active->show(Box([=](not_null box) { + const auto allowed = std::make_shared(); + const auto done = [=](Fn close) { + const auto state = ((*allowed) && (*allowed)->checked()) + ? ToggledState::AllowedToWrite + : ToggledState::Added; + toggleInMenu(bot.user, state, [=] { + if (callback) { + callback(); + } + Ui::ShowMultilineToast({ + .text = { tr::lng_bot_add_to_menu_done(tr::now) }, + }); + }); + close(); + }; + Ui::ConfirmBox(box, { + tr::lng_bot_add_to_menu( + tr::now, + lt_bot, + Ui::Text::Bold(bot.name), + Ui::Text::WithEntities), + done, + }); + if (bot.requestWriteAccess) { + (*allowed) = box->addRow( + object_ptr( + box, + tr::lng_url_auth_allow_messages( + tr::now, + lt_bot, + Ui::Text::Bold(bot.name), + Ui::Text::WithEntities), + true, + st::urlAuthCheckbox), + style::margins( + st::boxRowPadding.left(), + st::boxPhotoCaptionSkip, + st::boxRowPadding.right(), + st::boxPhotoCaptionSkip)); + (*allowed)->setAllowTextLines(); + } })); } void AttachWebView::toggleInMenu( not_null bot, - bool enabled, + ToggledState state, Fn callback) { + using Flag = MTPmessages_ToggleBotInAttachMenu::Flag; _session->api().request(MTPmessages_ToggleBotInAttachMenu( - MTP_flags(0), + MTP_flags((state == ToggledState::AllowedToWrite) + ? Flag::f_write_allowed + : Flag()), bot->inputUser, - MTP_bool(enabled) + MTP_bool(state != ToggledState::Removed) )).done([=] { _requestId = 0; requestBots(); diff --git a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h index 2fb9545e55..81d6a824f2 100644 --- a/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h +++ b/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h @@ -58,6 +58,7 @@ struct AttachWebViewBot { PeerTypes types = 0; bool inactive = false; bool hasSettings = false; + bool requestWriteAccess = false; }; class AttachWebView final : public base::has_weak_ptr { @@ -120,9 +121,14 @@ private: not_null controller, Fn done); + enum class ToggledState { + Removed, + Added, + AllowedToWrite, + }; void toggleInMenu( not_null bot, - bool enabled, + ToggledState state, Fn callback = nullptr); void show(