From 40f6c540dd941da5ab08891cb9d4c03d69469710 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 14 Jun 2024 17:23:13 +0300 Subject: [PATCH] Add and use StarManager::get_star_count. --- td/telegram/Payments.cpp | 19 +++++++++-------- td/telegram/StarManager.cpp | 38 ++++++++++++++++++++-------------- td/telegram/StarManager.h | 2 ++ td/telegram/UpdatesManager.cpp | 8 +++---- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index 093c268d0..16db3ff49 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -20,6 +20,7 @@ #include "td/telegram/Photo.h" #include "td/telegram/Premium.h" #include "td/telegram/ServerMessageId.h" +#include "td/telegram/StarManager.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" #include "td/telegram/ThemeManager.h" @@ -508,7 +509,8 @@ class GetPaymentFormQuery final : public Td::ResultHandler { return on_error(Status::Error(500, "Receive invalid price")); } auto photo = get_web_document_photo(td_->file_manager_.get(), std::move(payment_form->photo_), dialog_id_); - auto type = td_api::make_object(payment_form->invoice_->prices_[0]->amount_); + auto type = td_api::make_object( + StarManager::get_star_count(payment_form->invoice_->prices_[0]->amount_)); promise_.set_value(td_api::make_object( payment_form->form_id_, std::move(type), td_->user_manager_->get_user_id_object(seller_bot_user_id, "paymentForm seller"), @@ -663,16 +665,16 @@ class SendStarPaymentFormQuery final : public Td::ResultHandler { switch (payment_result->get_id()) { case telegram_api::payments_paymentResult::ID: { - auto result = move_tl_object_as(payment_result); + auto result = telegram_api::move_object_as(payment_result); td_->updates_manager_->on_get_updates( std::move(result->updates_), PromiseCreator::lambda([promise = std::move(promise_)](Unit) mutable { - promise.set_value(make_tl_object(true, string())); + promise.set_value(td_api::make_object(true, string())); })); return; } case telegram_api::payments_paymentVerificationNeeded::ID: { - auto result = move_tl_object_as(payment_result); - promise_.set_value(make_tl_object(false, std::move(result->url_))); + auto result = telegram_api::move_object_as(payment_result); + promise_.set_value(td_api::make_object(false, std::move(result->url_))); return; } default: @@ -729,13 +731,12 @@ class GetPaymentReceiptQuery final : public Td::ResultHandler { LOG(ERROR) << "Receive invalid prices " << to_string(payment_receipt->invoice_->prices_); return on_error(Status::Error(500, "Receive invalid price")); } - auto type = td_api::make_object(); - promise_.set_value(make_tl_object( get_product_info_object(td_, payment_receipt->title_, payment_receipt->description_, photo), payment_receipt->date_, td_->user_manager_->get_user_id_object(seller_bot_user_id, "paymentReceipt seller"), - td_api::make_object(payment_receipt->invoice_->prices_[0]->amount_, - payment_receipt->transaction_id_))); + td_api::make_object( + StarManager::get_star_count(payment_receipt->invoice_->prices_[0]->amount_), + payment_receipt->transaction_id_))); break; } case telegram_api::payments_paymentReceipt::ID: { diff --git a/td/telegram/StarManager.cpp b/td/telegram/StarManager.cpp index aec6552ff..824986fb5 100644 --- a/td/telegram/StarManager.cpp +++ b/td/telegram/StarManager.cpp @@ -47,8 +47,9 @@ class GetStarsTopupOptionsQuery final : public Td::ResultHandler { auto results = result_ptr.move_as_ok(); vector> options; for (auto &result : results) { - options.push_back(td_api::make_object( - result->currency_, result->amount_, result->stars_, result->store_product_, result->extended_)); + options.push_back(td_api::make_object(result->currency_, result->amount_, + StarManager::get_star_count(result->stars_), + result->store_product_, result->extended_)); } promise_.set_value(td_api::make_object(std::move(options))); @@ -156,11 +157,12 @@ class GetStarsTransactionsQuery final : public Td::ResultHandler { } }(); transactions.push_back(td_api::make_object( - transaction->id_, transaction->stars_, transaction->refund_, transaction->date_, std::move(source))); + transaction->id_, StarManager::get_star_count(transaction->stars_), transaction->refund_, transaction->date_, + std::move(source))); } - promise_.set_value( - td_api::make_object(result->balance_, std::move(transactions), result->next_offset_)); + promise_.set_value(td_api::make_object(StarManager::get_star_count(result->balance_), + std::move(transactions), result->next_offset_)); } void on_error(Status status) final { @@ -200,16 +202,9 @@ class RefundStarsChargeQuery final : public Td::ResultHandler { static td_api::object_ptr convert_stars_revenue_status( telegram_api::object_ptr obj) { CHECK(obj != nullptr); - auto get_amount = [](int64 amount) { - if (amount < 0) { - LOG(ERROR) << "Receive star amount = " << amount; - return static_cast(0); - } - return amount; - }; - return td_api::make_object(get_amount(obj->overall_revenue_), - get_amount(obj->current_balance_), - get_amount(obj->available_balance_), obj->withdrawal_enabled_); + return td_api::make_object( + StarManager::get_star_count(obj->overall_revenue_), StarManager::get_star_count(obj->current_balance_), + StarManager::get_star_count(obj->available_balance_), obj->withdrawal_enabled_); } class GetStarsRevenueStatsQuery final : public Td::ResultHandler { @@ -383,4 +378,17 @@ void StarManager::send_get_star_withdrawal_url_query( ->send(dialog_id, star_count, std::move(input_check_password)); } +int64 StarManager::get_star_count(int64 amount) { + if (amount < 0) { + LOG(ERROR) << "Receive star amount = " << amount; + return 0; + } + auto max_amount = static_cast(1) << 51; + if (amount > max_amount) { + LOG(ERROR) << "Receive star amount = " << amount; + return max_amount; + } + return amount; +} + } // namespace td diff --git a/td/telegram/StarManager.h b/td/telegram/StarManager.h index 91a2672d2..caa84365e 100644 --- a/td/telegram/StarManager.h +++ b/td/telegram/StarManager.h @@ -38,6 +38,8 @@ class StarManager final : public Actor { void get_star_withdrawal_url(const td_api::object_ptr &owner_id, int64 star_count, const string &password, Promise &&promise); + static int64 get_star_count(int64 amount); + private: void tear_down() final; diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 500235985..a70d3ac28 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -66,6 +66,7 @@ #include "td/telegram/SecretChatsManager.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/SpecialStickerSetType.h" +#include "td/telegram/StarManager.h" #include "td/telegram/StateManager.h" #include "td/telegram/StatisticsManager.h" #include "td/telegram/StickerListType.h" @@ -4569,11 +4570,8 @@ void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { - if (update->balance_ < 0) { - LOG(ERROR) << "Receive " << update->balance_ << " stars"; - update->balance_ = 0; - } - send_closure(G()->td(), &Td::send_update, td_api::make_object(update->balance_)); + send_closure(G()->td(), &Td::send_update, + td_api::make_object(StarManager::get_star_count(update->balance_))); promise.set_value(Unit()); }