From a4171a8cc77e54687496beb4f094286ead0ebe47 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 7 Aug 2024 14:30:45 +0300 Subject: [PATCH] Locally update owned star count after bying a paid media. --- td/telegram/MessagesManager.cpp | 12 ++++++++---- td/telegram/MessagesManager.h | 6 +++++- td/telegram/Payments.cpp | 15 +++++++++++---- td/telegram/StarManager.cpp | 4 ++++ td/telegram/StarManager.h | 2 ++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 82d83e2df..58d678c6c 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17466,7 +17466,7 @@ void MessagesManager::get_message_properties(DialogId dialog_id, MessageId messa auto can_be_saved = can_save_message(dialog_id, m); auto can_be_edited = can_edit_message(dialog_id, m, false, is_bot); auto can_be_forwarded = can_be_saved && can_forward_message(dialog_id, m); - auto can_be_paid = get_invoice_message_id({dialog_id, m->message_id}).is_ok(); + auto can_be_paid = get_invoice_message_info({dialog_id, m->message_id}).is_ok(); auto can_be_pinned = can_pin_message(dialog_id, m).is_ok(); auto can_be_replied = message_id.is_valid() && !(message_id == MessageId(ServerMessageId(1)) && dialog_type == DialogType::Channel) && @@ -38600,8 +38600,8 @@ void MessagesManager::stop_poll(MessageFullId message_full_id, td_api::object_pt stop_message_content_poll(td_, m->content.get(), message_full_id, std::move(new_reply_markup), std::move(promise)); } -Result MessagesManager::get_invoice_message_id(MessageFullId message_full_id) { - auto m = get_message_force(message_full_id, "get_invoice_message_id"); +Result MessagesManager::get_invoice_message_info(MessageFullId message_full_id) { + auto m = get_message_force(message_full_id, "get_invoice_message_info"); if (m == nullptr) { return Status::Error(400, "Message not found"); } @@ -38628,7 +38628,11 @@ Result MessagesManager::get_invoice_message_id(MessageFullId me } } - return m->message_id.get_server_message_id(); + InvoiceMessageInfo result; + result.server_message_id_ = m->message_id.get_server_message_id(); + result.star_count_ = + content_type != MessageContentType::PaidMedia ? 0 : get_message_content_star_count(m->content.get()); + return std::move(result); } Result MessagesManager::get_payment_successful_message_id(MessageFullId message_full_id) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 829129304..e88137751 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -966,7 +966,11 @@ class MessagesManager final : public Actor { Result get_login_button_url(MessageFullId message_full_id, int64 button_id); - Result get_invoice_message_id(MessageFullId message_full_id); + struct InvoiceMessageInfo { + ServerMessageId server_message_id_; + int64 star_count_ = 0; + }; + Result get_invoice_message_info(MessageFullId message_full_id); Result get_payment_successful_message_id(MessageFullId message_full_id); diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index ed73bccab..915a3aa96 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -46,6 +46,7 @@ namespace { struct InputInvoiceInfo { DialogId dialog_id_; telegram_api::object_ptr input_invoice_; + int64 star_count_ = 0; }; Result get_input_invoice_info(Td *td, td_api::object_ptr &&input_invoice) { @@ -59,7 +60,7 @@ Result get_input_invoice_info(Td *td, td_api::object_ptr(input_invoice); DialogId dialog_id(invoice->chat_id_); MessageId message_id(invoice->message_id_); - TRY_RESULT(server_message_id, td->messages_manager_->get_invoice_message_id({dialog_id, message_id})); + TRY_RESULT(invoice_message_info, td->messages_manager_->get_invoice_message_info({dialog_id, message_id})); auto input_peer = td->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read); if (input_peer == nullptr) { @@ -67,8 +68,9 @@ Result get_input_invoice_info(Td *td, td_api::object_ptr(std::move(input_peer), server_message_id.get()); + result.input_invoice_ = telegram_api::make_object( + std::move(input_peer), invoice_message_info.server_message_id_.get()); + result.star_count_ = invoice_message_info.star_count_; break; } case td_api::inputInvoiceName::ID: { @@ -673,6 +675,7 @@ class SendPaymentFormQuery final : public Td::ResultHandler { class SendStarPaymentFormQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; + int64 star_count_; public: explicit SendStarPaymentFormQuery(Promise> &&promise) @@ -681,13 +684,14 @@ class SendStarPaymentFormQuery final : public Td::ResultHandler { void send(InputInvoiceInfo &&input_invoice_info, int64 payment_form_id) { dialog_id_ = input_invoice_info.dialog_id_; + star_count_ = input_invoice_info.star_count_; send_query(G()->net_query_creator().create( telegram_api::payments_sendStarsForm(0, payment_form_id, std::move(input_invoice_info.input_invoice_)))); } void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); + auto result_ptr = fetch_result(packet); if (result_ptr.is_error()) { return on_error(result_ptr.move_as_error()); } @@ -698,6 +702,9 @@ class SendStarPaymentFormQuery final : public Td::ResultHandler { switch (payment_result->get_id()) { case telegram_api::payments_paymentResult::ID: { auto result = telegram_api::move_object_as(payment_result); + if (star_count_ != 0) { + td_->star_manager_->add_owned_star_count(-star_count_); + } td_->updates_manager_->on_get_updates( std::move(result->updates_), PromiseCreator::lambda([promise = std::move(promise_)](Unit) mutable { promise.set_value(td_api::make_object(true, string())); diff --git a/td/telegram/StarManager.cpp b/td/telegram/StarManager.cpp index 4a05a2319..466fcc94b 100644 --- a/td/telegram/StarManager.cpp +++ b/td/telegram/StarManager.cpp @@ -666,6 +666,10 @@ void StarManager::on_update_owned_star_count(int64 star_count) { send_closure(G()->td(), &Td::send_update, get_update_owned_star_count_object()); } +void StarManager::add_owned_star_count(int64 star_count) { + on_update_owned_star_count(star_count + owned_star_count_); +} + Status StarManager::can_manage_stars(DialogId dialog_id, bool allow_self) const { switch (dialog_id.get_type()) { case DialogType::User: { diff --git a/td/telegram/StarManager.h b/td/telegram/StarManager.h index 7797191f5..267cb37c5 100644 --- a/td/telegram/StarManager.h +++ b/td/telegram/StarManager.h @@ -29,6 +29,8 @@ class StarManager final : public Actor { void on_update_owned_star_count(int64 star_count); + void add_owned_star_count(int64 star_count); + void get_star_payment_options(Promise> &&promise); void get_star_gift_payment_options(UserId user_id, Promise> &&promise);