From 7be16c8a68279f8110dab46a59577388562b96c8 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 14 Feb 2023 14:59:03 +0300 Subject: [PATCH] Move sticker_format out of inputSticker. --- td/generate/scheme/td_api.tl | 7 ++--- td/telegram/StickerFormat.cpp | 4 ++- td/telegram/StickersManager.cpp | 48 +++++++++++++++------------------ td/telegram/StickersManager.h | 16 ++++++----- td/telegram/Td.cpp | 8 +++--- td/telegram/cli.cpp | 8 +++--- 6 files changed, 46 insertions(+), 45 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 9db8dab80..9dd58b9d2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5026,10 +5026,9 @@ proxies proxies:vector = Proxies; //@sticker File with the sticker; must fit in a 512x512 square. For WEBP stickers the file must be in WEBP or PNG format, which will be converted to WEBP server-side. //-See https://core.telegram.org/animated_stickers#technical-requirements for technical requirements //@emojis String with 1-20 emoji corresponding to the sticker -//@format Sticker format //@mask_position Position where the mask is placed; pass null if not specified //@keywords List of up to 20 keywords with total length up to 64 characters, which can be used to find the sticker -inputSticker sticker:InputFile emojis:string format:StickerFormat mask_position:maskPosition keywords:vector = InputSticker; +inputSticker sticker:InputFile emojis:string mask_position:maskPosition keywords:vector = InputSticker; //@description Represents a date range @start_date Point in time (Unix timestamp) at which the date range begins @end_date Point in time (Unix timestamp) at which the date range ends @@ -7986,11 +7985,13 @@ checkStickerSetName name:string = CheckStickerSetNameResult; //@user_id Sticker set owner; ignored for regular users //@title Sticker set title; 1-64 characters //@name Sticker set name. Can contain only English letters, digits and underscores. Must end with *"_by_"* (** is case insensitive) for bots; 1-64 characters +//@sticker_format Format of the stickers in the set //@sticker_type Type of the stickers in the set //@needs_repainting True, if stickers in the sticker set must be repainted; for custom emoji sticker sets only //@stickers List of stickers to be added to the set; must be non-empty. All stickers must have the same format. For TGS stickers, uploadStickerFile must be used before the sticker is shown //@source Source of the sticker set; may be empty if unknown -createNewStickerSet user_id:int53 title:string name:string sticker_type:StickerType needs_repainting:Bool stickers:vector source:string = StickerSet; +createNewStickerSet user_id:int53 title:string name:string sticker_format:StickerFormat sticker_type:StickerType needs_repainting:Bool stickers:vector source:string = StickerSet; + //@description Adds a new sticker to a set; for bots only. Returns the sticker set //@user_id Sticker set owner diff --git a/td/telegram/StickerFormat.cpp b/td/telegram/StickerFormat.cpp index 97359e501..7fd287119 100644 --- a/td/telegram/StickerFormat.cpp +++ b/td/telegram/StickerFormat.cpp @@ -11,7 +11,9 @@ namespace td { StickerFormat get_sticker_format(const td_api::object_ptr &format) { - CHECK(format != nullptr); + if (format == nullptr) { + return StickerFormat::Unknown; + } switch (format->get_id()) { case td_api::stickerFormatWebp::ID: return StickerFormat::Webp; diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index f72d49e77..f9ea72238 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -7978,8 +7978,9 @@ void StickersManager::move_sticker_set_to_top_by_custom_emoji_ids(const vector> StickersManager::prepare_input_sticker( - td_api::inputSticker *sticker, StickerType sticker_type) { +Result> StickersManager::prepare_input_sticker(td_api::inputSticker *sticker, + StickerFormat sticker_format, + StickerType sticker_type) { if (sticker == nullptr) { return Status::Error(400, "Input sticker must be non-empty"); } @@ -7999,26 +8000,23 @@ Result> StickersManager::prepare_i } } - if (sticker->format_ == nullptr) { - return Status::Error(400, "Sticker format must be non-empty"); - } - - return prepare_input_file(sticker->sticker_, get_sticker_format(sticker->format_), sticker_type, false); + return prepare_input_file(sticker->sticker_, sticker_format, sticker_type, false); } -Result> StickersManager::prepare_input_file( - const tl_object_ptr &input_file, StickerFormat format, StickerType type, bool for_thumbnail) { - auto file_type = format == StickerFormat::Tgs ? FileType::Sticker : FileType::Document; +Result> StickersManager::prepare_input_file( + const tl_object_ptr &input_file, StickerFormat sticker_format, StickerType sticker_type, + bool for_thumbnail) { + auto file_type = sticker_format == StickerFormat::Tgs ? FileType::Sticker : FileType::Document; TRY_RESULT(file_id, td_->file_manager_->get_input_file_id(file_type, input_file, DialogId(), for_thumbnail, false)); if (file_id.empty()) { - return std::make_tuple(FileId(), false, false, StickerFormat::Unknown); + return std::make_tuple(FileId(), false, false); } - if (format == StickerFormat::Tgs) { + if (sticker_format == StickerFormat::Tgs) { int32 width = for_thumbnail ? 100 : 512; create_sticker(file_id, FileId(), string(), PhotoSize(), get_dimensions(width, width, "prepare_input_file"), - nullptr, nullptr, format, nullptr); - } else if (format == StickerFormat::Webm) { + nullptr, nullptr, sticker_format, nullptr); + } else if (sticker_format == StickerFormat::Webm) { td_->documents_manager_->create_document(file_id, string(), PhotoSize(), "sticker.webm", "video/webm", false); } else { td_->documents_manager_->create_document(file_id, string(), PhotoSize(), "sticker.png", "image/png", false); @@ -8041,13 +8039,13 @@ Result> StickersManager::prepare_i is_url = true; } else { if (file_view.has_local_location() && - file_view.expected_size() > get_max_sticker_file_size(format, type, for_thumbnail)) { + file_view.expected_size() > get_max_sticker_file_size(sticker_format, sticker_type, for_thumbnail)) { return Status::Error(400, "File is too big"); } is_local = true; } } - return std::make_tuple(file_id, is_url, is_local, format); + return std::make_tuple(file_id, is_url, is_local); } FileId StickersManager::upload_sticker_file(UserId user_id, td_api::object_ptr &&sticker_format, @@ -8160,7 +8158,8 @@ td_api::object_ptr StickersManager::get_check } } -void StickersManager::create_new_sticker_set(UserId user_id, string title, string short_name, StickerType sticker_type, +void StickersManager::create_new_sticker_set(UserId user_id, string title, string short_name, + StickerFormat sticker_format, StickerType sticker_type, bool has_text_color, vector> &&stickers, string software, @@ -8190,25 +8189,25 @@ void StickersManager::create_new_sticker_set(UserId user_id, string title, strin return promise.set_error(Status::Error(400, "Only custom emoji stickers support repainting")); } + if (sticker_format == StickerFormat::Unknown) { + return promise.set_error(Status::Error(400, "Sticker format must be non-empty")); + } + vector file_ids; file_ids.reserve(stickers.size()); vector local_file_ids; vector url_file_ids; - FlatHashSet sticker_formats; - StickerFormat sticker_format = StickerFormat::Unknown; for (auto &sticker : stickers) { - auto r_file_id = prepare_input_sticker(sticker.get(), sticker_type); + auto r_file_id = prepare_input_sticker(sticker.get(), sticker_format, sticker_type); if (r_file_id.is_error()) { return promise.set_error(r_file_id.move_as_error()); } auto file_id = std::get<0>(r_file_id.ok()); auto is_url = std::get<1>(r_file_id.ok()); auto is_local = std::get<2>(r_file_id.ok()); - sticker_format = std::get<3>(r_file_id.ok()); if (is_sticker_format_animated(sticker_format) && is_url) { return promise.set_error(Status::Error(400, "Animated stickers can't be uploaded by URL")); } - sticker_formats.insert(static_cast(get_sticker_format(sticker->format_)) + 1); file_ids.push_back(file_id); if (is_url) { @@ -8217,9 +8216,6 @@ void StickersManager::create_new_sticker_set(UserId user_id, string title, strin local_file_ids.push_back(file_id); } } - if (sticker_formats.size() != 1) { - return promise.set_error(Status::Error(400, "All stickers must be of the same format")); - } auto pending_new_sticker_set = make_unique(); pending_new_sticker_set->user_id_ = user_id; @@ -8456,7 +8452,7 @@ void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name, return promise.set_error(Status::Error(400, "Sticker set not found")); } - auto r_file_id = prepare_input_sticker(sticker.get(), sticker_set->sticker_type_); + auto r_file_id = prepare_input_sticker(sticker.get(), sticker_set->sticker_format_, sticker_set->sticker_type_); if (r_file_id.is_error()) { return promise.set_error(r_file_id.move_as_error()); } diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 9769d92a8..afc863068 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -298,9 +298,10 @@ class StickersManager final : public Actor { static td_api::object_ptr get_check_sticker_set_name_result_object( CheckStickerSetNameResult result); - void create_new_sticker_set(UserId user_id, string title, string short_name, StickerType sticker_type, - bool has_text_color, vector> &&stickers, - string software, Promise> &&promise); + void create_new_sticker_set(UserId user_id, string title, string short_name, StickerFormat sticker_format, + StickerType sticker_type, bool has_text_color, + vector> &&stickers, string software, + Promise> &&promise); void add_sticker_to_set(UserId user_id, string short_name, tl_object_ptr &&sticker, Promise> &&promise); @@ -846,11 +847,12 @@ class StickersManager final : public Actor { std::pair, vector> split_stickers_by_premium(const StickerSet *sticker_set) const; - Result> prepare_input_file( - const tl_object_ptr &input_file, StickerFormat format, StickerType type, bool for_thumbnail); + Result> prepare_input_file(const tl_object_ptr &input_file, + StickerFormat sticker_format, StickerType sticker_type, + bool for_thumbnail); - Result> prepare_input_sticker(td_api::inputSticker *sticker, - StickerType sticker_type); + Result> prepare_input_sticker(td_api::inputSticker *sticker, + StickerFormat sticker_format, StickerType sticker_type); tl_object_ptr get_input_sticker(const td_api::inputSticker *sticker, FileId file_id) const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0fad504fb..92ff0714a 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7288,10 +7288,10 @@ void Td::on_request(uint64 id, td_api::createNewStickerSet &request) { CLEAN_INPUT_STRING(request.name_); CLEAN_INPUT_STRING(request.source_); CREATE_REQUEST_PROMISE(); - stickers_manager_->create_new_sticker_set(UserId(request.user_id_), std::move(request.title_), - std::move(request.name_), get_sticker_type(request.sticker_type_), - request.needs_repainting_, std::move(request.stickers_), - std::move(request.source_), std::move(promise)); + stickers_manager_->create_new_sticker_set( + UserId(request.user_id_), std::move(request.title_), std::move(request.name_), + get_sticker_format(request.sticker_format_), get_sticker_type(request.sticker_type_), request.needs_repainting_, + std::move(request.stickers_), std::move(request.source_), std::move(promise)); } void Td::on_request(uint64 id, td_api::addStickerToSet &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 65c1346d1..a741781fa 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2896,11 +2896,11 @@ class CliClient final : public Actor { get_args(args, title, name, stickers); auto input_stickers = transform(autosplit(stickers), [op](Slice sticker) -> td_api::object_ptr { - return td_api::make_object(as_input_file(sticker), "😀", as_sticker_format(op), - as_mask_position(op), vector{"keyword"}); + return td_api::make_object(as_input_file(sticker), "😀", as_mask_position(op), + vector{"keyword"}); }); - send_request(td_api::make_object(my_id_, title, name, as_sticker_type(op), false, - std::move(input_stickers), "tg_cli")); + send_request(td_api::make_object( + my_id_, title, name, as_sticker_format(op), as_sticker_type(op), false, std::move(input_stickers), "tg_cli")); } else if (op == "sss") { send_request(td_api::make_object(args)); } else if (op == "siss") {