Add and use StarManager::get_star_count.

This commit is contained in:
levlam 2024-06-14 17:23:13 +03:00
parent db4f61e261
commit 40f6c540dd
4 changed files with 38 additions and 29 deletions

View File

@ -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<td_api::paymentFormTypeStars>(payment_form->invoice_->prices_[0]->amount_);
auto type = td_api::make_object<td_api::paymentFormTypeStars>(
StarManager::get_star_count(payment_form->invoice_->prices_[0]->amount_));
promise_.set_value(td_api::make_object<td_api::paymentForm>(
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<telegram_api::payments_paymentResult>(payment_result);
auto result = telegram_api::move_object_as<telegram_api::payments_paymentResult>(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<td_api::paymentResult>(true, string()));
promise.set_value(td_api::make_object<td_api::paymentResult>(true, string()));
}));
return;
}
case telegram_api::payments_paymentVerificationNeeded::ID: {
auto result = move_tl_object_as<telegram_api::payments_paymentVerificationNeeded>(payment_result);
promise_.set_value(make_tl_object<td_api::paymentResult>(false, std::move(result->url_)));
auto result = telegram_api::move_object_as<telegram_api::payments_paymentVerificationNeeded>(payment_result);
promise_.set_value(td_api::make_object<td_api::paymentResult>(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<td_api::paymentFormTypeStars>();
promise_.set_value(make_tl_object<td_api::paymentReceipt>(
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<td_api::paymentReceiptTypeStars>(payment_receipt->invoice_->prices_[0]->amount_,
payment_receipt->transaction_id_)));
td_api::make_object<td_api::paymentReceiptTypeStars>(
StarManager::get_star_count(payment_receipt->invoice_->prices_[0]->amount_),
payment_receipt->transaction_id_)));
break;
}
case telegram_api::payments_paymentReceipt::ID: {

View File

@ -47,8 +47,9 @@ class GetStarsTopupOptionsQuery final : public Td::ResultHandler {
auto results = result_ptr.move_as_ok();
vector<td_api::object_ptr<td_api::starPaymentOption>> options;
for (auto &result : results) {
options.push_back(td_api::make_object<td_api::starPaymentOption>(
result->currency_, result->amount_, result->stars_, result->store_product_, result->extended_));
options.push_back(td_api::make_object<td_api::starPaymentOption>(result->currency_, result->amount_,
StarManager::get_star_count(result->stars_),
result->store_product_, result->extended_));
}
promise_.set_value(td_api::make_object<td_api::starPaymentOptions>(std::move(options)));
@ -156,11 +157,12 @@ class GetStarsTransactionsQuery final : public Td::ResultHandler {
}
}();
transactions.push_back(td_api::make_object<td_api::starTransaction>(
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<td_api::starTransactions>(result->balance_, std::move(transactions), result->next_offset_));
promise_.set_value(td_api::make_object<td_api::starTransactions>(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<td_api::starRevenueStatus> convert_stars_revenue_status(
telegram_api::object_ptr<telegram_api::starsRevenueStatus> obj) {
CHECK(obj != nullptr);
auto get_amount = [](int64 amount) {
if (amount < 0) {
LOG(ERROR) << "Receive star amount = " << amount;
return static_cast<int64>(0);
}
return amount;
};
return td_api::make_object<td_api::starRevenueStatus>(get_amount(obj->overall_revenue_),
get_amount(obj->current_balance_),
get_amount(obj->available_balance_), obj->withdrawal_enabled_);
return td_api::make_object<td_api::starRevenueStatus>(
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<int64>(1) << 51;
if (amount > max_amount) {
LOG(ERROR) << "Receive star amount = " << amount;
return max_amount;
}
return amount;
}
} // namespace td

View File

@ -38,6 +38,8 @@ class StarManager final : public Actor {
void get_star_withdrawal_url(const td_api::object_ptr<td_api::MessageSender> &owner_id, int64 star_count,
const string &password, Promise<string> &&promise);
static int64 get_star_count(int64 amount);
private:
void tear_down() final;

View File

@ -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<telegram_api::updateBroadcastRevenu
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateStarsBalance> update, Promise<Unit> &&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<td_api::updateOwnedStarCount>(update->balance_));
send_closure(G()->td(), &Td::send_update,
td_api::make_object<td_api::updateOwnedStarCount>(StarManager::get_star_count(update->balance_)));
promise.set_value(Unit());
}