From 7092674c056b0f7154d8c275d8bc15031c752364 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 1 Jun 2022 15:14:09 +0300 Subject: [PATCH] Add td_api::getPremiumState. --- td/generate/scheme/td_api.tl | 16 +++- td/telegram/Premium.cpp | 149 +++++++++++++++++++++++++++-------- td/telegram/Premium.h | 2 + td/telegram/Td.cpp | 6 ++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 2 + 6 files changed, 143 insertions(+), 34 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 269f46805..004d5d157 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2974,6 +2974,17 @@ premiumSourceLink referrer:string = PremiumSource; premiumSourceSettings = PremiumSource; +//@description Describes a promotion video for a Premium feature @feature Premium feature @video Promotion video for the feature +premiumFeaturePromotionVideo feature:PremiumFeature video:video = PremiumFeaturePromotionVideo; + +//@description Contains state of Telegram Premium subscription and promotion videos for Premium features +//@state Text description of the state of the current Premium subscription; may be empty if the current user has no Telegram Premium subscription +//@currency ISO 4217 currency code for Telegram Premium subscription payment +//@monthly_amount Monthly subscription payment for Telegram Premium subscription, in the smallest units of the currency +//@videos The list of available promotion videos for Premium features +premiumState state:formattedText currency:string monthly_amount:int53 videos:vector = PremiumState; + + //@class DeviceToken @description Represents a data needed to subscribe for push notifications through registerDevice method. To use specific push notification service, the correct application platform must be specified and a valid server authentication data must be uploaded at https://my.telegram.org //@description A token for Firebase Cloud Messaging @token Device registration token; may be empty to deregister a device @encrypt True, if push notifications must be additionally encrypted @@ -4986,7 +4997,7 @@ editInlineMessageReplyMarkup inline_message_id:string reply_markup:ReplyMarkup = editMessageSchedulingState chat_id:int53 message_id:int53 scheduling_state:MessageSchedulingState = Ok; -//@description Returns reactions, which can be added to a message. The list can change after updateReactions, updateChatAvailableReactions for the chat, or updateMessageInteractionInfo for the message. The method will return Premium reactions, even the current user has no Premium account +//@description Returns reactions, which can be added to a message. The list can change after updateReactions, updateChatAvailableReactions for the chat, or updateMessageInteractionInfo for the message. The method will return Premium reactions, even the current user has no Premium subscription //@chat_id Identifier of the chat to which the message belongs //@message_id Identifier of the message getMessageAvailableReactions chat_id:int53 message_id:int53 = AvailableReactions; @@ -6347,6 +6358,9 @@ viewPremiumFeature feature:PremiumFeature = Ok; //@description Informs TDLib that the user clicked Premium subscription button on the Premium features screen clickPremiumSubscriptionButton = Ok; +//@description Returns state of Telegram Premium subscription and promotion videos for Premium features +getPremiumState = PremiumState; + //@description Accepts Telegram terms of services @terms_of_service_id Terms of service identifier acceptTermsOfService terms_of_service_id:string = Ok; diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index 1fac2123f..6c2c34dd3 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -8,13 +8,124 @@ #include "td/telegram/Application.h" #include "td/telegram/ConfigShared.h" +#include "td/telegram/DocumentsManager.h" #include "td/telegram/Global.h" +#include "td/telegram/MessageEntity.h" +#include "td/telegram/Td.h" +#include "td/telegram/VideosManager.h" #include "td/utils/algorithm.h" +#include "td/utils/buffer.h" #include "td/utils/SliceBuilder.h" namespace td { +static td_api::object_ptr get_premium_feature_object(Slice premium_feature) { + if (premium_feature == "double_limits") { + return td_api::make_object(); + } + if (premium_feature == "more_upload") { + return td_api::make_object(); + } + if (premium_feature == "faster_download") { + return td_api::make_object(); + } + if (premium_feature == "voice_to_text") { + return td_api::make_object(); + } + if (premium_feature == "no_ads") { + return td_api::make_object(); + } + if (premium_feature == "unique_reactions") { + return td_api::make_object(); + } + if (premium_feature == "premium_stickers") { + return td_api::make_object(); + } + if (premium_feature == "advanced_chat_management") { + return td_api::make_object(); + } + if (premium_feature == "profile_badge") { + return td_api::make_object(); + } + if (premium_feature == "animated_userpics") { + return td_api::make_object(); + } + return nullptr; +} + +class GetPremiumPromoQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetPremiumPromoQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(telegram_api::help_getPremiumPromo())); + } + + 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 promo = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetPremiumPromoQuery: " << to_string(promo); + + auto state = get_message_text(td_->contacts_manager_.get(), std::move(promo->status_text_), + std::move(promo->status_entities_), true, true, 0, false, "GetPremiumPromoQuery"); + + if (promo->video_sections_.size() != promo->videos_.size()) { + return on_error(Status::Error(500, "Receive wrong number of videos")); + } + + if (promo->monthly_amount_ < 0 || promo->monthly_amount_ > 9999'9999'9999) { + return on_error(Status::Error(500, "Receive invalid monthly amount")); + } + + if (promo->currency_.size() != 3) { + return on_error(Status::Error(500, "Receive invalid currency")); + } + + vector> videos; + for (size_t i = 0; i < promo->video_sections_.size(); i++) { + auto feature = get_premium_feature_object(promo->video_sections_[i]); + if (feature == nullptr) { + continue; + } + + auto video = std::move(promo->videos_[i]); + if (video->get_id() != telegram_api::document::ID) { + LOG(ERROR) << "Receive " << to_string(video) << " for " << promo->video_sections_[i]; + continue; + } + + auto parsed_document = td_->documents_manager_->on_get_document(move_tl_object_as(video), + DialogId(), nullptr, Document::Type::Video); + + if (parsed_document.type != Document::Type::Video) { + LOG(ERROR) << "Receive " << parsed_document.type << " for " << promo->video_sections_[i]; + continue; + } + + auto video_object = td_->videos_manager_->get_video_object(parsed_document.file_id); + videos.push_back( + td_api::make_object(std::move(feature), std::move(video_object))); + } + + promise_.set_value(td_api::make_object(get_formatted_text_object(state, true, 0), + std::move(promo->currency_), promo->monthly_amount_, + std::move(videos))); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + const vector &get_premium_limit_keys() { static const vector limit_keys{"channels", "saved_gifs", @@ -190,39 +301,7 @@ void get_premium_features(Td *td, const td_api::object_ptr> features; for (const auto &premium_feature : premium_features) { - auto feature = [&]() -> td_api::object_ptr { - if (premium_feature == "double_limits") { - return td_api::make_object(); - } - if (premium_feature == "more_upload") { - return td_api::make_object(); - } - if (premium_feature == "faster_download") { - return td_api::make_object(); - } - if (premium_feature == "voice_to_text") { - return td_api::make_object(); - } - if (premium_feature == "no_ads") { - return td_api::make_object(); - } - if (premium_feature == "unique_reactions") { - return td_api::make_object(); - } - if (premium_feature == "premium_stickers") { - return td_api::make_object(); - } - if (premium_feature == "advanced_chat_management") { - return td_api::make_object(); - } - if (premium_feature == "profile_badge") { - return td_api::make_object(); - } - if (premium_feature == "animated_userpics") { - return td_api::make_object(); - } - return nullptr; - }(); + auto feature = get_premium_feature_object(premium_feature); if (feature != nullptr) { features.push_back(std::move(feature)); } @@ -280,4 +359,8 @@ void click_premium_subscription_button(Td *td, Promise &&promise) { std::move(promise)); } +void get_premium_state(Td *td, Promise> &&promise) { + td->create_handler(std::move(promise))->send(); +} + } // namespace td diff --git a/td/telegram/Premium.h b/td/telegram/Premium.h index cbcb0374a..7519cb7d9 100644 --- a/td/telegram/Premium.h +++ b/td/telegram/Premium.h @@ -29,4 +29,6 @@ void view_premium_feature(Td *td, const td_api::object_ptr &&promise); +void get_premium_state(Td *td, Promise> &&promise); + } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index b51183452..daca18349 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7867,6 +7867,12 @@ void Td::on_request(uint64 id, const td_api::clickPremiumSubscriptionButton &req click_premium_subscription_button(this, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getPremiumState &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + get_premium_state(this, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::acceptTermsOfService &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.terms_of_service_id_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e7659b404..8bf31c026 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1301,6 +1301,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::clickPremiumSubscriptionButton &request); + void on_request(uint64 id, const td_api::getPremiumState &request); + void on_request(uint64 id, td_api::acceptTermsOfService &request); void on_request(uint64 id, const td_api::getCountries &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5cfafc4c8..eaba8f28f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2555,6 +2555,8 @@ class CliClient final : public Actor { send_request(td_api::make_object(std::move(feature))); } else if (op == "cprsb") { send_request(td_api::make_object()); + } else if (op == "gprs") { + send_request(td_api::make_object()); } else if (op == "atos") { send_request(td_api::make_object(args)); } else if (op == "gdli") {