diff --git a/Telegram/SourceFiles/data/components/credits.cpp b/Telegram/SourceFiles/data/components/credits.cpp index 5ae899d77e..119847e0fc 100644 --- a/Telegram/SourceFiles/data/components/credits.cpp +++ b/Telegram/SourceFiles/data/components/credits.cpp @@ -47,10 +47,23 @@ void Credits::load(bool force) { && _lastLoaded + kReloadThreshold > crl::now())) { return; } - _loader = std::make_unique(_session->user()); - _loader->request({}, [=](Data::CreditsStatusSlice slice) { - _loader = nullptr; - apply(slice.balance); + const auto self = _session->user(); + _loader = std::make_unique(); + _loader->make_state(self)->request({}, [=]( + Data::CreditsStatusSlice slice) { + const auto balance = slice.balance; + const auto apiStats + = _loader->make_state(self); + const auto finish = [=](bool statsEnabled) { + _statsEnabled = statsEnabled; + apply(balance); + _loader = nullptr; + }; + apiStats->request() | rpl::start_with_error_done([=] { + finish(false); + }, [=] { + finish(true); + }, *_loader); }); } @@ -148,4 +161,8 @@ rpl::producer<> Credits::refreshedByPeerId(PeerId peerId) { ) | rpl::filter(rpl::mappers::_1 == peerId) | rpl::to_empty; } +bool Credits::statsEnabled() const { + return _statsEnabled; +} + } // namespace Data diff --git a/Telegram/SourceFiles/data/components/credits.h b/Telegram/SourceFiles/data/components/credits.h index e2d719091c..dac6df8981 100644 --- a/Telegram/SourceFiles/data/components/credits.h +++ b/Telegram/SourceFiles/data/components/credits.h @@ -7,10 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -namespace Api { -class CreditsStatus; -} // namespace Api - namespace Main { class Session; } // namespace Main @@ -39,6 +35,8 @@ public: [[nodiscard]] rpl::producer<> refreshedByPeerId(PeerId peerId); + [[nodiscard]] bool statsEnabled() const; + void applyCurrency(PeerId peerId, uint64 balance); [[nodiscard]] uint64 balanceCurrency(PeerId peerId) const; @@ -54,7 +52,7 @@ private: const not_null _session; - std::unique_ptr _loader; + std::unique_ptr _loader; base::flat_map _cachedPeerBalances; base::flat_map _cachedPeerCurrencyBalances; @@ -66,6 +64,8 @@ private: crl::time _lastLoaded = 0; float64 _rate = 0.; + bool _statsEnabled = false; + rpl::event_stream _refreshedByPeerId; SingleQueuedInvokation _reload;