From d56f3cfecf408bc5af50e344aa905d46967d37cd Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 3 Oct 2024 23:17:03 +0300 Subject: [PATCH] Added credits balance to profile section of owned bots. --- Telegram/SourceFiles/info/info.style | 1 + .../info/profile/info_profile_actions.cpp | 64 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/info/info.style b/Telegram/SourceFiles/info/info.style index ac90070da..fb096b94c 100644 --- a/Telegram/SourceFiles/info/info.style +++ b/Telegram/SourceFiles/info/info.style @@ -459,6 +459,7 @@ infoProfileLabeledButtonQr: IconButton(infoProfileLabeledButtonCopy) { infoIconInformation: icon {{ "info/info_information", infoIconFg }}; infoIconAddMember: icon {{ "info/info_add_member", infoIconFg }}; +infoIconBotBalance: icon {{ "menu/earn", infoIconFg, point(5px, 5px) }}; infoIconNotifications: icon {{ "info/info_notifications", infoIconFg }}; infoIconMediaPhoto: icon {{ "info/info_media_photo", infoIconFg }}; infoIconMediaVideo: icon {{ "info/info_media_video", infoIconFg }}; diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 94b034d0a..3e016c0a3 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_blocked_peers.h" #include "api/api_chat_participants.h" +#include "api/api_credits.h" #include "apiwrap.h" #include "base/options.h" #include "base/timer_rpl.h" @@ -21,8 +22,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/translate_box.h" #include "core/application.h" #include "core/click_handler_types.h" +#include "core/ui_integration.h" #include "data/business/data_business_common.h" #include "data/business/data_business_info.h" +#include "data/components/credits.h" #include "data/data_changes.h" #include "data/data_channel.h" #include "data/data_chat.h" @@ -32,12 +35,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_user.h" #include "data/notify/data_notify_settings.h" +#include "data/stickers/data_custom_emoji.h" #include "dialogs/ui/dialogs_layout.h" #include "dialogs/ui/dialogs_message_view.h" #include "history/history.h" #include "history/history_item.h" #include "history/history_item_helpers.h" #include "history/view/history_view_item_preview.h" +#include "info/bot/earn/info_bot_earn_widget.h" #include "info/info_controller.h" #include "info/info_memento.h" #include "info/profile/info_profile_icon.h" @@ -75,6 +80,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_info.h" #include "styles/style_layers.h" #include "styles/style_menu_icons.h" +#include "styles/style_settings.h" // settingsButtonRightSkip. #include #include @@ -825,6 +831,7 @@ public: private: void addInviteToGroupAction(not_null user); + void addCreditsAction(not_null user); void addShareContactAction(not_null user); void addEditContactAction(not_null user); void addDeleteContactAction(not_null user); @@ -1830,8 +1837,7 @@ ActionsFiller::ActionsFiller( , _peer(peer) { } -void ActionsFiller::addInviteToGroupAction( - not_null user) { +void ActionsFiller::addInviteToGroupAction(not_null user) { const auto notEmpty = [](const QString &value) { return !value.isEmpty(); }; @@ -1855,6 +1861,59 @@ void ActionsFiller::addInviteToGroupAction( about->finishAnimating(); } +void ActionsFiller::addCreditsAction(not_null user) { + struct State final { + rpl::variable balance; + }; + const auto state = _wrap->lifetime().make_state(); + const auto controller = _controller->parentController(); + const auto wrap = AddActionButton( + _wrap, + tr::lng_manage_peer_bot_balance(), + state->balance.value() | rpl::map(rpl::mappers::_1 > 0), + [=] { controller->showSection(Info::BotEarn::Make(user)); }, + &st::infoIconBotBalance); + if (const auto balance = user->session().credits().balance(user->id)) { + state->balance = balance; + } + { + const auto api = _wrap->lifetime().make_state( + user); + api->request({}, [=](Data::CreditsStatusSlice data) { + state->balance = data.balance; + }); + } + const auto &st = st::infoSharedMediaButton; + const auto button = wrap->entity(); + const auto name = Ui::CreateChild(button, st.rightLabel); + name->show(); + rpl::combine( + button->widthValue(), + tr::lng_manage_peer_bot_balance(), + state->balance.value() + ) | rpl::start_with_next([=, &st]( + int width, + const QString &button, + uint64 balance) { + const auto available = width + - rect::m::sum::h(st.padding) + - st.style.font->width(button) + - st::settingsButtonRightSkip; + name->setMarkedText( + user->owner().customEmojiManager().creditsEmoji() + .append(QChar(' ')) + .append(QString::number(balance)), + Core::MarkedTextContext{ + .session = &user->session(), + .customEmojiRepaint = [=] { name->update(); }, + }); + name->resizeToNaturalWidth(available); + name->moveToRight(st::settingsButtonRightSkip, st.padding.top()); + }, name->lifetime()); + name->setAttribute(Qt::WA_TransparentForMouseEvents); + wrap->finishAnimating(); +} + void ActionsFiller::addShareContactAction(not_null user) { const auto controller = _controller->parentController(); AddActionButton( @@ -2077,6 +2136,7 @@ void ActionsFiller::addJoinChannelAction( void ActionsFiller::fillUserActions(not_null user) { if (user->isBot()) { + addCreditsAction(user); addInviteToGroupAction(user); } addShareContactAction(user);