diff options
| author | levlam <levlam@telegram.org> | 2025-02-19 14:40:00 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2025-02-19 14:40:00 +0300 |
| commit | 1581c1b3ef014a834d0f728fd7c90390149cac60 (patch) | |
| tree | e89be454382fc4b3544075e06c68be337eda2ba3 /td/telegram/Premium.cpp | |
| parent | 5aa6845a1829ddb39d0ad87b7afcb5ef19fa41af (diff) | |
Add separate methods for premium gifting and giveaway creation options.
Diffstat (limited to 'td/telegram/Premium.cpp')
| -rw-r--r-- | td/telegram/Premium.cpp | 109 |
1 files changed, 73 insertions, 36 deletions
diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index e9dd5ce4e..f8408fb66 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -166,10 +166,6 @@ static td_api::object_ptr<td_api::BusinessFeature> get_business_feature_object(S } Result<telegram_api::object_ptr<telegram_api::InputPeer>> get_boost_input_peer(Td *td, DialogId dialog_id) { - if (dialog_id == DialogId()) { - return nullptr; - } - if (!td->dialog_manager_->have_dialog_force(dialog_id, "get_boost_input_peer")) { return Status::Error(400, "Chat to boost not found"); } @@ -217,7 +213,10 @@ static Result<tl_object_ptr<telegram_api::InputStorePaymentPurpose>> get_input_s return Status::Error(400, "Strings must be encoded in UTF-8"); } DialogId boosted_dialog_id(p->boosted_chat_id_); - TRY_RESULT(boost_input_peer, get_boost_input_peer(td, boosted_dialog_id)); + telegram_api::object_ptr<telegram_api::InputPeer> boost_input_peer; + if (boosted_dialog_id != DialogId()) { + TRY_RESULT_ASSIGN(boost_input_peer, get_boost_input_peer(td, boosted_dialog_id)); + } TRY_RESULT(message, get_formatted_text(td, td->dialog_manager_->get_my_dialog_id(), std::move(p->text_), false, true, true, false)); MessageQuote::remove_unallowed_quote_entities(message); @@ -373,12 +372,62 @@ class GetPremiumPromoQuery final : public Td::ResultHandler { } }; -class GetPremiumGiftCodeOptionsQuery final : public Td::ResultHandler { - Promise<td_api::object_ptr<td_api::premiumGiftCodePaymentOptions>> promise_; +class GetPremiumGiftOptionsQuery final : public Td::ResultHandler { + Promise<td_api::object_ptr<td_api::premiumGiftPaymentOptions>> promise_; + + public: + explicit GetPremiumGiftOptionsQuery(Promise<td_api::object_ptr<td_api::premiumGiftPaymentOptions>> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(telegram_api::payments_getPremiumGiftCodeOptions(0, nullptr))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result<telegram_api::payments_getPremiumGiftCodeOptions>(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto results = result_ptr.move_as_ok(); + td::remove_if(results, [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { + return payment_option->users_ != 1 || payment_option->months_ <= 0 || payment_option->amount_ <= 0 || + payment_option->currency_.empty() || payment_option->store_quantity_ > 1; + }); + auto get_monthly_price = [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { + return static_cast<double>(payment_option->amount_) / static_cast<double>(payment_option->months_); + }; + FlatHashMap<string, double> max_prices; + for (auto &result : results) { + auto &max_price = max_prices[result->currency_]; + auto price = get_monthly_price(result); + if (price > max_price) { + max_price = price; + } + } + + vector<td_api::object_ptr<td_api::premiumGiftPaymentOption>> options; + for (auto &result : results) { + double relative_price = get_monthly_price(result) / max_prices[result->currency_]; + options.push_back(td_api::make_object<td_api::premiumGiftPaymentOption>( + result->currency_, result->amount_, static_cast<int32>(100 * (1.0 - relative_price)), result->months_, + result->store_product_, td_->stickers_manager_->get_premium_gift_sticker_object(result->months_, 0))); + } + promise_.set_value(td_api::make_object<td_api::premiumGiftPaymentOptions>(std::move(options))); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +class GetPremiumGiveawayOptionsQuery final : public Td::ResultHandler { + Promise<td_api::object_ptr<td_api::premiumGiveawayPaymentOptions>> promise_; DialogId boosted_dialog_id_; public: - explicit GetPremiumGiftCodeOptionsQuery(Promise<td_api::object_ptr<td_api::premiumGiftCodePaymentOptions>> &&promise) + explicit GetPremiumGiveawayOptionsQuery(Promise<td_api::object_ptr<td_api::premiumGiveawayPaymentOptions>> &&promise) : promise_(std::move(promise)) { } @@ -389,10 +438,7 @@ class GetPremiumGiftCodeOptionsQuery final : public Td::ResultHandler { } auto boost_input_peer = r_boost_input_peer.move_as_ok(); - int32 flags = 0; - if (boost_input_peer != nullptr) { - flags |= telegram_api::payments_getPremiumGiftCodeOptions::BOOST_PEER_MASK; - } + int32 flags = telegram_api::payments_getPremiumGiftCodeOptions::BOOST_PEER_MASK; send_query(G()->net_query_creator().create( telegram_api::payments_getPremiumGiftCodeOptions(flags, std::move(boost_input_peer)))); } @@ -406,39 +452,26 @@ class GetPremiumGiftCodeOptionsQuery final : public Td::ResultHandler { auto results = result_ptr.move_as_ok(); td::remove_if(results, [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { return payment_option->users_ <= 0 || payment_option->months_ <= 0 || payment_option->amount_ <= 0 || - payment_option->currency_.empty(); + payment_option->currency_.empty() || payment_option->currency_ == "XTR"; }); - auto get_monthly_price = [](const telegram_api::object_ptr<telegram_api::premiumGiftCodeOption> &payment_option) { - return static_cast<double>(payment_option->amount_) / static_cast<double>(payment_option->months_); - }; - FlatHashMap<int32, FlatHashMap<string, double>> max_prices; - for (auto &result : results) { - auto &max_price = max_prices[result->users_][result->currency_]; - auto price = get_monthly_price(result); - if (price > max_price) { - max_price = price; - } - } - vector<td_api::object_ptr<td_api::premiumGiftCodePaymentOption>> options; + vector<td_api::object_ptr<td_api::premiumGiveawayPaymentOption>> options; for (auto &result : results) { if (result->store_product_.empty()) { result->store_quantity_ = 0; } else if (result->store_quantity_ <= 0) { result->store_quantity_ = 1; } - double relative_price = get_monthly_price(result) / max_prices[result->users_][result->currency_]; - options.push_back(td_api::make_object<td_api::premiumGiftCodePaymentOption>( - result->currency_, result->amount_, static_cast<int32>(100 * (1.0 - relative_price)), result->users_, - result->months_, result->store_product_, result->store_quantity_, - td_->stickers_manager_->get_premium_gift_sticker_object(result->months_, 0))); + options.push_back(td_api::make_object<td_api::premiumGiveawayPaymentOption>( + result->currency_, result->amount_, result->users_, result->months_, result->store_product_, + result->store_quantity_)); } - promise_.set_value(td_api::make_object<td_api::premiumGiftCodePaymentOptions>(std::move(options))); + promise_.set_value(td_api::make_object<td_api::premiumGiveawayPaymentOptions>(std::move(options))); } void on_error(Status status) final { - td_->dialog_manager_->on_get_dialog_error(boosted_dialog_id_, status, "GetPremiumGiftCodeOptionsQuery"); + td_->dialog_manager_->on_get_dialog_error(boosted_dialog_id_, status, "GetPremiumGiveawayOptionsQuery"); promise_.set_error(std::move(status)); } }; @@ -1188,19 +1221,23 @@ void get_premium_state(Td *td, Promise<td_api::object_ptr<td_api::premiumState>> td->create_handler<GetPremiumPromoQuery>(std::move(promise))->send(); } -void get_premium_gift_code_options(Td *td, DialogId boosted_dialog_id, - Promise<td_api::object_ptr<td_api::premiumGiftCodePaymentOptions>> &&promise) { +void get_premium_gift_options(Td *td, Promise<td_api::object_ptr<td_api::premiumGiftPaymentOptions>> &&promise) { td->stickers_manager_->load_premium_gift_sticker_set( - PromiseCreator::lambda([td, boosted_dialog_id, promise = std::move(promise)](Result<Unit> &&result) mutable { + PromiseCreator::lambda([td, promise = std::move(promise)](Result<Unit> &&result) mutable { if (result.is_error()) { promise.set_error(result.move_as_error()); } else { TRY_STATUS_PROMISE(promise, G()->close_status()); - td->create_handler<GetPremiumGiftCodeOptionsQuery>(std::move(promise))->send(boosted_dialog_id); + td->create_handler<GetPremiumGiftOptionsQuery>(std::move(promise))->send(); } })); } +void get_premium_giveaway_options(Td *td, DialogId boosted_dialog_id, + Promise<td_api::object_ptr<td_api::premiumGiveawayPaymentOptions>> &&promise) { + td->create_handler<GetPremiumGiveawayOptionsQuery>(std::move(promise))->send(boosted_dialog_id); +} + void check_premium_gift_code(Td *td, const string &code, Promise<td_api::object_ptr<td_api::premiumGiftCodeInfo>> &&promise) { td->create_handler<CheckGiftCodeQuery>(std::move(promise))->send(code); |
