From 51dc5d6e377e0dd735e1086d0443e2cc05d4f964 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 3 Mar 2025 19:07:46 +0300 Subject: [PATCH] Fixed discount calculation for gifts options with different currencies. --- .../SourceFiles/api/api_premium_option.cpp | 2 +- Telegram/SourceFiles/api/api_premium_option.h | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Telegram/SourceFiles/api/api_premium_option.cpp b/Telegram/SourceFiles/api/api_premium_option.cpp index bd3056a75..d3c67e23b 100644 --- a/Telegram/SourceFiles/api/api_premium_option.cpp +++ b/Telegram/SourceFiles/api/api_premium_option.cpp @@ -26,7 +26,7 @@ Data::PremiumSubscriptionOption CreateSubscriptionOption( }(); return { .duration = Ui::FormatTTL(months * 86400 * 31), - .discount = discount + .discount = (discount > 0) ? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount) : QString(), .costPerMonth = Ui::FillAmountAndCurrency( diff --git a/Telegram/SourceFiles/api/api_premium_option.h b/Telegram/SourceFiles/api/api_premium_option.h index afe66ac4a..2648a7f9c 100644 --- a/Telegram/SourceFiles/api/api_premium_option.h +++ b/Telegram/SourceFiles/api/api_premium_option.h @@ -24,15 +24,26 @@ template if (tlOpts.isEmpty()) { return {}; } + auto monthlyAmountPerCurrency = base::flat_map(); auto result = Data::PremiumSubscriptionOptions(); - const auto monthlyAmount = [&] { + const auto monthlyAmount = [&](const QString ¤cy) -> int { + const auto it = monthlyAmountPerCurrency.find(currency); + if (it != end(monthlyAmountPerCurrency)) { + return it->second; + } const auto &min = ranges::min_element( tlOpts, ranges::less(), - [](const Option &o) { return o.data().vamount().v; } + [&](const Option &o) { + return currency == qs(o.data().vcurrency()) + ? o.data().vamount().v + : std::numeric_limits::max(); + } )->data(); - return min.vamount().v / float64(min.vmonths().v); - }(); + const auto monthly = min.vamount().v / float64(min.vmonths().v); + monthlyAmountPerCurrency.emplace(currency, monthly); + return monthly; + }; result.reserve(tlOpts.size()); for (const auto &tlOption : tlOpts) { const auto &option = tlOption.data(); @@ -45,7 +56,7 @@ template const auto currency = qs(option.vcurrency()); result.push_back(CreateSubscriptionOption( months, - monthlyAmount, + monthlyAmount(currency), amount, currency, botUrl));