From 57de8b5b0980c93156c49d23dcaad550db900fb6 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 8 Aug 2022 17:01:04 +0300 Subject: [PATCH] Use request promise in addStickerToSet. --- td/telegram/StickersManager.cpp | 21 +++++++++++++-------- td/telegram/StickersManager.h | 8 ++++---- td/telegram/Td.cpp | 31 +++---------------------------- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index dbf710bf8..573b12303 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1080,10 +1080,11 @@ class CreateNewStickerSetQuery final : public Td::ResultHandler { }; class AddStickerToSetQuery final : public Td::ResultHandler { - Promise promise_; + Promise> promise_; public: - explicit AddStickerToSetQuery(Promise &&promise) : promise_(std::move(promise)) { + explicit AddStickerToSetQuery(Promise> &&promise) + : promise_(std::move(promise)) { } void send(const string &short_name, tl_object_ptr &&input_sticker) { @@ -1099,10 +1100,10 @@ class AddStickerToSetQuery final : public Td::ResultHandler { return on_error(result_ptr.move_as_error()); } - td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(), true, - "AddStickerToSetQuery"); + auto sticker_set_id = td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(), + true, "AddStickerToSetQuery"); - promise_.set_value(Unit()); + promise_.set_value(td_->stickers_manager_->get_sticker_set_object(sticker_set_id)); } void on_error(Status status) final { @@ -7108,8 +7109,9 @@ void StickersManager::on_new_stickers_uploaded(int64 random_id, Result res sticker_format, std::move(input_stickers), pending_new_sticker_set->software_); } -void StickersManager::add_sticker_to_set(UserId user_id, string &short_name, - tl_object_ptr &&sticker, Promise &&promise) { +void StickersManager::add_sticker_to_set(UserId user_id, string short_name, + tl_object_ptr &&sticker, + Promise> &&promise) { TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(user_id)); short_name = clean_username(strip_empty_characters(short_name, MAX_STICKER_SET_SHORT_NAME_LENGTH)); @@ -7136,7 +7138,8 @@ void StickersManager::add_sticker_to_set(UserId user_id, string &short_name, } void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, - tl_object_ptr &&sticker, Promise &&promise) { + tl_object_ptr &&sticker, + Promise> &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name)); @@ -7186,6 +7189,8 @@ void StickersManager::on_added_sticker_uploaded(int64 random_id, Result re pending_add_sticker_to_sets_.erase(it); + TRY_STATUS_PROMISE(pending_add_sticker_to_set->promise_, G()->close_status()); + if (result.is_error()) { pending_add_sticker_to_set->promise_.set_error(result.move_as_error()); return; diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index cbabe1b28..014a0270d 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -247,8 +247,8 @@ class StickersManager final : public Actor { vector> &&stickers, string software, Promise &&promise); - void add_sticker_to_set(UserId user_id, string &short_name, tl_object_ptr &&sticker, - Promise &&promise); + void add_sticker_to_set(UserId user_id, string short_name, tl_object_ptr &&sticker, + Promise> &&promise); void set_sticker_set_thumbnail(UserId user_id, string &short_name, tl_object_ptr &&thumbnail, Promise &&promise); @@ -455,7 +455,7 @@ class StickersManager final : public Actor { string short_name_; FileId file_id_; tl_object_ptr sticker_; - Promise<> promise_; + Promise> promise_; }; struct PendingSetStickerSetThumbnail { @@ -744,7 +744,7 @@ class StickersManager final : public Actor { void on_added_sticker_uploaded(int64 random_id, Result result); void do_add_sticker_to_set(UserId user_id, string short_name, tl_object_ptr &&sticker, - Promise &&promise); + Promise> &&promise); void on_sticker_set_thumbnail_uploaded(int64 random_id, Result result); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 955ded16b..e636db1f7 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2255,33 +2255,6 @@ class CreateNewStickerSetRequest final : public RequestOnceActor { } }; -class AddStickerToSetRequest final : public RequestOnceActor { - UserId user_id_; - string name_; - tl_object_ptr sticker_; - - void do_run(Promise &&promise) final { - td_->stickers_manager_->add_sticker_to_set(user_id_, name_, std::move(sticker_), std::move(promise)); - } - - void do_send_result() final { - auto set_id = td_->stickers_manager_->search_sticker_set(name_, Auto()); - if (!set_id.is_valid()) { - return send_error(Status::Error(500, "Sticker set not found")); - } - send_result(td_->stickers_manager_->get_sticker_set_object(set_id)); - } - - public: - AddStickerToSetRequest(ActorShared td, uint64 request_id, int64 user_id, string &&name, - tl_object_ptr &&sticker) - : RequestOnceActor(std::move(td), request_id) - , user_id_(user_id) - , name_(std::move(name)) - , sticker_(std::move(sticker)) { - } -}; - class SetStickerSetThumbnailRequest final : public RequestOnceActor { UserId user_id_; string name_; @@ -7095,7 +7068,9 @@ void Td::on_request(uint64 id, td_api::createNewStickerSet &request) { void Td::on_request(uint64 id, td_api::addStickerToSet &request) { CHECK_IS_BOT(); CLEAN_INPUT_STRING(request.name_); - CREATE_REQUEST(AddStickerToSetRequest, request.user_id_, std::move(request.name_), std::move(request.sticker_)); + CREATE_REQUEST_PROMISE(); + stickers_manager_->add_sticker_to_set(UserId(request.user_id_), std::move(request.name_), std::move(request.sticker_), + std::move(promise)); } void Td::on_request(uint64 id, td_api::setStickerSetThumbnail &request) {