From 31194a7591b3782f4d76c25713ca667d6951dd87 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 16 May 2024 20:45:05 +0300 Subject: [PATCH] Add td_api::getStarTransactions. --- td/generate/scheme/td_api.tl | 26 ++++++++++++++++ td/telegram/Premium.cpp | 58 ++++++++++++++++++++++++++++++++++++ td/telegram/Premium.h | 4 +++ td/telegram/Td.cpp | 7 +++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 8 +++++ 6 files changed, 105 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 704e50e4b..83aeb6ef6 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -833,6 +833,27 @@ starPaymentOption currency:string amount:int53 star_count:int53 store_product_id starPaymentOptions options:vector = StarPaymentOptions; +//@class StarTransactionDirection @description Describes direction of a transaction with Telegram stars + +//@description The transaction is incoming and increases the number of owned Telegram stars +starTransactionDirectionIncoming = StarTransactionDirection; + +//@description The transaction is outgoing and decreases the number of owned Telegram stars +starTransactionDirectionOutgoing = StarTransactionDirection; + + +//@description Represents a transaction changing the amount of owned Telegram stars +//@id Unique identifier of the transaction +//@star_count The amount of added owned Telegram stars; negative for outgoing transactions +//@date Point in time (Unix timestamp) when the transaction was completed +starTransaction id:string star_count:int53 date:int32 = StarTransaction; + +//@description Represents a list of Telegram star transactions +//@star_count The amount of owned Telegram stars +//@transactions List of transactions with Telegram stars +//@next_offset The offset for the next request. If empty, then there are no more results +starTransactions star_count:int53 transactions:vector next_offset:string = StarTransactions; + //@class PremiumGiveawayParticipantStatus @description Contains information about status of a user in a Telegram Premium giveaway @@ -10756,6 +10777,11 @@ getPremiumGiveawayInfo chat_id:int53 message_id:int53 = PremiumGiveawayInfo; //@description Returns available options for Telegram stars purchase getStarPaymentOptions = StarPaymentOptions; +//@description Returns the list of Telegram star transactions for the current user +//@offset Offset of the first transaction to return as received from the previous request; use empty string to get the first chunk of results +//@direction Direction of the transactions to receive; pass null to get all transactions +getStarTransactions offset:string direction:StarTransactionDirection = StarTransactions; + //@description Checks whether an in-store purchase is possible. Must be called before any in-store purchase @purpose Transaction purpose canPurchaseFromStore purpose:StorePaymentPurpose = Ok; diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index c4fa0c326..561af08c6 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -640,6 +640,58 @@ class GetStarsTopupOptionsQuery final : public Td::ResultHandler { } }; +class GetStarsTransactionsQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetStarsTransactionsQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(const string &offset, td_api::object_ptr &&direction) { + int32 flags = 0; + if (direction != nullptr) { + switch (direction->get_id()) { + case td_api::starTransactionDirectionIncoming::ID: + flags |= telegram_api::payments_getStarsTransactions::INBOUND_MASK; + break; + case td_api::starTransactionDirectionOutgoing::ID: + flags |= telegram_api::payments_getStarsTransactions::OUTBOUND_MASK; + break; + default: + UNREACHABLE(); + } + } + send_query(G()->net_query_creator().create( + telegram_api::payments_getStarsTransactions(flags, false /*ignored*/, false /*ignored*/, + telegram_api::make_object(), offset))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto result = result_ptr.move_as_ok(); + td_->user_manager_->on_get_users(std::move(result->users_), "GetStarsTransactionsQuery"); + td_->chat_manager_->on_get_chats(std::move(result->chats_), "GetStarsTransactionsQuery"); + + vector> transactions; + for (auto &transaction : result->history_) { + transactions.push_back( + td_api::make_object(transaction->id_, transaction->stars_, transaction->date_)); + } + + promise_.set_value( + td_api::make_object(result->balance_, std::move(transactions), result->next_offset_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class CanPurchasePremiumQuery final : public Td::ResultHandler { Promise promise_; @@ -1196,6 +1248,12 @@ void get_star_payment_options(Td *td, Promisecreate_handler(std::move(promise))->send(); } +void get_star_transactions(Td *td, const string &offset, + td_api::object_ptr &&direction, + Promise> &&promise) { + td->create_handler(std::move(promise))->send(offset, std::move(direction)); +} + void can_purchase_premium(Td *td, td_api::object_ptr &&purpose, Promise &&promise) { td->create_handler(std::move(promise))->send(std::move(purpose)); } diff --git a/td/telegram/Premium.h b/td/telegram/Premium.h index 3d0440d6c..a4bb53d83 100644 --- a/td/telegram/Premium.h +++ b/td/telegram/Premium.h @@ -56,6 +56,10 @@ void get_premium_giveaway_info(Td *td, MessageFullId message_full_id, void get_star_payment_options(Td *td, Promise> &&promise); +void get_star_transactions(Td *td, const string &offset, + td_api::object_ptr &&direction, + Promise> &&promise); + void can_purchase_premium(Td *td, td_api::object_ptr &&purpose, Promise &&promise); void assign_app_store_transaction(Td *td, const string &receipt, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index f35f6fbe2..d52ae1c37 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -9394,6 +9394,13 @@ void Td::on_request(uint64 id, const td_api::getStarPaymentOptions &request) { get_star_payment_options(this, std::move(promise)); } +void Td::on_request(uint64 id, td_api::getStarTransactions &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.offset_); + CREATE_REQUEST_PROMISE(); + get_star_transactions(this, request.offset_, std::move(request.direction_), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::canPurchaseFromStore &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index a2d636622..f126b1f77 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1835,6 +1835,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getStarPaymentOptions &request); + void on_request(uint64 id, td_api::getStarTransactions &request); + void on_request(uint64 id, td_api::canPurchaseFromStore &request); void on_request(uint64 id, td_api::assignAppStoreTransaction &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 443483be4..61e8ff3ea 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3382,6 +3382,14 @@ class CliClient final : public Actor { send_request(td_api::make_object(chat_id, message_id)); } else if (op == "gspo") { send_request(td_api::make_object()); + } else if (op == "gsta" || op == "gsti" || op == "gsto") { + td_api::object_ptr direction; + if (op == "gsti") { + direction = td_api::make_object(); + } else if (op == "gsto") { + direction = td_api::make_object(); + } + send_request(td_api::make_object(args, std::move(direction))); } else if (op == "cpfs" || op == "cpfsb") { UserId user_id; string currency;