From bcf2580e0930773818b664d537fd2cddfcc746e5 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 24 Jul 2022 16:18:20 +0300 Subject: [PATCH] Add discount_percentage. --- td/generate/scheme/td_api.tl | 3 ++- td/telegram/ContactsManager.cpp | 7 +++++-- td/telegram/PremiumGiftOption.cpp | 24 ++++++++++++++++++++++-- td/telegram/PremiumGiftOption.h | 7 ++++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 3d8c1e428..0a9710558 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -469,10 +469,11 @@ chatAdministratorRights can_manage_chat:Bool can_change_info:Bool can_post_messa //@description Describes an option for gifting Telegram Premium to a user //@currency ISO 4217 currency code for Telegram Premium subscription payment //@amount The amount to pay, in the smallest units of the currency +//@discount_percentage The discount associated with this gift option, as a percentage //@month_count Number of month the Telegram Premium subscription will be active //@store_product_id Identifier of the store product associated with the option //@payment_link An internal link to be opened for gifting Telegram Premium to the user if store payment isn't possible; may be null if direct payment isn't available -premiumGiftOption currency:string amount:int53 month_count:int32 store_product_id:string payment_link:InternalLinkType = PremiumGiftOption; +premiumGiftOption currency:string amount:int53 discount_percentage:int32 month_count:int32 store_product_id:string payment_link:InternalLinkType = PremiumGiftOption; //@description Represents a user diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 3a61f4a10..f38d0667a 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -16769,8 +16769,11 @@ tl_object_ptr ContactsManager::get_user_full_info_object(U } bio_object = get_formatted_text_object(bio, true, 0); } - auto premium_gift_options = transform(user_full->premium_gift_options, - [](const auto &option) { return option.get_premium_gift_option_object(); }); + auto base_premium_gift_it = + std::min_element(user_full->premium_gift_options.begin(), user_full->premium_gift_options.end()); + auto premium_gift_options = transform(user_full->premium_gift_options, [&base_premium_gift_it](const auto &option) { + return option.get_premium_gift_option_object(*base_premium_gift_it); + }); auto voice_messages_forbidden = is_premium ? user_full->voice_messages_forbidden : false; return make_tl_object( get_chat_photo_object(td_->file_manager_.get(), user_full->photo), user_full->is_blocked, diff --git a/td/telegram/PremiumGiftOption.cpp b/td/telegram/PremiumGiftOption.cpp index 688830ac2..9f401eb47 100644 --- a/td/telegram/PremiumGiftOption.cpp +++ b/td/telegram/PremiumGiftOption.cpp @@ -10,6 +10,9 @@ #include "td/utils/common.h" +#include +#include + namespace td { PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr &&option) @@ -20,13 +23,30 @@ PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptrstore_product_)) { } -td_api::object_ptr PremiumGiftOption::get_premium_gift_option_object() const { +double PremiumGiftOption::get_monthly_price() const { + return static_cast(amount_) / static_cast(months_); +} + +td_api::object_ptr PremiumGiftOption::get_premium_gift_option_object( + const PremiumGiftOption &base_option) const { auto link_type = LinkManager::parse_internal_link(bot_url_, true); + int32 discount_percentage = 0; + if (base_option.months_ > 0 && months_ > 0 && base_option.amount_ > 0 && amount_ > 0) { + double relative_price = get_monthly_price() / base_option.get_monthly_price(); + if (relative_price < 1.0) { + discount_percentage = static_cast(100 * (1.0 - relative_price)); + } + } return td_api::make_object( - currency_, amount_, months_, store_product_, + currency_, amount_, discount_percentage, months_, store_product_, link_type == nullptr ? nullptr : link_type->get_internal_link_type_object()); } +bool operator<(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs) { + return std::tie(lhs.months_, lhs.amount_, lhs.currency_, lhs.store_product_, lhs.bot_url_) < + std::tie(rhs.months_, rhs.amount_, rhs.currency_, rhs.store_product_, rhs.bot_url_); +} + bool operator==(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs) { return lhs.months_ == rhs.months_ && lhs.currency_ == rhs.currency_ && lhs.amount_ == rhs.amount_ && lhs.bot_url_ == rhs.bot_url_ && lhs.store_product_ == rhs.store_product_; diff --git a/td/telegram/PremiumGiftOption.h b/td/telegram/PremiumGiftOption.h index 5c6ee87af..cb235ee76 100644 --- a/td/telegram/PremiumGiftOption.h +++ b/td/telegram/PremiumGiftOption.h @@ -20,13 +20,18 @@ class PremiumGiftOption { string bot_url_; string store_product_; + friend bool operator<(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs); + friend bool operator==(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs); + double get_monthly_price() const; + public: PremiumGiftOption() = default; explicit PremiumGiftOption(telegram_api::object_ptr &&option); - td_api::object_ptr get_premium_gift_option_object() const; + td_api::object_ptr get_premium_gift_option_object( + const PremiumGiftOption &base_option) const; template void store(StorerT &storer) const;