credits.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. namespace Api {
  9. class CreditsStatus;
  10. } // namespace Api
  11. namespace Main {
  12. class Session;
  13. } // namespace Main
  14. namespace Data {
  15. [[nodiscard]] StarsAmount FromTL(const MTPStarsAmount &value);
  16. class Credits final {
  17. public:
  18. explicit Credits(not_null<Main::Session*> session);
  19. ~Credits();
  20. void load(bool force = false);
  21. void apply(StarsAmount balance);
  22. void apply(PeerId peerId, StarsAmount balance);
  23. [[nodiscard]] bool loaded() const;
  24. [[nodiscard]] rpl::producer<bool> loadedValue() const;
  25. [[nodiscard]] StarsAmount balance() const;
  26. [[nodiscard]] StarsAmount balance(PeerId peerId) const;
  27. [[nodiscard]] rpl::producer<StarsAmount> balanceValue() const;
  28. [[nodiscard]] rpl::producer<float64> rateValue(
  29. not_null<PeerData*> ownedBotOrChannel);
  30. void applyCurrency(PeerId peerId, uint64 balance);
  31. [[nodiscard]] uint64 balanceCurrency(PeerId peerId) const;
  32. void lock(StarsAmount count);
  33. void unlock(StarsAmount count);
  34. void withdrawLocked(StarsAmount count);
  35. void invalidate();
  36. void apply(const MTPDupdateStarsBalance &data);
  37. private:
  38. void updateNonLockedValue();
  39. const not_null<Main::Session*> _session;
  40. std::unique_ptr<Api::CreditsStatus> _loader;
  41. base::flat_map<PeerId, StarsAmount> _cachedPeerBalances;
  42. base::flat_map<PeerId, uint64> _cachedPeerCurrencyBalances;
  43. StarsAmount _balance;
  44. StarsAmount _locked;
  45. rpl::variable<StarsAmount> _nonLockedBalance;
  46. rpl::event_stream<> _loadedChanges;
  47. crl::time _lastLoaded = 0;
  48. float64 _rate = 0.;
  49. SingleQueuedInvokation _reload;
  50. };
  51. } // namespace Data