From 22d2f6a98d31bc339cece8a79b8e55bfa8e3ebfb Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Mar 2024 15:41:49 +0300 Subject: [PATCH] Add td_api::getOwnedStickerSets. --- td/generate/scheme/td_api.tl | 10 ++++- td/telegram/StickersManager.cpp | 77 ++++++++++++++++++++++++++++++++- td/telegram/StickersManager.h | 6 +++ td/telegram/Td.cpp | 7 +++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 5 +++ 6 files changed, 105 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index f5a7b8db9..92330f8f7 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9296,7 +9296,10 @@ getPremiumStickers limit:int32 = Stickers; //@description Returns a list of installed sticker sets @sticker_type Type of the sticker sets to return getInstalledStickerSets sticker_type:StickerType = StickerSets; -//@description Returns a list of archived sticker sets @sticker_type Type of the sticker sets to return @offset_sticker_set_id Identifier of the sticker set from which to return the result @limit The maximum number of sticker sets to return; up to 100 +//@description Returns a list of archived sticker sets +//@sticker_type Type of the sticker sets to return +//@offset_sticker_set_id Identifier of the sticker set from which to return the result; use 0 to get results from the beginning +//@limit The maximum number of sticker sets to return; up to 100 getArchivedStickerSets sticker_type:StickerType offset_sticker_set_id:int64 limit:int32 = StickerSets; //@description Returns a list of trending sticker sets. For optimal performance, the number of returned sticker sets is chosen by TDLib @@ -10156,6 +10159,11 @@ setStickerKeywords sticker:InputFile keywords:vector = Ok; //@mask_position Position where the mask is placed; pass null to remove mask position setStickerMaskPosition sticker:InputFile mask_position:maskPosition = Ok; +//@description Returns sticker sets owned by the current user +//@offset_sticker_set_id Identifier of the sticker set from which to return owned sticker sets; use 0 to get results from the beginning +//@limit The maximum number of sticker sets to be returned; must be positive and can't be greater than 100. For optimal performance, the number of returned objects is chosen by TDLib and can be smaller than the specified limit +getOwnedStickerSets offset_sticker_set_id:int64 limit:int32 = StickerSets; + //@description Returns information about a file with a map thumbnail in PNG format. Only map thumbnail files with size less than 1MB can be downloaded //@location Location of the map center diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 769f01703..441e7fa09 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1386,6 +1386,35 @@ class ChangeStickerQuery final : public Td::ResultHandler { } }; +class GetMyStickersQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetMyStickersQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(StickerSetId offset_sticker_set_id, int32 limit) { + send_query( + G()->net_query_creator().create(telegram_api::messages_getMyStickers(offset_sticker_set_id.get(), limit))); + } + + 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 ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetMyStickersQuery: " << to_string(ptr); + promise_.set_value(std::move(ptr)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class GetCustomEmojiDocumentsQuery final : public Td::ResultHandler { Promise>> promise_; @@ -3645,7 +3674,7 @@ StickerSetId StickersManager::on_get_sticker_set(tl_object_ptrsticker_type_ = sticker_type; s->has_text_color_ = has_text_color; s->channel_emoji_status_ = channel_emoji_status; - s->is_created_ = is_official; + s->is_created_ = is_created; s->is_changed_ = true; } else { CHECK(s->id_ == set_id); @@ -8561,6 +8590,52 @@ void StickersManager::set_sticker_mask_position(const td_api::object_ptr> &&promise) { + if (limit <= 0) { + return promise.set_error(Status::Error(400, "Parameter limit must be positive")); + } + auto query_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), promise = std::move(promise)]( + Result> r_my_stickers) mutable { + send_closure(actor_id, &StickersManager::on_get_created_sticker_sets, std::move(r_my_stickers), + std::move(promise)); + }); + td_->create_handler(std::move(query_promise))->send(offset_sticker_set_id, limit); +} + +void StickersManager::on_get_created_sticker_sets( + Result> r_my_stickers, + Promise> &&promise) { + G()->ignore_result_if_closing(r_my_stickers); + if (r_my_stickers.is_error()) { + return promise.set_error(r_my_stickers.move_as_error()); + } + auto my_stickers = r_my_stickers.move_as_ok(); + auto total_count = my_stickers->count_; + vector sticker_set_ids; + for (auto &sticker_set_covered : my_stickers->sets_) { + auto sticker_set_id = + on_get_sticker_set_covered(std::move(sticker_set_covered), false, "on_get_created_sticker_sets"); + if (sticker_set_id.is_valid()) { + auto sticker_set = get_sticker_set(sticker_set_id); + CHECK(sticker_set != nullptr); + update_sticker_set(sticker_set, "on_get_created_sticker_sets"); + + if (!td::contains(sticker_set_ids, sticker_set_id) && sticker_set->is_created_) { + sticker_set_ids.push_back(sticker_set_id); + } + } + } + if (static_cast(sticker_set_ids.size()) > total_count) { + LOG(ERROR) << "Expected total of " << total_count << " owned sticker sets, but " << sticker_set_ids.size() + << " received"; + total_count = static_cast(sticker_set_ids.size()); + } + send_update_installed_sticker_sets(); + promise.set_value(get_sticker_sets_object(total_count, sticker_set_ids, 1)); +} + vector StickersManager::get_attached_sticker_file_ids(const vector &int_file_ids) { vector result; diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index afebb3b1c..8e788e3ba 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -320,6 +320,9 @@ class StickersManager final : public Actor { void set_sticker_mask_position(const td_api::object_ptr &sticker, td_api::object_ptr &&mask_position, Promise &&promise); + void get_created_sticker_sets(StickerSetId offset_sticker_set_id, int32 limit, + Promise> &&promise); + vector get_recent_stickers(bool is_attached, Promise &&promise); void on_get_recent_stickers(bool is_repair, bool is_attached, @@ -835,6 +838,9 @@ class StickersManager final : public Actor { }; Result get_sticker_input_document(const tl_object_ptr &sticker) const; + void on_get_created_sticker_sets(Result> r_my_stickers, + Promise> &&promise); + bool update_sticker_set_cache(const StickerSet *sticker_set, Promise &promise); const StickerSet *get_premium_gift_sticker_set(); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0df84a064..9296b661e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8292,6 +8292,13 @@ void Td::on_request(uint64 id, td_api::setStickerMaskPosition &request) { stickers_manager_->set_sticker_mask_position(request.sticker_, std::move(request.mask_position_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getOwnedStickerSets &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + stickers_manager_->get_created_sticker_sets(StickerSetId(request.offset_sticker_set_id_), request.limit_, + std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getRecentStickers &request) { CHECK_IS_USER(); CREATE_REQUEST(GetRecentStickersRequest, request.is_attached_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 257597db1..6d0c7be9b 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1526,6 +1526,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::setStickerMaskPosition &request); + void on_request(uint64 id, const td_api::getOwnedStickerSets &request); + void on_request(uint64 id, const td_api::getRecentStickers &request); void on_request(uint64 id, td_api::addRecentSticker &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 95b8c65e5..c1f55e095 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3522,6 +3522,11 @@ class CliClient final : public Actor { }); send_request(td_api::make_object(my_id_, title, name, as_sticker_type(op), false, std::move(input_stickers), "tg_cli")); + } else if (op == "goss") { + int64 sticker_set_id; + string limit; + get_args(args, sticker_set_id, limit); + send_request(td_api::make_object(sticker_set_id, as_limit(limit))); } else if (op == "sss") { send_request(td_api::make_object(args)); } else if (op == "siss") {