From 2f07bb397300d568408845488f930f34284032cc Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 27 Mar 2024 23:36:29 +0300 Subject: [PATCH] Added initial support of withdraw button in channel earn info section. --- Telegram/CMakeLists.txt | 2 + Telegram/Resources/langs/lang.strings | 6 +- Telegram/SourceFiles/api/api_earn.cpp | 88 +++++++++++++++++++ Telegram/SourceFiles/api/api_earn.h | 29 ++++++ .../data/data_sponsored_messages.cpp | 18 ---- .../data/data_sponsored_messages.h | 10 --- .../earn/info_earn_inner_widget.cpp | 30 +++++-- 7 files changed, 146 insertions(+), 37 deletions(-) create mode 100644 Telegram/SourceFiles/api/api_earn.cpp create mode 100644 Telegram/SourceFiles/api/api_earn.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 9ec116fd3..4988f34b3 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -120,6 +120,8 @@ PRIVATE api/api_common.h api/api_confirm_phone.cpp api/api_confirm_phone.h + api/api_earn.cpp + api/api_earn.h api/api_editing.cpp api/api_editing.h api/api_global_privacy.cpp diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index be00ea9c9..47f5d390b 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -4979,9 +4979,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_channel_earn_reward" = "Proceeds since last withdrawal"; "lng_channel_earn_total" = "Total lifetime proceeds"; "lng_channel_earn_balance_title" = "Available balance"; -"lng_channel_earn_balance_placeholder" = "Enter your TON address"; "lng_channel_earn_balance_button" = "Withdraw"; -"lng_channel_earn_balance_about" = "By the end of May, you will be able to collect your reward using Fragment, a third-party platform used by the advertiser to pay for the ad. {link}"; +"lng_channel_earn_balance_password_title" = "Two-step verification"; +"lng_channel_earn_balance_password_description" = "Please enter your password to withdraw."; +"lng_channel_earn_balance_about" = "Collect your reward using Fragment, a third-party platform used by the advertiser to pay for the ad. {link}"; +"lng_channel_earn_balance_about_temp" = "By the end of May, you will be able to collect your reward using Fragment, a third-party platform used by the advertiser to pay for the ad. {link}"; "lng_channel_earn_transfer_sure_about1" = "Check the address of the recipient:"; "lng_channel_earn_transfer_sure_about2" = "This action can not be undone. If the address above is incorrect you will lose your TON."; "lng_channel_earn_history_title" = "Transaction history"; diff --git a/Telegram/SourceFiles/api/api_earn.cpp b/Telegram/SourceFiles/api/api_earn.cpp new file mode 100644 index 000000000..e8f38e06b --- /dev/null +++ b/Telegram/SourceFiles/api/api_earn.cpp @@ -0,0 +1,88 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "api/api_earn.h" + +#include "api/api_cloud_password.h" +#include "apiwrap.h" +#include "boxes/passcode_box.h" +#include "data/data_channel.h" +#include "data/data_session.h" +#include "lang/lang_keys.h" +#include "main/main_session.h" +#include "ui/basic_click_handlers.h" +#include "ui/widgets/buttons.h" + +namespace Api { + +void RestrictSponsored( + not_null channel, + bool restricted, + Fn failed) { + channel->session().api().request(MTPchannels_RestrictSponsoredMessages( + channel->inputChannel, + MTP_bool(restricted)) + ).done([=](const MTPUpdates &updates) { + channel->session().api().applyUpdates(updates); + }).fail([=](const MTP::Error &error) { + failed(error.type()); + }).send(); +} + +void HandleWithdrawalButton( + not_null channel, + not_null button, + std::shared_ptr show) { + struct State { + rpl::lifetime lifetime; + bool loading = false; + }; + + const auto state = button->lifetime().make_state(); + const auto session = &channel->session(); + + session->api().cloudPassword().reload(); + button->setClickedCallback([=] { + if (state->loading) { + return; + } + state->loading = true; + state->lifetime = session->api().cloudPassword().state( + ) | rpl::take( + 1 + ) | rpl::start_with_next([=](const Core::CloudPasswordState &pass) { + state->loading = false; + + auto fields = PasscodeBox::CloudFields::From(pass); + fields.customTitle = + tr::lng_channel_earn_balance_password_title(); + fields.customDescription = + tr::lng_channel_earn_balance_password_description(tr::now); + fields.customSubmitButton = tr::lng_passcode_submit(); + fields.customCheckCallback = crl::guard(button, [=]( + const Core::CloudPasswordResult &result) { + session->api().request( + MTPstats_GetBroadcastRevenueWithdrawalUrl( + channel->inputChannel, + result.result + )).done([=](const MTPstats_BroadcastRevenueWithdrawalUrl &r) { + const auto url = qs(r.data().vurl()); + + if (!url.isEmpty()) { + UrlClickHandler::Open(url); + } + }).fail([=](const MTP::Error &error) { + show->showToast(error.type()); + }).send(); + }); + show->show(Box(session, fields)); + }); + + }); +} + +} // namespace Api diff --git a/Telegram/SourceFiles/api/api_earn.h b/Telegram/SourceFiles/api/api_earn.h new file mode 100644 index 000000000..93f2bf6eb --- /dev/null +++ b/Telegram/SourceFiles/api/api_earn.h @@ -0,0 +1,29 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +class ChannelData; + +namespace Ui { +class RippleButton; +class Show; +} // namespace Ui + +namespace Api { + +void RestrictSponsored( + not_null channel, + bool restricted, + Fn failed); + +void HandleWithdrawalButton( + not_null channel, + not_null button, + std::shared_ptr show); + +} // namespace Api diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.cpp b/Telegram/SourceFiles/data/data_sponsored_messages.cpp index a88111fbe..ae37e9191 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.cpp +++ b/Telegram/SourceFiles/data/data_sponsored_messages.cpp @@ -566,21 +566,3 @@ SponsoredMessages::State SponsoredMessages::state( } } // namespace Data - -namespace Api { - -void RestrictSponsored( - not_null channel, - bool restricted, - Fn failed) { - channel->session().api().request(MTPchannels_RestrictSponsoredMessages( - channel->inputChannel, - MTP_bool(restricted)) - ).done([=](const MTPUpdates &updates) { - channel->session().api().applyUpdates(updates); - }).fail([=](const MTP::Error &error) { - failed(error.type()); - }).send(); -} - -} // namespace Api diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.h b/Telegram/SourceFiles/data/data_sponsored_messages.h index e124dd1d4..04f0cd196 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.h +++ b/Telegram/SourceFiles/data/data_sponsored_messages.h @@ -158,13 +158,3 @@ private: }; } // namespace Data - -namespace Api { - -void RestrictSponsored( - not_null channel, - bool restricted, - Fn failed); - -} // namespace Api - diff --git a/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp b/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp index 44f124c0b..5a961819a 100644 --- a/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp +++ b/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/channel_statistics/earn/info_earn_inner_widget.h" +#include "api/api_earn.h" #include "api/api_statistics.h" #include "base/random.h" #include "base/unixtime.h" @@ -17,13 +18,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_peer.h" #include "data/data_premium_limits.h" #include "data/data_session.h" -#include "data/data_sponsored_messages.h" #include "data/stickers/data_custom_emoji.h" #include "info/channel_statistics/earn/info_earn_widget.h" #include "info/info_controller.h" #include "info/profile/info_profile_values.h" // Info::Profile::NameValue. #include "info/statistics/info_statistics_inner_widget.h" // FillLoading. #include "lang/lang_keys.h" +#include "main/main_app_config.h" #include "main/main_session.h" #include "statistics/chart_widget.h" #include "ui/boxes/boost_box.h" @@ -36,6 +37,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/text/text_utilities.h" #include "ui/vertical_list.h" #include "ui/widgets/continuous_sliders.h" +#include "main/main_account.h" +#include "main/main_app_config.h" #include "ui/widgets/fields/input_field.h" #include "ui/wrap/slide_wrap.h" #include "styles/style_boxes.h" @@ -97,6 +100,11 @@ constexpr auto kDot = QChar('.'); + MinorPart(result); } +[[nodiscard]] bool WithdrawalEnabled(not_null session) { + const auto key = u"channel_revenue_withdrawal_enabled"_q; + return session->appConfig().get(key, false); +} + void AddHeader( not_null content, tr::phrase<> text) { @@ -211,6 +219,8 @@ void InnerWidget::fill() { const auto multiplier = data.usdRate; const auto session = &_peer->session(); + const auto channel = _peer->asChannel(); + const auto withdrawalEnabled = WithdrawalEnabled(session); const auto makeContext = [=](not_null l) { return Core::MarkedTextContext{ .session = session, @@ -511,7 +521,7 @@ void InnerWidget::fill() { Ui::AddSkip(container); Ui::AddDivider(container); Ui::AddSkip(container); - { + if (channel) { const auto value = data.availableBalance; Ui::AddSkip(container); AddHeader(container, tr::lng_channel_earn_balance_title); @@ -591,15 +601,21 @@ void InnerWidget::fill() { stButton.textFg->c, anim::interpolateF(.5, 1., value))); }; - colorText(1.); + colorText(withdrawalEnabled ? 1. : 0.); +#ifndef _DEBUG + button->setAttribute( + Qt::WA_TransparentForMouseEvents, + !withdrawalEnabled); +#endif - button->setClickedCallback([=] { - }); + Api::HandleWithdrawalButton(channel, button, _controller->uiShow()); Ui::ToggleChildrenVisibility(button, true); Ui::AddSkip(container); Ui::AddSkip(container); - addAboutWithLearn(tr::lng_channel_earn_balance_about); + addAboutWithLearn(withdrawalEnabled + ? tr::lng_channel_earn_balance_about + : tr::lng_channel_earn_balance_about_temp); Ui::AddSkip(container); } Ui::AddSkip(container); @@ -844,7 +860,7 @@ void InnerWidget::fill() { Ui::AddSkip(container); Ui::AddDivider(container); Ui::AddSkip(container); - if (const auto channel = _peer->asChannel()) { + if (channel) { constexpr auto kMaxCPM = 50; // Debug. const auto requiredLevel = Data::LevelLimits(session) .channelRestrictSponsoredLevelMin();