From c627f5987d6ecf98862ccd7ed8b2f134a8594628 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 31 Aug 2022 15:47:51 +0300 Subject: [PATCH] Add getRecentPremiumStatuses. --- td/generate/scheme/td_api.tl | 3 ++ td/telegram/EmojiStatus.cpp | 60 ++++++++++++++++++++++++++++++++++-- td/telegram/EmojiStatus.h | 2 ++ td/telegram/Td.cpp | 6 ++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 2 ++ 6 files changed, 73 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2919c5ddf..348c1a5cb 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5578,6 +5578,9 @@ toggleBotIsAddedToAttachmentMenu bot_user_id:int53 is_added:Bool = Ok; //@description Returns default premium statuses getDefaultPremiumStatuses = PremiumStatuses; +//@description Returns recent premium statuses +getRecentPremiumStatuses = PremiumStatuses; + //@description Downloads a file from the cloud. Download progress and completion of the download will be notified through updateFile updates //@file_id Identifier of the file to download diff --git a/td/telegram/EmojiStatus.cpp b/td/telegram/EmojiStatus.cpp index 9e50e9f30..561eb552b 100644 --- a/td/telegram/EmojiStatus.cpp +++ b/td/telegram/EmojiStatus.cpp @@ -18,7 +18,7 @@ namespace td { struct EmojiStatuses { - int64 hash_ = 0; + int64 hash_ = -1; vector emoji_statuses_; td_api::object_ptr get_premium_statuses_object() const { @@ -63,6 +63,11 @@ static const string &get_default_emoji_statuses_database_key() { return key; } +static const string &get_recent_emoji_statuses_database_key() { + static string key = "rec_emoji_statuses"; + return key; +} + static EmojiStatuses load_emoji_statuses(const string &key) { EmojiStatuses result; auto log_event_string = G()->td_db()->get_binlog_pmc()->get(key); @@ -118,6 +123,48 @@ class GetDefaultEmojiStatusesQuery final : public Td::ResultHandler { } }; +class GetRecentEmojiStatusesQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetRecentEmojiStatusesQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(int64 hash) { + send_query(G()->net_query_creator().create(telegram_api::account_getRecentEmojiStatuses(hash))); + } + + 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 emoji_statuses_ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetRecentEmojiStatusesQuery: " << to_string(emoji_statuses_ptr); + + if (emoji_statuses_ptr->get_id() == telegram_api::account_emojiStatusesNotModified::ID) { + if (promise_) { + promise_.set_error(Status::Error(500, "Receive wrong server response")); + } + return; + } + + CHECK(emoji_statuses_ptr->get_id() == telegram_api::account_emojiStatuses::ID); + EmojiStatuses emoji_statuses(move_tl_object_as(emoji_statuses_ptr)); + save_emoji_statuses(get_recent_emoji_statuses_database_key(), emoji_statuses); + + if (promise_) { + promise_.set_value(emoji_statuses.get_premium_statuses_object()); + } + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + EmojiStatus::EmojiStatus(const td_api::object_ptr &premium_status) : custom_emoji_id_(premium_status != nullptr ? premium_status->custom_emoji_id_ : 0) { } @@ -151,11 +198,20 @@ StringBuilder &operator<<(StringBuilder &string_builder, const EmojiStatus &emoj void get_default_emoji_statuses(Td *td, Promise> &&promise) { auto statuses = load_emoji_statuses(get_default_emoji_statuses_database_key()); - if (statuses.hash_ != 0 && promise) { + if (statuses.hash_ != -1 && promise) { promise.set_value(statuses.get_premium_statuses_object()); promise = Promise>(); } td->create_handler(std::move(promise))->send(statuses.hash_); } +void get_recent_emoji_statuses(Td *td, Promise> &&promise) { + auto statuses = load_emoji_statuses(get_recent_emoji_statuses_database_key()); + if (statuses.hash_ != -1 && promise) { + promise.set_value(statuses.get_premium_statuses_object()); + promise = Promise>(); + } + td->create_handler(std::move(promise))->send(statuses.hash_); +} + } // namespace td diff --git a/td/telegram/EmojiStatus.h b/td/telegram/EmojiStatus.h index c7d76ff46..9193a1970 100644 --- a/td/telegram/EmojiStatus.h +++ b/td/telegram/EmojiStatus.h @@ -63,4 +63,6 @@ StringBuilder &operator<<(StringBuilder &string_builder, const EmojiStatus &emoj void get_default_emoji_statuses(Td *td, Promise> &&promise); +void get_recent_emoji_statuses(Td *td, Promise> &&promise); + } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 7cc800255..253a7e4b0 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6739,6 +6739,12 @@ void Td::on_request(uint64 id, const td_api::getDefaultPremiumStatuses &request) get_default_emoji_statuses(this, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getRecentPremiumStatuses &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + get_recent_emoji_statuses(this, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setCommands &request) { CHECK_IS_BOT(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index f508aa9e1..b4f87b7bd 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1016,6 +1016,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getDefaultPremiumStatuses &request); + void on_request(uint64 id, const td_api::getRecentPremiumStatuses &request); + void on_request(uint64 id, td_api::setCommands &request); void on_request(uint64 id, td_api::deleteCommands &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index c1c281706..6536748e6 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4479,6 +4479,8 @@ class CliClient final : public Actor { send_request(td_api::make_object(std::move(premium_status))); } else if (op == "gdps") { send_request(td_api::make_object()); + } else if (op == "grps") { + send_request(td_api::make_object()); } else if (op == "ccun") { ChatId chat_id; string username;