From f7ab0329766f984257ca81af0a4aa3ccc7478b6a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Mar 2024 17:55:27 +0300 Subject: [PATCH] Add td_api::getCollectibleItemInfo. --- td/generate/scheme/td_api.tl | 31 ++++++++++++++--- td/telegram/Payments.cpp | 67 ++++++++++++++++++++++++++++++++++++ td/telegram/Payments.h | 3 ++ td/telegram/Td.cpp | 5 +++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 6 ++++ 6 files changed, 110 insertions(+), 4 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 92330f8f7..eb2d35357 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -851,7 +851,7 @@ emojiStatuses custom_emoji_ids:vector = EmojiStatuses; //@description Describes usernames assigned to a user, a supergroup, or a channel //@active_usernames List of active usernames; the first one must be shown as the primary username. The order of active usernames can be changed with reorderActiveUsernames, reorderBotActiveUsernames or reorderSupergroupActiveUsernames //@disabled_usernames List of currently disabled usernames; the username can be activated with toggleUsernameIsActive, toggleBotUsernameIsActive, or toggleSupergroupUsernameIsActive -//@editable_username The active username, which can be changed with setUsername or setSupergroupUsername +//@editable_username The active username, which can be changed with setUsername or setSupergroupUsername. Information about other usernames can be received using getCollectibleItemInfo usernames active_usernames:vector disabled_usernames:vector editable_username:string = Usernames; @@ -2356,10 +2356,29 @@ countries countries:vector = Countries; //@country Information about the country to which the phone number belongs; may be null //@country_calling_code The part of the phone number denoting country calling code or its part //@formatted_phone_number The phone number without country calling code formatted accordingly to local rules. Expected digits are returned as '-', but even more digits might be entered by the user -//@is_anonymous True, if the phone number was bought on Fragment and isn't tied to a SIM card +//@is_anonymous True, if the phone number was bought at https://fragment.com and isn't tied to a SIM card. Information about the phone number can be received using getCollectibleItemInfo phoneNumberInfo country:countryInfo country_calling_code:string formatted_phone_number:string is_anonymous:Bool = PhoneNumberInfo; +//@class CollectibleItemType @description Describes a collectible item that can be purchased at https://fragment.com + +//@description A username @username The username +collectibleItemTypeUsername username:string = CollectibleItemType; + +//@description A phone number @phone_number The phone number +collectibleItemTypePhoneNumber phone_number:string = CollectibleItemType; + + +//@description Contains information about a collectible item and its last purchase +//@purchase_date Point in time (Unix timestamp) when the item was purchased +//@currency Currency for the paid amount +//@amount The paid amount, in the smallest units of the currency +//@cryptocurrency Cryptocurrency used to pay for the item +//@cryptocurrency_amount The paid amount, in the smallest units of the cryptocurrency +//@url Individual URL for the item on https://fragment.com +collectibleItemInfo purchase_date:int32 currency:string amount:int53 cryptocurrency:string cryptocurrency_amount:int64 url:string = CollectibleItemInfo; + + //@description Describes an action associated with a bank card number @text Action text @url The URL to be opened bankCardActionOpenUrl text:string url:string = BankCardActionOpenUrl; @@ -2543,7 +2562,7 @@ messageExtendedMediaUnsupported caption:formattedText = MessageExtendedMedia; //@only_new_members True, if only new members of the chats will be eligible for the giveaway //@has_public_winners True, if the list of winners of the giveaway will be available to everyone //@country_codes The list of two-letter ISO 3166-1 alpha-2 codes of countries, users from which will be eligible for the giveaway. If empty, then all users can participate in the giveaway. -//-There can be up to getOption("giveaway_country_count_max") chosen countries. Users with phone number that was bought on Fragment can participate in any giveaway and the country code "FT" must not be specified in the list +//-There can be up to getOption("giveaway_country_count_max") chosen countries. Users with phone number that was bought at https://fragment.com can participate in any giveaway and the country code "FT" must not be specified in the list //@prize_description Additional description of the giveaway prize; 0-128 characters premiumGiveawayParameters boosted_chat_id:int53 additional_chat_ids:vector winners_selection_date:int32 only_new_members:Bool has_public_winners:Bool country_codes:vector prize_description:string = PremiumGiveawayParameters; @@ -5197,7 +5216,7 @@ checkChatUsernameResultUsernameInvalid = CheckChatUsernameResult; //@description The username is occupied checkChatUsernameResultUsernameOccupied = CheckChatUsernameResult; -//@description The username can be purchased at fragment.com +//@description The username can be purchased at https://fragment.com. Information about the username can be received using getCollectibleItemInfo checkChatUsernameResultUsernamePurchasable = CheckChatUsernameResult; //@description The user has too many chats with username, one of them must be made private first @@ -10265,6 +10284,10 @@ getPhoneNumberInfo phone_number_prefix:string = PhoneNumberInfo; getPhoneNumberInfoSync language_code:string phone_number_prefix:string = PhoneNumberInfo; +//@description Returns information about a given collectible item that was purchased at https://fragment.com @type Type of the collectible item +getCollectibleItemInfo type:CollectibleItemType = CollectibleItemInfo; + + //@description Returns information about a tg:// deep link. Use "tg://need_update_for_some_feature" or "tg:some_unsupported_feature" for testing. Returns a 404 error for unknown links. Can be called before authorization @link The link getDeepLinkInfo link:string = DeepLinkInfo; diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index b9b67857f..ffd0185e0 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -765,6 +765,44 @@ class GetBankCardInfoQuery final : public Td::ResultHandler { } }; +class GetCollectibleInfoQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetCollectibleInfoQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(telegram_api::object_ptr &&input_collectible) { + send_query( + G()->net_query_creator().create(telegram_api::fragment_getCollectibleInfo(std::move(input_collectible)))); + } + + 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(); + if (result->amount_ <= 0 || !check_currency_amount(result->amount_)) { + LOG(ERROR) << "Receive invalid collectible item price " << result->amount_; + result->amount_ = 0; + } + if (result->crypto_currency_.empty() || result->crypto_amount_ <= 0) { + LOG(ERROR) << "Receive invalid collectible item cryptocurrency price " << result->crypto_amount_; + result->crypto_amount_ = 0; + } + promise_.set_value(td_api::make_object(result->purchase_date_, result->currency_, + result->amount_, result->crypto_currency_, + result->crypto_amount_, result->url_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + void answer_shipping_query(Td *td, int64 shipping_query_id, vector> &&shipping_options, const string &error_message, Promise &&promise) { @@ -958,4 +996,33 @@ void get_bank_card_info(Td *td, const string &bank_card_number, td->create_handler(std::move(promise))->send(bank_card_number); } +void get_collectible_info(Td *td, td_api::object_ptr type, + Promise> &&promise) { + if (type == nullptr) { + return promise.set_error(Status::Error(400, "Item type must be non-empty")); + } + switch (type->get_id()) { + case td_api::collectibleItemTypeUsername::ID: { + auto username = td_api::move_object_as(type); + if (!clean_input_string(username->username_)) { + return promise.set_error(Status::Error(400, "Username must be encoded in UTF-8")); + } + td->create_handler(std::move(promise)) + ->send(telegram_api::make_object(username->username_)); + break; + } + case td_api::collectibleItemTypePhoneNumber::ID: { + auto phone_number = td_api::move_object_as(type); + if (!clean_input_string(phone_number->phone_number_)) { + return promise.set_error(Status::Error(400, "Phone number must be encoded in UTF-8")); + } + td->create_handler(std::move(promise)) + ->send(telegram_api::make_object(phone_number->phone_number_)); + break; + } + default: + UNREACHABLE(); + } +} + } // namespace td diff --git a/td/telegram/Payments.h b/td/telegram/Payments.h index 07fbf0066..667fa9db8 100644 --- a/td/telegram/Payments.h +++ b/td/telegram/Payments.h @@ -51,4 +51,7 @@ void export_invoice(Td *td, td_api::object_ptr &&in void get_bank_card_info(Td *td, const string &bank_card_number, Promise> &&promise); +void get_collectible_info(Td *td, td_api::object_ptr type, + Promise> &&promise); + } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9296b661e..7071cd6e1 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -9322,6 +9322,11 @@ void Td::on_request(uint64 id, const td_api::getPhoneNumberInfo &request) { country_info_manager_->get_phone_number_info(request.phone_number_prefix_, std::move(promise)); } +void Td::on_request(uint64 id, td_api::getCollectibleItemInfo &request) { + CREATE_REQUEST_PROMISE(); + get_collectible_info(this, std::move(request.type_), std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getApplicationDownloadLink &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 6d0c7be9b..445161aca 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1794,6 +1794,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getPhoneNumberInfo &request); + void on_request(uint64 id, td_api::getCollectibleItemInfo &request); + void on_request(uint64 id, const td_api::getApplicationDownloadLink &request); void on_request(uint64 id, td_api::getDeepLinkInfo &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index c1f55e095..f7f86e30b 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3275,6 +3275,12 @@ class CliClient final : public Actor { send_request(td_api::make_object(args)); } else if (op == "gpnis") { execute(td_api::make_object(rand_bool() ? "en" : "", args)); + } else if (op == "gciu") { + send_request(td_api::make_object( + td_api::make_object(args))); + } else if (op == "gcipn") { + send_request(td_api::make_object( + td_api::make_object(args))); } else if (op == "gadl") { send_request(td_api::make_object()); } else if (op == "gprl") {