From a2785867b2634ae65ce637f0e3ec75bf6e34b9ed Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 13 Aug 2024 14:35:12 +0200 Subject: [PATCH] Support correct paid-media-from-bot phrases. --- Telegram/Resources/langs/lang.strings | 4 +++ .../SourceFiles/boxes/send_credits_box.cpp | 16 ++++++--- .../SourceFiles/data/components/credits.cpp | 34 +++++++++---------- .../SourceFiles/data/components/credits.h | 7 ++-- .../view/media/history_view_media_common.cpp | 28 +++++++++++---- 5 files changed, 59 insertions(+), 30 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 2b765f110..ca7b89756 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2376,6 +2376,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_credits_box_out_sure#other" = "Do you want to buy **\"{text}\"** in **{bot}** for **{count} Stars**?"; "lng_credits_box_out_media#one" = "Do you want to unlock {media} in {chat} for **{count} Star**?"; "lng_credits_box_out_media#other" = "Do you want to unlock {media} in {chat} for **{count} Stars**?"; +"lng_credits_box_out_media_user#one" = "Do you want to unlock {media} from {user} for **{count} Star**?"; +"lng_credits_box_out_media_user#other" = "Do you want to unlock {media} from {user} for **{count} Stars**?"; "lng_credits_box_out_photo" = "a photo"; "lng_credits_box_out_photos#one" = "{count} photo"; "lng_credits_box_out_photos#other" = "{count} photos"; @@ -2390,6 +2392,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_credits_media_done_title" = "Media Unlocked"; "lng_credits_media_done_text#one" = "**{count} Star** transferred to {chat}."; "lng_credits_media_done_text#other" = "**{count} Stars** transferred to {chat}."; +"lng_credits_media_done_text_user#one" = "**{count} Star** transferred to {user}."; +"lng_credits_media_done_text_user#other" = "**{count} Stars** transferred to {user}."; "lng_credits_summary_in_toast_title" = "Stars Acquired"; "lng_credits_summary_in_toast_about#one" = "**{count}** Star added to your balance."; "lng_credits_summary_in_toast_about#other" = "**{count}** Stars added to your balance."; diff --git a/Telegram/SourceFiles/boxes/send_credits_box.cpp b/Telegram/SourceFiles/boxes/send_credits_box.cpp index 61b3b8952..efa5d46cf 100644 --- a/Telegram/SourceFiles/boxes/send_credits_box.cpp +++ b/Telegram/SourceFiles/boxes/send_credits_box.cpp @@ -81,14 +81,12 @@ struct PaidMediaData { } } + const auto bot = item->viaBot(); const auto sender = item->originalSender(); - const auto broadcast = (sender && sender->isBroadcast()) - ? sender - : message->peer.get(); return { .invoice = invoice, .item = item, - .peer = broadcast, + .peer = (bot ? bot : sender ? sender : message->peer.get()), .photos = photos, .videos = videos, }; @@ -131,6 +129,16 @@ struct PaidMediaData { lt_video, std::move(videosBold), Ui::Text::WithEntities); + if (const auto user = data.peer->asUser()) { + return tr::lng_credits_box_out_media_user( + lt_count, + rpl::single(form->invoice.amount) | tr::to_count(), + lt_media, + std::move(media), + lt_user, + rpl::single(Ui::Text::Bold(user->shortName())), + Ui::Text::RichLangValue); + } return tr::lng_credits_box_out_media( lt_count, rpl::single(form->invoice.amount) | tr::to_count(), diff --git a/Telegram/SourceFiles/data/components/credits.cpp b/Telegram/SourceFiles/data/components/credits.cpp index 3ada4a1fb..016d8ef4a 100644 --- a/Telegram/SourceFiles/data/components/credits.cpp +++ b/Telegram/SourceFiles/data/components/credits.cpp @@ -81,43 +81,42 @@ rpl::producer Credits::loadedValue() const { } uint64 Credits::balance() const { - const auto balance = _balance.current(); - const auto locked = _locked.current(); - return (balance >= locked) ? (balance - locked) : 0; + return _nonLockedBalance.current(); } rpl::producer Credits::balanceValue() const { - return rpl::combine( - _balance.value(), - _locked.value() - ) | rpl::map([=](uint64 balance, uint64 locked) { - return (balance >= locked) ? (balance - locked) : 0; - }); + return _nonLockedBalance.value(); +} + +void Credits::updateNonLockedValue() { + _nonLockedBalance = (_balance >= _locked) ? (_balance - _locked) : 0; } void Credits::lock(int count) { Expects(loaded()); Expects(count >= 0); + Expects(_locked + count <= _balance); - _locked = _locked.current() + count; + _locked += count; - Ensures(_locked.current() <= _balance.current()); + updateNonLockedValue(); } void Credits::unlock(int count) { Expects(count >= 0); - Expects(_locked.current() >= count); + Expects(_locked >= count); - _locked = _locked.current() - count; + _locked -= count; + + updateNonLockedValue(); } void Credits::withdrawLocked(int count) { Expects(count >= 0); - Expects(_locked.current() >= count); + Expects(_locked >= count); - const auto balance = _balance.current(); - _locked = _locked.current() - count; - apply(balance >= count ? (balance - count) : 0); + _locked -= count; + apply(_balance >= count ? (_balance - count) : 0); invalidate(); } @@ -127,6 +126,7 @@ void Credits::invalidate() { void Credits::apply(uint64 balance) { _balance = balance; + updateNonLockedValue(); const auto was = std::exchange(_lastLoaded, crl::now()); if (!was) { diff --git a/Telegram/SourceFiles/data/components/credits.h b/Telegram/SourceFiles/data/components/credits.h index 312d80ba5..bd5a2f725 100644 --- a/Telegram/SourceFiles/data/components/credits.h +++ b/Telegram/SourceFiles/data/components/credits.h @@ -41,12 +41,15 @@ public: void apply(const MTPDupdateStarsBalance &data); private: + void updateNonLockedValue(); + const not_null _session; std::unique_ptr _loader; - rpl::variable _balance; - rpl::variable _locked; + uint64 _balance = 0; + uint64 _locked = 0; + rpl::variable _nonLockedBalance; rpl::event_stream<> _loadedChanges; crl::time _lastLoaded = 0; float64 _rate = 0.; diff --git a/Telegram/SourceFiles/history/view/media/history_view_media_common.cpp b/Telegram/SourceFiles/history/view/media/history_view_media_common.cpp index 71b01fbf8..245f4ceb9 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_media_common.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_media_common.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_wall_paper.h" #include "data/data_media_types.h" +#include "data/data_user.h" #include "history/view/history_view_element.h" #include "history/view/media/history_view_media_grouped.h" #include "history/view/media/history_view_photo.h" @@ -210,16 +211,29 @@ void ShowPaidMediaUnlockedToast( const auto broadcast = (sender && sender->isBroadcast()) ? sender : item->history()->peer.get(); + const auto user = item->viaBot() + ? item->viaBot() + : item->originalSender() + ? item->originalSender()->asUser() + : nullptr; auto text = tr::lng_credits_media_done_title( tr::now, Ui::Text::Bold - ).append('\n').append(tr::lng_credits_media_done_text( - tr::now, - lt_count, - invoice->amount, - lt_chat, - Ui::Text::Bold(broadcast->name()), - Ui::Text::RichLangValue)); + ).append('\n').append(user + ? tr::lng_credits_media_done_text_user( + tr::now, + lt_count, + invoice->amount, + lt_user, + Ui::Text::Bold(user->shortName()), + Ui::Text::RichLangValue) + : tr::lng_credits_media_done_text( + tr::now, + lt_count, + invoice->amount, + lt_chat, + Ui::Text::Bold(broadcast->name()), + Ui::Text::RichLangValue)); controller->showToast(std::move(text), kMediaUnlockedTooltipDuration); }