From fad7d022fb9ac08e6128136a4528cf999052e2c8 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Mar 2024 13:49:01 +0300 Subject: [PATCH] Add td_api::replaceStickerInSet. --- td/generate/scheme/td_api.tl | 9 ++++- td/telegram/StickersManager.cpp | 64 ++++++++++++++++++++++----------- td/telegram/StickersManager.h | 7 ++-- td/telegram/Td.cpp | 11 ++++++ td/telegram/Td.h | 2 ++ 5 files changed, 68 insertions(+), 25 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index d17a4ce0d..b64337d12 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -10108,6 +10108,13 @@ createNewStickerSet user_id:int53 title:string name:string sticker_type:StickerT //@sticker Sticker to add to the set addStickerToSet user_id:int53 name:string sticker:inputSticker = Ok; +//@description Replaces existing sticker in a set. The function is equivalent to removeStickerFromSet, then addStickerToSet, then setStickerPositionInSet +//@user_id Sticker set owner; ignored for regular users +//@name Sticker set name. The sticker set must have been created by the current user +//@old_sticker Sticker to remove from the set +//@new_sticker Sticker to add to the set +replaceStickerInSet user_id:int53 name:string old_sticker:InputFile new_sticker:inputSticker = Ok; + //@description Sets a sticker set thumbnail //@user_id Sticker set owner; ignored for regular users //@name Sticker set name. The sticker set must have been created by the current user @@ -10131,7 +10138,7 @@ deleteStickerSet name:string = Ok; //@position New position of the sticker in the set, 0-based setStickerPositionInSet sticker:InputFile position:int32 = Ok; -//@description Removes a sticker from the set to which it belongs. The sticker set must have been created by the current user @sticker Sticker +//@description Removes a sticker from the set to which it belongs. The sticker set must have been created by the current user @sticker Sticker to remove from the set removeStickerFromSet sticker:InputFile = Ok; //@description Changes the list of emoji corresponding to a sticker. The sticker must belong to a regular or custom emoji sticker set created by the current user diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 47710ceab..769f01703 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1104,14 +1104,23 @@ class AddStickerToSetQuery final : public Td::ResultHandler { explicit AddStickerToSetQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(const string &short_name, tl_object_ptr &&input_sticker) { - send_query(G()->net_query_creator().create( - telegram_api::stickers_addStickerToSet(make_tl_object(short_name), - std::move(input_sticker)), - {{short_name}})); + void send(const string &short_name, telegram_api::object_ptr &&input_sticker, + telegram_api::object_ptr &&input_document) { + if (input_document != nullptr) { + send_query(G()->net_query_creator().create( + telegram_api::stickers_replaceSticker(std::move(input_document), std::move(input_sticker)), {{short_name}})); + } else { + send_query(G()->net_query_creator().create( + telegram_api::stickers_addStickerToSet(make_tl_object(short_name), + std::move(input_sticker)), + {{short_name}})); + } } void on_result(BufferSlice packet) final { + static_assert(std::is_same::value, + ""); auto result_ptr = fetch_result(packet); if (result_ptr.is_error()) { return on_error(result_ptr.move_as_error()); @@ -8182,7 +8191,8 @@ StickerFormat StickersManager::guess_sticker_set_format(const StickerSet *sticke } void StickersManager::add_sticker_to_set(UserId user_id, string short_name, - td_api::object_ptr &&sticker, Promise &&promise) { + td_api::object_ptr &&sticker, + td_api::object_ptr &&old_sticker, Promise &&promise) { bool is_bot = td_->auth_manager_->is_bot(); if (!is_bot) { user_id = td_->contacts_manager_->get_my_id(); @@ -8197,25 +8207,27 @@ void StickersManager::add_sticker_to_set(UserId user_id, string short_name, const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name)); if (sticker_set != nullptr && sticker_set->was_loaded_) { - return do_add_sticker_to_set(user_id, short_name, std::move(sticker), std::move(promise)); + return do_add_sticker_to_set(user_id, short_name, std::move(sticker), std::move(old_sticker), std::move(promise)); } - do_reload_sticker_set( - StickerSetId(), make_tl_object(short_name), 0, - PromiseCreator::lambda([actor_id = actor_id(this), user_id, short_name, sticker = std::move(sticker), - promise = std::move(promise)](Result result) mutable { - if (result.is_error()) { - promise.set_error(result.move_as_error()); - } else { - send_closure(actor_id, &StickersManager::do_add_sticker_to_set, user_id, std::move(short_name), - std::move(sticker), std::move(promise)); - } - }), - "add_sticker_to_set"); + do_reload_sticker_set(StickerSetId(), make_tl_object(short_name), 0, + PromiseCreator::lambda([actor_id = actor_id(this), user_id, short_name, + sticker = std::move(sticker), old_sticker = std::move(old_sticker), + promise = std::move(promise)](Result result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + send_closure(actor_id, &StickersManager::do_add_sticker_to_set, user_id, + std::move(short_name), std::move(sticker), std::move(old_sticker), + std::move(promise)); + } + }), + "add_sticker_to_set"); } void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, td_api::object_ptr &&sticker, + td_api::object_ptr &&old_sticker, Promise &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); @@ -8223,13 +8235,21 @@ void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, if (sticker_set == nullptr || !sticker_set->was_loaded_) { return promise.set_error(Status::Error(400, "Sticker set not found")); } + telegram_api::object_ptr input_document; + if (old_sticker != nullptr) { + TRY_RESULT_PROMISE(promise, sticker_input_document, get_sticker_input_document(old_sticker)); + if (sticker_input_document.sticker_set_short_name_ != short_name) { + return promise.set_error(Status::Error(400, "The old sticker isn't from the set")); + } + input_document = std::move(sticker_input_document.input_document_); + } + if (sticker != nullptr && sticker->format_ == nullptr) { auto format = guess_sticker_set_format(sticker_set); if (format != StickerFormat::Unknown) { sticker->format_ = get_sticker_format_object(format); } } - auto r_file_id = prepare_input_sticker(sticker.get(), sticker_set->sticker_type_); if (r_file_id.is_error()) { return promise.set_error(r_file_id.move_as_error()); @@ -8242,6 +8262,7 @@ void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, pending_add_sticker_to_set->short_name_ = short_name; pending_add_sticker_to_set->file_id_ = file_id; pending_add_sticker_to_set->sticker_ = std::move(sticker); + pending_add_sticker_to_set->input_document_ = std::move(input_document); pending_add_sticker_to_set->promise_ = std::move(promise); int64 random_id; @@ -8281,7 +8302,8 @@ void StickersManager::on_added_sticker_uploaded(int64 random_id, Result re td_->create_handler(std::move(pending_add_sticker_to_set->promise_)) ->send(pending_add_sticker_to_set->short_name_, - get_input_sticker(pending_add_sticker_to_set->sticker_.get(), pending_add_sticker_to_set->file_id_)); + get_input_sticker(pending_add_sticker_to_set->sticker_.get(), pending_add_sticker_to_set->file_id_), + std::move(pending_add_sticker_to_set->input_document_)); } void StickersManager::set_sticker_set_thumbnail(UserId user_id, string short_name, diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index ad7cbc766..afebb3b1c 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -294,7 +294,7 @@ class StickersManager final : public Actor { string software, Promise> &&promise); void add_sticker_to_set(UserId user_id, string short_name, td_api::object_ptr &&sticker, - Promise &&promise); + td_api::object_ptr &&old_sticker, Promise &&promise); void set_sticker_set_thumbnail(UserId user_id, string short_name, td_api::object_ptr &&thumbnail, StickerFormat format, Promise &&promise); @@ -531,7 +531,8 @@ class StickersManager final : public Actor { struct PendingAddStickerToSet { string short_name_; FileId file_id_; - tl_object_ptr sticker_; + td_api::object_ptr sticker_; + telegram_api::object_ptr input_document_; Promise promise_; }; @@ -817,7 +818,7 @@ class StickersManager final : public Actor { StickerFormat guess_sticker_set_format(const StickerSet *sticker_set) const; void do_add_sticker_to_set(UserId user_id, string short_name, td_api::object_ptr &&sticker, - Promise &&promise); + td_api::object_ptr &&old_sticker, 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 e34b4f045..0df84a064 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8221,6 +8221,17 @@ void Td::on_request(uint64 id, td_api::addStickerToSet &request) { CLEAN_INPUT_STRING(request.name_); CREATE_OK_REQUEST_PROMISE(); stickers_manager_->add_sticker_to_set(UserId(request.user_id_), std::move(request.name_), std::move(request.sticker_), + nullptr, std::move(promise)); +} + +void Td::on_request(uint64 id, td_api::replaceStickerInSet &request) { + CLEAN_INPUT_STRING(request.name_); + CREATE_OK_REQUEST_PROMISE(); + if (request.old_sticker_ == nullptr) { + return send_error_raw(id, 400, "Old sticker must be non-empty"); + } + stickers_manager_->add_sticker_to_set(UserId(request.user_id_), std::move(request.name_), + std::move(request.new_sticker_), std::move(request.old_sticker_), std::move(promise)); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 2944e84e0..257597db1 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1506,6 +1506,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::addStickerToSet &request); + void on_request(uint64 id, td_api::replaceStickerInSet &request); + void on_request(uint64 id, td_api::setStickerSetThumbnail &request); void on_request(uint64 id, td_api::setCustomEmojiStickerSetThumbnail &request);