diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index 33ae2c195..8e6c880d0 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -267,21 +267,18 @@ static tl_object_ptr get_animated_chat_photo_object(F } static tl_object_ptr get_chat_photo_sticker_object( - const StickerPhotoSize &sticker_photo_size) { + const unique_value_ptr &sticker_photo_size) { + if (sticker_photo_size == nullptr) { + return nullptr; + } td_api::object_ptr type; - switch (sticker_photo_size.type) { + switch (sticker_photo_size->type) { case StickerPhotoSize::Type::Sticker: - if (!sticker_photo_size.sticker_set_id.is_valid()) { - return nullptr; - } - type = td_api::make_object(sticker_photo_size.sticker_set_id.get(), - sticker_photo_size.sticker_id); + type = td_api::make_object(sticker_photo_size->sticker_set_id.get(), + sticker_photo_size->sticker_id); break; case StickerPhotoSize::Type::CustomEmoji: - if (!sticker_photo_size.custom_emoji_id.is_valid()) { - return nullptr; - } - type = td_api::make_object(sticker_photo_size.custom_emoji_id.get()); + type = td_api::make_object(sticker_photo_size->custom_emoji_id.get()); break; default: UNREACHABLE(); @@ -302,7 +299,7 @@ static tl_object_ptr get_chat_photo_sticker_object( UNREACHABLE(); return nullptr; } - }(sticker_photo_size.background_colors); + }(sticker_photo_size->background_colors); return td_api::make_object(std::move(type), std::move(background_fill)); } @@ -382,12 +379,14 @@ Photo get_photo(Td *td, tl_object_ptr &&photo, DialogId own continue; } animation.visit(overloaded( - [&](const AnimationSize &animation_size) { + [&](AnimationSize &&animation_size) { if (animation_size.type != 0 && animation_size.dimensions.width == animation_size.dimensions.height) { res.animations.push_back(std::move(animation_size)); } }, - [&](const StickerPhotoSize &sticker_photo_size) { res.sticker_photo_size = std::move(sticker_photo_size); })); + [&](unique_ptr &&sticker_photo_size) { + res.sticker_photo_size = std::move(sticker_photo_size); + })); } return res; @@ -636,8 +635,8 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Photo &photo) { if (!photo.animations.empty()) { string_builder << ", animations = " << format::as_array(photo.animations); } - if (photo.sticker_photo_size.custom_emoji_id.is_valid()) { - string_builder << ", custom emoji size = " << photo.sticker_photo_size; + if (photo.sticker_photo_size != nullptr) { + string_builder << ", sticker = " << *photo.sticker_photo_size; } return string_builder << ']'; } diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index 15cc812fe..ca0f72018 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -21,6 +21,7 @@ #include "td/utils/common.h" #include "td/utils/MovableValue.h" #include "td/utils/StringBuilder.h" +#include "td/utils/unique_value_ptr.h" namespace td { @@ -47,7 +48,7 @@ struct Photo { vector animations; - StickerPhotoSize sticker_photo_size; + unique_value_ptr sticker_photo_size; bool has_stickers = false; vector sticker_file_ids; diff --git a/td/telegram/Photo.hpp b/td/telegram/Photo.hpp index 4295e7227..f31b34785 100644 --- a/td/telegram/Photo.hpp +++ b/td/telegram/Photo.hpp @@ -72,7 +72,7 @@ template void store(const Photo &photo, StorerT &storer) { bool has_minithumbnail = !photo.minithumbnail.empty(); bool has_animations = !photo.animations.empty(); - bool has_sticker_photo_size = photo.sticker_photo_size.custom_emoji_id.is_valid(); + bool has_sticker_photo_size = photo.sticker_photo_size != nullptr; BEGIN_STORE_FLAGS(); STORE_FLAG(photo.has_stickers); STORE_FLAG(has_minithumbnail); diff --git a/td/telegram/PhotoSize.cpp b/td/telegram/PhotoSize.cpp index 8ecbeb76a..34260ef33 100644 --- a/td/telegram/PhotoSize.cpp +++ b/td/telegram/PhotoSize.cpp @@ -261,10 +261,9 @@ Variant get_photo_size(FileManager *file_manager, PhotoSizeSo return std::move(res); } -Variant get_animation_size(Td *td, PhotoSizeSource source, int64 id, int64 access_hash, - std::string file_reference, DcId dc_id, - DialogId owner_dialog_id, - tl_object_ptr &&size_ptr) { +Variant> get_animation_size( + Td *td, PhotoSizeSource source, int64 id, int64 access_hash, std::string file_reference, DcId dc_id, + DialogId owner_dialog_id, tl_object_ptr &&size_ptr) { CHECK(size_ptr != nullptr); switch (size_ptr->get_id()) { case telegram_api::videoSize::ID: { @@ -298,33 +297,33 @@ Variant get_animation_size(Td *td, PhotoSizeSou } case telegram_api::videoSizeEmojiMarkup::ID: { auto size = move_tl_object_as(size_ptr); - StickerPhotoSize result; - result.type = StickerPhotoSize::Type::CustomEmoji; - result.custom_emoji_id = CustomEmojiId(size->emoji_id_); - result.background_colors = std::move(size->background_colors_); - if (!result.custom_emoji_id.is_valid() || result.background_colors.empty() || - result.background_colors.size() > 4) { - LOG(ERROR) << "Receive invalid " << result; + auto result = make_unique(); + result->type = StickerPhotoSize::Type::CustomEmoji; + result->custom_emoji_id = CustomEmojiId(size->emoji_id_); + result->background_colors = std::move(size->background_colors_); + if (!result->custom_emoji_id.is_valid() || result->background_colors.empty() || + result->background_colors.size() > 4) { + LOG(ERROR) << "Receive invalid " << *result; return {}; } - for (auto &color : result.background_colors) { + for (auto &color : result->background_colors) { color &= 0xFFFFFF; } return std::move(result); } case telegram_api::videoSizeStickerMarkup::ID: { auto size = move_tl_object_as(size_ptr); - StickerPhotoSize result; - result.type = StickerPhotoSize::Type::Sticker; - result.sticker_set_id = td->stickers_manager_->add_sticker_set(std::move(size->stickerset_)); - result.sticker_id = size->sticker_id_; - result.background_colors = std::move(size->background_colors_); - if (!result.sticker_set_id.is_valid() || result.sticker_id == 0 || !result.custom_emoji_id.is_valid() || - result.background_colors.empty() || result.background_colors.size() > 4) { - LOG(ERROR) << "Receive invalid " << result; + auto result = make_unique(); + result->type = StickerPhotoSize::Type::Sticker; + result->sticker_set_id = td->stickers_manager_->add_sticker_set(std::move(size->stickerset_)); + result->sticker_id = size->sticker_id_; + result->background_colors = std::move(size->background_colors_); + if (!result->sticker_set_id.is_valid() || result->sticker_id == 0 || !result->custom_emoji_id.is_valid() || + result->background_colors.empty() || result->background_colors.size() > 4) { + LOG(ERROR) << "Receive invalid " << *result; return {}; } - for (auto &color : result.background_colors) { + for (auto &color : result->background_colors) { color &= 0xFFFFFF; } return std::move(result); diff --git a/td/telegram/PhotoSize.h b/td/telegram/PhotoSize.h index 68c23b04a..9bf324f96 100644 --- a/td/telegram/PhotoSize.h +++ b/td/telegram/PhotoSize.h @@ -55,9 +55,9 @@ Variant get_photo_size(FileManager *file_manager, PhotoSizeSo DialogId owner_dialog_id, tl_object_ptr &&size_ptr, PhotoFormat format); -Variant get_animation_size(Td *td, PhotoSizeSource source, int64 id, int64 access_hash, - string file_reference, DcId dc_id, DialogId owner_dialog_id, - tl_object_ptr &&size_ptr); +Variant> get_animation_size( + Td *td, PhotoSizeSource source, int64 id, int64 access_hash, string file_reference, DcId dc_id, + DialogId owner_dialog_id, tl_object_ptr &&size_ptr); PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id, tl_object_ptr web_document_ptr); diff --git a/tdutils/td/utils/tl_helpers.h b/tdutils/td/utils/tl_helpers.h index 2de4d5d8e..4d2123887 100644 --- a/tdutils/td/utils/tl_helpers.h +++ b/tdutils/td/utils/tl_helpers.h @@ -16,6 +16,7 @@ #include "td/utils/Status.h" #include "td/utils/tl_parsers.h" #include "td/utils/tl_storers.h" +#include "td/utils/unique_value_ptr.h" #include "td/utils/Variant.h" #include @@ -172,6 +173,18 @@ void parse(unique_ptr &ptr, ParserT &parser) { parse(*ptr, parser); } +template +void store(const unique_value_ptr &ptr, StorerT &storer) { + CHECK(ptr != nullptr); + store(*ptr, storer); +} +template +void parse(unique_value_ptr &ptr, ParserT &parser) { + CHECK(ptr == nullptr); + ptr = make_unique_value(); + parse(*ptr, parser); +} + template void store(const FlatHashSet &s, StorerT &storer) { storer.store_binary(narrow_cast(s.size()));