Add discount_percentage.

This commit is contained in:
levlam 2022-07-24 16:18:20 +03:00
parent 8183e062ab
commit bcf2580e09
4 changed files with 35 additions and 6 deletions

View File

@ -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

View File

@ -16769,8 +16769,11 @@ tl_object_ptr<td_api::userFullInfo> 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<td_api::userFullInfo>(
get_chat_photo_object(td_->file_manager_.get(), user_full->photo), user_full->is_blocked,

View File

@ -10,6 +10,9 @@
#include "td/utils/common.h"
#include <limits>
#include <tuple>
namespace td {
PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::premiumGiftOption> &&option)
@ -20,13 +23,30 @@ PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::prem
, store_product_(std::move(option->store_product_)) {
}
td_api::object_ptr<td_api::premiumGiftOption> PremiumGiftOption::get_premium_gift_option_object() const {
double PremiumGiftOption::get_monthly_price() const {
return static_cast<double>(amount_) / static_cast<double>(months_);
}
td_api::object_ptr<td_api::premiumGiftOption> 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<int32>(100 * (1.0 - relative_price));
}
}
return td_api::make_object<td_api::premiumGiftOption>(
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_;

View File

@ -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<telegram_api::premiumGiftOption> &&option);
td_api::object_ptr<td_api::premiumGiftOption> get_premium_gift_option_object() const;
td_api::object_ptr<td_api::premiumGiftOption> get_premium_gift_option_object(
const PremiumGiftOption &base_option) const;
template <class StorerT>
void store(StorerT &storer) const;