Remove invalid premium payment options.

This commit is contained in:
levlam 2022-08-26 18:03:56 +03:00
parent df238a7034
commit 472fdd248e
4 changed files with 37 additions and 11 deletions

View File

@ -10901,9 +10901,7 @@ void ContactsManager::on_get_user_full(tl_object_ptr<telegram_api::userFull> &&u
bool supports_video_calls = user->video_calls_available_ && !user->phone_calls_private_;
bool has_private_calls = user->phone_calls_private_;
bool voice_messages_forbidden = u->is_premium ? user->voice_messages_forbidden_ : false;
auto premium_gift_options = transform(std::move(user->premium_gifts_), [](auto &&premium_gift_option) {
return PremiumGiftOption(std::move(premium_gift_option));
});
auto premium_gift_options = get_premium_gift_options(std::move(user->premium_gifts_));
AdministratorRights group_administrator_rights(user->bot_group_admin_rights_, ChannelType::Megagroup);
AdministratorRights broadcast_administrator_rights(user->bot_broadcast_admin_rights_, ChannelType::Broadcast);
if (user_full->can_be_called != can_be_called || user_full->supports_video_calls != supports_video_calls ||

View File

@ -156,8 +156,7 @@ class GetPremiumPromoQuery final : public Td::ResultHandler {
std::move(animation_object)));
}
auto period_options = transform(std::move(promo->period_options_),
[](auto &&period_option) { return PremiumGiftOption(std::move(period_option)); });
auto period_options = get_premium_gift_options(std::move(promo->period_options_));
auto base_premium_gift_it = std::min_element(period_options.begin(), period_options.end());
auto payment_options = transform(period_options, [&base_premium_gift_it](const auto &option) {
return option.get_premium_payment_option_object(*base_premium_gift_it);

View File

@ -9,6 +9,7 @@
#include "td/telegram/LinkManager.h"
#include "td/telegram/Payments.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
@ -22,10 +23,6 @@ PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::prem
, 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;
}
}
PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::premiumSubscriptionOption> &&option)
@ -34,10 +31,18 @@ PremiumGiftOption::PremiumGiftOption(telegram_api::object_ptr<telegram_api::prem
, 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;
}
bool PremiumGiftOption::is_valid() const {
if (amount_ <= 0 || !check_currency_amount(amount_)) {
LOG(ERROR) << "Receive invalid premium payment option amount " << amount_;
return false;
}
if (currency_.size() != 3) {
LOG(ERROR) << "Receive invalid premium payment option currency " << currency_;
return false;
}
return true;
}
double PremiumGiftOption::get_monthly_price() const {
@ -73,4 +78,20 @@ bool operator!=(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs) {
return !(lhs == rhs);
}
vector<PremiumGiftOption> get_premium_gift_options(
vector<telegram_api::object_ptr<telegram_api::premiumGiftOption>> &&options) {
auto premium_gift_options = transform(
std::move(options), [](auto &&premium_gift_option) { return PremiumGiftOption(std::move(premium_gift_option)); });
td::remove_if(premium_gift_options, [](const auto &premium_gift_option) { return !premium_gift_option.is_valid(); });
return premium_gift_options;
}
vector<PremiumGiftOption> get_premium_gift_options(
vector<telegram_api::object_ptr<telegram_api::premiumSubscriptionOption>> &&options) {
auto premium_gift_options = transform(
std::move(options), [](auto &&premium_gift_option) { return PremiumGiftOption(std::move(premium_gift_option)); });
td::remove_if(premium_gift_options, [](const auto &premium_gift_option) { return !premium_gift_option.is_valid(); });
return premium_gift_options;
}
} // namespace td

View File

@ -34,6 +34,8 @@ class PremiumGiftOption {
td_api::object_ptr<td_api::premiumPaymentOption> get_premium_payment_option_object(
const PremiumGiftOption &base_option) const;
bool is_valid() const;
template <class StorerT>
void store(StorerT &storer) const;
@ -44,4 +46,10 @@ class PremiumGiftOption {
bool operator==(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs);
bool operator!=(const PremiumGiftOption &lhs, const PremiumGiftOption &rhs);
vector<PremiumGiftOption> get_premium_gift_options(
vector<telegram_api::object_ptr<telegram_api::premiumGiftOption>> &&options);
vector<PremiumGiftOption> get_premium_gift_options(
vector<telegram_api::object_ptr<telegram_api::premiumSubscriptionOption>> &&options);
} // namespace td