api_premium_option.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include "data/data_premium_subscription_option.h"
  9. namespace Api {
  10. [[nodiscard]] Data::PremiumSubscriptionOption CreateSubscriptionOption(
  11. int months,
  12. int monthlyAmount,
  13. int64 amount,
  14. const QString &currency,
  15. const QString &botUrl);
  16. template<typename Option>
  17. [[nodiscard]] auto PremiumSubscriptionOptionsFromTL(
  18. const QVector<Option> &tlOpts) -> Data::PremiumSubscriptionOptions {
  19. if (tlOpts.isEmpty()) {
  20. return {};
  21. }
  22. auto monthlyAmountPerCurrency = base::flat_map<QString, int>();
  23. auto result = Data::PremiumSubscriptionOptions();
  24. const auto monthlyAmount = [&](const QString &currency) -> int {
  25. const auto it = monthlyAmountPerCurrency.find(currency);
  26. if (it != end(monthlyAmountPerCurrency)) {
  27. return it->second;
  28. }
  29. const auto &min = ranges::min_element(
  30. tlOpts,
  31. ranges::less(),
  32. [&](const Option &o) {
  33. return currency == qs(o.data().vcurrency())
  34. ? o.data().vamount().v
  35. : std::numeric_limits<int64_t>::max();
  36. }
  37. )->data();
  38. const auto monthly = min.vamount().v / float64(min.vmonths().v);
  39. monthlyAmountPerCurrency.emplace(currency, monthly);
  40. return monthly;
  41. };
  42. result.reserve(tlOpts.size());
  43. for (const auto &tlOption : tlOpts) {
  44. const auto &option = tlOption.data();
  45. auto botUrl = QString();
  46. if constexpr (!std::is_same_v<Option, MTPPremiumGiftCodeOption>) {
  47. botUrl = qs(option.vbot_url());
  48. }
  49. const auto months = option.vmonths().v;
  50. const auto amount = option.vamount().v;
  51. const auto currency = qs(option.vcurrency());
  52. result.push_back(CreateSubscriptionOption(
  53. months,
  54. monthlyAmount(currency),
  55. amount,
  56. currency,
  57. botUrl));
  58. }
  59. return result;
  60. }
  61. } // namespace Api