From 6aeaed74b5dae30b72782634859f1d46db484eeb Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 15 Apr 2026 15:27:33 +0300 Subject: [PATCH] Add and use Client::get_sticker_set_names. --- telegram-bot-api/Client.cpp | 59 ++++++++++++++++++++++++------------- telegram-bot-api/Client.h | 4 ++- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 04028b7..77a6414 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -7726,15 +7726,19 @@ class Client::TdOnReturnStickerSetCallback final : public TdQueryCallback { PromisedQueryPtr query_; }; -class Client::TdOnGetStickerSetPromiseCallback final : public TdQueryCallback { +class Client::TdOnGetStickerSetNameCallback final : public TdQueryCallback { public: - TdOnGetStickerSetPromiseCallback(Client *client, int64 sticker_set_id, td::Promise &&promise) + TdOnGetStickerSetNameCallback(Client *client, int64 sticker_set_id, td::Promise &&promise) : client_(client), sticker_set_id_(sticker_set_id), promise_(std::move(promise)) { } void on_result(object_ptr result) final { if (result->get_id() == td_api::error::ID) { auto error = move_object_as(result); + if (error->message_ == "STICKERSET_INVALID") { + client_->on_get_sticker_set_name(sticker_set_id_, td::string()); + return promise_.set_value(td::Unit()); + } return promise_.set_error(td::Status::Error(error->code_, error->message_)); } @@ -7760,27 +7764,16 @@ class Client::TdOnGetStickersCallback final : public TdQueryCallback { CHECK(result->get_id() == td_api::stickers::ID); auto stickers = move_object_as(result); - td::FlatHashSet sticker_set_ids; + td::vector sticker_set_ids; for (const auto &sticker : stickers->stickers_) { - if (sticker->set_id_ != 0 && client_->get_sticker_set_name(sticker->set_id_).empty()) { - sticker_set_ids.insert(sticker->set_id_); - } + sticker_set_ids.push_back(sticker->set_id_); } - - td::MultiPromiseActorSafe mpas("GetStickerSetsMultiPromiseActor"); - mpas.add_promise(td::PromiseCreator::lambda([actor_id = client_->actor_id(client_), stickers = std::move(stickers), - query = std::move(query_)](td::Unit) mutable { - send_closure(actor_id, &Client::return_stickers, std::move(stickers), std::move(query)); - })); - mpas.set_ignore_errors(true); - - auto lock = mpas.get_promise(); - for (auto sticker_set_id : sticker_set_ids) { - client_->send_request( - make_object(sticker_set_id), - td::make_unique(client_, sticker_set_id, mpas.get_promise())); - } - lock.set_value(td::Unit()); + client_->get_sticker_set_names( + std::move(sticker_set_ids), + td::PromiseCreator::lambda([actor_id = client_->actor_id(client_), stickers = std::move(stickers), + query = std::move(query_)](td::Unit) mutable { + send_closure(actor_id, &Client::return_stickers, std::move(stickers), std::move(query)); + })); } private: @@ -8100,6 +8093,30 @@ void Client::on_get_sticker_set(int64 set_id, int64 new_callback_query_user_id, } } +void Client::get_sticker_set_names(td::vector sticker_set_ids, td::Promise &&promise) { + td::FlatHashSet missing_sticker_set_ids; + for (auto sticker_set_id : sticker_set_ids) { + if (sticker_set_id != 0 && !have_sticker_set_name(sticker_set_id)) { + missing_sticker_set_ids.insert(sticker_set_id); + } + } + if (missing_sticker_set_ids.empty()) { + // fast pass + return promise.set_value(td::Unit()); + } + + td::MultiPromiseActorSafe mpas("GetStickerSetNamesMultiPromiseActor"); + mpas.add_promise(std::move(promise)); + mpas.set_ignore_errors(true); + + auto lock = mpas.get_promise(); + for (auto sticker_set_id : missing_sticker_set_ids) { + send_request(make_object(sticker_set_id), + td::make_unique(this, sticker_set_id, mpas.get_promise())); + } + lock.set_value(td::Unit()); +} + void Client::on_get_sticker_set_name(int64 set_id, const td::string &name) { CHECK(set_id != 0); if (set_id != GREAT_MINDS_SET_ID) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index aa521fc..76ba65f 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -310,7 +310,7 @@ class Client final : public WebhookActor::Callback { class TdOnSavePreparedKeyboardButtonCallback; class TdOnReturnFileCallback; class TdOnReturnStickerSetCallback; - class TdOnGetStickerSetPromiseCallback; + class TdOnGetStickerSetNameCallback; class TdOnGetStickersCallback; class TdOnDownloadFileCallback; class TdOnCancelDownloadFileCallback; @@ -326,6 +326,8 @@ class Client final : public WebhookActor::Callback { const td::string &new_message_business_connection_id, int64 new_business_callback_query_user_id, object_ptr sticker_set_name); + void get_sticker_set_names(td::vector sticker_set_ids, td::Promise &&promise); + void on_get_sticker_set_name(int64 set_id, const td::string &name); void on_get_sticker_set_name(int64 set_id, object_ptr sticker_set_name);