tdlight/td/telegram/PremiumGiftOption.cpp

65 lines
2.5 KiB
C++
Raw Normal View History

2022-07-01 14:57:38 +02:00
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/PremiumGiftOption.h"
#include "td/telegram/LinkManager.h"
#include "td/telegram/Payments.h"
2022-07-01 14:57:38 +02:00
#include "td/utils/common.h"
2022-08-14 14:04:08 +02:00
#include "td/utils/logging.h"
2022-07-01 14:57:38 +02:00
2022-07-24 15:18:20 +02:00
#include <tuple>
2022-07-01 14:57:38 +02:00
namespace td {
PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::premiumGiftOption> &&option)
: months_(option->months_)
, currency_(std::move(option->currency_))
, amount_(option->amount_)
, bot_url_(std::move(option->bot_url_))
, store_product_(std::move(option->store_product_)) {
if (amount_ <= 0 || !check_currency_amount(amount_)) {
LOG(ERROR) << "Receive invalid premium gift option amount " << amount_;
amount_ = static_cast<int64>(1) << 40;
}
2022-07-01 14:57:38 +02:00
}
2022-07-24 15:18:20 +02:00
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 {
2022-07-01 14:57:38 +02:00
auto link_type = LinkManager::parse_internal_link(bot_url_, true);
2022-07-24 15:18:20 +02:00
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));
}
}
2022-07-01 14:57:38 +02:00
return td_api::make_object<td_api::premiumGiftOption>(
2022-07-24 15:18:20 +02:00
currency_, amount_, discount_percentage, months_, store_product_,
2022-07-01 14:57:38 +02:00
link_type == nullptr ? nullptr : link_type->get_internal_link_type_object());
}
2022-07-24 15:18:20 +02:00
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_);
}
2022-07-01 14:57:38 +02:00
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_;
}
bool operator!=(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs) {
return !(lhs == rhs);
}
} // namespace td