diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 48351d7e4..e43a1bd4b 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -486,7 +486,8 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler { if (is_fallback) { flags |= telegram_api::photos_uploadProfilePhoto::FALLBACK_MASK; } - auto video_emoji_markup = get_input_video_size_object(td_, sticker_photo_size); + auto video_emoji_markup = + sticker_photo_size != nullptr ? sticker_photo_size->get_input_video_size_object(td_) : nullptr; if (video_emoji_markup != nullptr) { flags |= telegram_api::photos_uploadProfilePhoto::VIDEO_EMOJI_MARKUP_MASK; } @@ -7116,7 +7117,7 @@ void ContactsManager::set_profile_photo_impl(UserId user_id, return promise.set_error(Status::Error(400, "Wrong main frame timestamp specified")); } - TRY_RESULT_PROMISE(promise, sticker_photo_size, get_sticker_photo_size(td_, sticker)); + TRY_RESULT_PROMISE(promise, sticker_photo_size, StickerPhotoSize::get_sticker_photo_size(td_, sticker)); auto file_type = is_animation ? FileType::Animation : FileType::Photo; auto r_file_id = td_->file_manager_->get_input_file_id(file_type, *input_file, DialogId(user_id), false, false); diff --git a/td/telegram/PhotoSize.cpp b/td/telegram/PhotoSize.cpp index 2add1bb6b..6c3e41595 100644 --- a/td/telegram/PhotoSize.cpp +++ b/td/telegram/PhotoSize.cpp @@ -308,7 +308,7 @@ Variant> process_video_size( } case telegram_api::videoSizeEmojiMarkup::ID: case telegram_api::videoSizeStickerMarkup::ID: { - auto sticker_photo_size = get_sticker_photo_size(td, std::move(size_ptr)); + auto sticker_photo_size = StickerPhotoSize::get_sticker_photo_size(td, std::move(size_ptr)); if (sticker_photo_size == nullptr) { return {}; } diff --git a/td/telegram/StickerPhotoSize.cpp b/td/telegram/StickerPhotoSize.cpp index 642869356..9f4253922 100644 --- a/td/telegram/StickerPhotoSize.cpp +++ b/td/telegram/StickerPhotoSize.cpp @@ -11,7 +11,7 @@ namespace td { -Result> get_sticker_photo_size( +Result> StickerPhotoSize::get_sticker_photo_size( Td *td, const td_api::object_ptr &sticker) { if (sticker == nullptr) { return nullptr; @@ -26,7 +26,7 @@ Result> get_sticker_photo_size( switch (sticker->type_->get_id()) { case td_api::chatPhotoStickerTypeRegularOrMask::ID: { auto type = static_cast(sticker->type_.get()); - result->type_ = StickerPhotoSize::Type::Sticker; + result->type_ = Type::Sticker; result->sticker_set_id_ = StickerSetId(type->sticker_set_id_); result->sticker_id_ = type->sticker_id_; if (!td->stickers_manager_->have_sticker(result->sticker_set_id_, result->sticker_id_)) { @@ -36,7 +36,7 @@ Result> get_sticker_photo_size( } case td_api::chatPhotoStickerTypeCustomEmoji::ID: { auto type = static_cast(sticker->type_.get()); - result->type_ = StickerPhotoSize::Type::CustomEmoji; + result->type_ = Type::CustomEmoji; result->custom_emoji_id_ = CustomEmojiId(type->custom_emoji_id_); if (!td->stickers_manager_->have_custom_emoji(result->custom_emoji_id_)) { return Status::Error(400, "Custom emoji not found"); @@ -75,34 +75,30 @@ Result> get_sticker_photo_size( return std::move(result); } -telegram_api::object_ptr get_input_video_size_object( - Td *td, const unique_ptr &sticker_photo_size) { - if (sticker_photo_size == nullptr) { - return nullptr; - } - switch (sticker_photo_size->type_) { - case StickerPhotoSize::Type::Sticker: +telegram_api::object_ptr StickerPhotoSize::get_input_video_size_object(Td *td) const { + switch (type_) { + case Type::Sticker: return telegram_api::make_object( - td->stickers_manager_->get_input_sticker_set(sticker_photo_size->sticker_set_id_), - sticker_photo_size->sticker_id_, vector(sticker_photo_size->background_colors_)); - case StickerPhotoSize::Type::CustomEmoji: - return telegram_api::make_object( - sticker_photo_size->custom_emoji_id_.get(), vector(sticker_photo_size->background_colors_)); + td->stickers_manager_->get_input_sticker_set(sticker_set_id_), sticker_id_, + vector(background_colors_)); + case Type::CustomEmoji: + return telegram_api::make_object(custom_emoji_id_.get(), + vector(background_colors_)); default: UNREACHABLE(); return nullptr; } } -unique_ptr get_sticker_photo_size(Td *td, - telegram_api::object_ptr &&size_ptr) { +unique_ptr StickerPhotoSize::get_sticker_photo_size( + Td *td, telegram_api::object_ptr &&size_ptr) { CHECK(size_ptr != nullptr); auto result = make_unique(); bool is_valid = false; switch (size_ptr->get_id()) { case telegram_api::videoSizeEmojiMarkup::ID: { auto size = move_tl_object_as(size_ptr); - result->type_ = StickerPhotoSize::Type::CustomEmoji; + result->type_ = Type::CustomEmoji; result->custom_emoji_id_ = CustomEmojiId(size->emoji_id_); result->background_colors_ = std::move(size->background_colors_); is_valid = result->custom_emoji_id_.is_valid(); @@ -110,7 +106,7 @@ unique_ptr get_sticker_photo_size(Td *td, } case telegram_api::videoSizeStickerMarkup::ID: { auto size = move_tl_object_as(size_ptr); - result->type_ = StickerPhotoSize::Type::Sticker; + result->type_ = 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_); @@ -133,10 +129,10 @@ unique_ptr get_sticker_photo_size(Td *td, td_api::object_ptr StickerPhotoSize::get_chat_photo_sticker_object() const { td_api::object_ptr sticker_type; switch (type_) { - case StickerPhotoSize::Type::Sticker: + case Type::Sticker: sticker_type = td_api::make_object(sticker_set_id_.get(), sticker_id_); break; - case StickerPhotoSize::Type::CustomEmoji: + case Type::CustomEmoji: sticker_type = td_api::make_object(custom_emoji_id_.get()); break; default: diff --git a/td/telegram/StickerPhotoSize.h b/td/telegram/StickerPhotoSize.h index 888e06b21..bbce4e93c 100644 --- a/td/telegram/StickerPhotoSize.h +++ b/td/telegram/StickerPhotoSize.h @@ -19,7 +19,7 @@ namespace td { class Td; -struct StickerPhotoSize { +class StickerPhotoSize { enum class Type : int32 { Sticker, CustomEmoji }; Type type_ = Type::CustomEmoji; CustomEmojiId custom_emoji_id_; @@ -27,18 +27,28 @@ struct StickerPhotoSize { int64 sticker_id_ = 0; vector background_colors_; + friend bool operator==(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const StickerPhotoSize &sticker_photo_size); + + public: + static Result> get_sticker_photo_size( + Td *td, const td_api::object_ptr &chat_photo_sticker); + + static unique_ptr get_sticker_photo_size( + Td *td, telegram_api::object_ptr &&size_ptr); + + telegram_api::object_ptr get_input_video_size_object(Td *td) const; + td_api::object_ptr get_chat_photo_sticker_object() const; + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); }; -Result> get_sticker_photo_size( - Td *td, const td_api::object_ptr &chat_photo_sticker); - -telegram_api::object_ptr get_input_video_size_object( - Td *td, const unique_ptr &sticker_photo_size); - -unique_ptr get_sticker_photo_size(Td *td, - telegram_api::object_ptr &&size_ptr); - bool operator==(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs); bool operator!=(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs); diff --git a/td/telegram/StickerPhotoSize.hpp b/td/telegram/StickerPhotoSize.hpp index 9a1b1b22a..011e253f0 100644 --- a/td/telegram/StickerPhotoSize.hpp +++ b/td/telegram/StickerPhotoSize.hpp @@ -14,24 +14,24 @@ namespace td { template -void store(const StickerPhotoSize &sticker_photo_size, StorerT &storer) { - bool is_custom_emoji = sticker_photo_size.type_ == StickerPhotoSize::Type::CustomEmoji; - bool is_sticker = sticker_photo_size.type_ == StickerPhotoSize::Type::Sticker; +void StickerPhotoSize::store(StorerT &storer) const { + bool is_custom_emoji = type_ == Type::CustomEmoji; + bool is_sticker = type_ == Type::Sticker; BEGIN_STORE_FLAGS(); STORE_FLAG(is_custom_emoji); STORE_FLAG(is_sticker); END_STORE_FLAGS(); if (is_custom_emoji) { - store(sticker_photo_size.custom_emoji_id_, storer); + td::store(custom_emoji_id_, storer); } else if (is_sticker) { - store(sticker_photo_size.sticker_set_id_, storer); - store(sticker_photo_size.sticker_id_, storer); + td::store(sticker_set_id_, storer); + td::store(sticker_id_, storer); } - store(sticker_photo_size.background_colors_, storer); + td::store(background_colors_, storer); } template -void parse(StickerPhotoSize &sticker_photo_size, ParserT &parser) { +void StickerPhotoSize::parse(ParserT &parser) { bool is_custom_emoji; bool is_sticker; BEGIN_PARSE_FLAGS(); @@ -39,16 +39,16 @@ void parse(StickerPhotoSize &sticker_photo_size, ParserT &parser) { PARSE_FLAG(is_sticker); END_PARSE_FLAGS(); if (is_custom_emoji) { - sticker_photo_size.type_ = StickerPhotoSize::Type::CustomEmoji; - parse(sticker_photo_size.custom_emoji_id_, parser); + type_ = Type::CustomEmoji; + td::parse(custom_emoji_id_, parser); } else if (is_sticker) { - sticker_photo_size.type_ = StickerPhotoSize::Type::Sticker; - parse(sticker_photo_size.sticker_set_id_, parser); - parse(sticker_photo_size.sticker_id_, parser); + type_ = Type::Sticker; + td::parse(sticker_set_id_, parser); + td::parse(sticker_id_, parser); } else { UNREACHABLE(); } - parse(sticker_photo_size.background_colors_, parser); + td::parse(background_colors_, parser); } } // namespace td