From 96d29d141a674eca93d2bba4bfc45e9aac79b767 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 24 Jun 2024 16:31:07 +0300 Subject: [PATCH] Move caption out of MessageExtendedMedia. --- td/telegram/InputInvoice.cpp | 38 ++++++++++++++++++---------- td/telegram/InputInvoice.h | 2 ++ td/telegram/InputInvoice.hpp | 11 ++++++++ td/telegram/MessageContent.cpp | 2 +- td/telegram/MessageExtendedMedia.cpp | 20 +++------------ td/telegram/MessageExtendedMedia.h | 13 ++-------- td/telegram/MessageExtendedMedia.hpp | 15 +++++------ 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/td/telegram/InputInvoice.cpp b/td/telegram/InputInvoice.cpp index 62555b8c5..bfc98fab4 100644 --- a/td/telegram/InputInvoice.cpp +++ b/td/telegram/InputInvoice.cpp @@ -44,7 +44,8 @@ bool operator==(const InputInvoice &lhs, const InputInvoice &rhs) { lhs.start_parameter_ == rhs.start_parameter_ && are_invoice_equal(lhs.invoice_, rhs.invoice_) && lhs.payload_ == rhs.payload_ && lhs.provider_token_ == rhs.provider_token_ && lhs.provider_data_ == rhs.provider_data_ && lhs.extended_media_ == rhs.extended_media_ && - lhs.total_amount_ == rhs.total_amount_ && lhs.receipt_message_id_ == rhs.receipt_message_id_; + lhs.extended_media_caption_ == rhs.extended_media_caption_ && lhs.total_amount_ == rhs.total_amount_ && + lhs.receipt_message_id_ == rhs.receipt_message_id_; } bool operator!=(const InputInvoice &lhs, const InputInvoice &rhs) { @@ -63,8 +64,10 @@ InputInvoice::InputInvoice(tl_object_ptr &&me // payload_ = string(); // provider_token_ = string(); // provider_data_ = string(); - extended_media_ = - MessageExtendedMedia(td, std::move(message_invoice->extended_media_), std::move(message), owner_dialog_id); + extended_media_ = MessageExtendedMedia(td, std::move(message_invoice->extended_media_), owner_dialog_id); + if (!extended_media_.is_empty()) { + extended_media_caption_ = std::move(message); + } if (message_invoice->total_amount_ <= 0 || !check_currency_amount(message_invoice->total_amount_)) { LOG(ERROR) << "Receive invalid total amount " << message_invoice->total_amount_; message_invoice->total_amount_ = 0; @@ -92,6 +95,7 @@ InputInvoice::InputInvoice(tl_object_ptrtotal_amount_ <= 0 || !check_currency_amount(message_invoice->total_amount_)) { LOG(ERROR) << "Receive invalid total amount " << message_invoice->total_amount_; message_invoice->total_amount_ = 0; @@ -230,25 +234,31 @@ Result InputInvoice::process_input_message_invoice( result.provider_token_ = std::move(input_invoice->provider_token_); result.provider_data_ = std::move(input_invoice->provider_data_); - bool is_bot = td->auth_manager_->is_bot(); - TRY_RESULT(extended_media_caption, - get_formatted_text(td, owner_dialog_id, std::move(input_invoice->paid_media_caption_), is_bot, true, false, - false)); - TRY_RESULT(extended_media, - MessageExtendedMedia::get_message_extended_media(td, std::move(input_invoice->paid_media_), - std::move(extended_media_caption), owner_dialog_id)); + TRY_RESULT(extended_media, MessageExtendedMedia::get_message_extended_media(td, std::move(input_invoice->paid_media_), + owner_dialog_id)); result.extended_media_ = std::move(extended_media); + if (!result.extended_media_.is_empty()) { + bool is_bot = td->auth_manager_->is_bot(); + TRY_RESULT(extended_media_caption, + get_formatted_text(td, owner_dialog_id, std::move(input_invoice->paid_media_caption_), is_bot, true, + false, false)); + result.extended_media_caption_ = std::move(extended_media_caption); + } return result; } td_api::object_ptr InputInvoice::get_message_invoice_object(Td *td, bool skip_bot_commands, int32 max_media_timestamp) const { + auto extended_media_object = extended_media_.get_message_extended_media_object(td); + auto extended_media_caption_object = + extended_media_object == nullptr + ? nullptr + : get_formatted_text_object(extended_media_caption_, skip_bot_commands, max_media_timestamp); return td_api::make_object( get_product_info_object(td, title_, description_, photo_), invoice_.currency_, total_amount_, start_parameter_, - invoice_.is_test_, invoice_.need_shipping_address_, receipt_message_id_.get(), - extended_media_.get_message_extended_media_object(td), - extended_media_.get_caption_object(skip_bot_commands, max_media_timestamp)); + invoice_.is_test_, invoice_.need_shipping_address_, receipt_message_id_.get(), std::move(extended_media_object), + std::move(extended_media_caption_object)); } tl_object_ptr InputInvoice::Invoice::get_input_invoice() const { @@ -394,7 +404,7 @@ bool InputInvoice::is_equal_but_different(const InputInvoice &other) const { } const FormattedText *InputInvoice::get_caption() const { - return extended_media_.get_caption(); + return &extended_media_caption_; } int32 InputInvoice::get_duration(const Td *td) const { diff --git a/td/telegram/InputInvoice.h b/td/telegram/InputInvoice.h index 975d829b3..809dd4de8 100644 --- a/td/telegram/InputInvoice.h +++ b/td/telegram/InputInvoice.h @@ -9,6 +9,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/files/FileId.h" #include "td/telegram/LabeledPricePart.h" +#include "td/telegram/MessageEntity.h" #include "td/telegram/MessageExtendedMedia.h" #include "td/telegram/MessageId.h" #include "td/telegram/Photo.h" @@ -63,6 +64,7 @@ class InputInvoice { string provider_token_; string provider_data_; MessageExtendedMedia extended_media_; + FormattedText extended_media_caption_; int64 total_amount_ = 0; MessageId receipt_message_id_; diff --git a/td/telegram/InputInvoice.hpp b/td/telegram/InputInvoice.hpp index 10050926e..4700d16e8 100644 --- a/td/telegram/InputInvoice.hpp +++ b/td/telegram/InputInvoice.hpp @@ -8,6 +8,7 @@ #include "td/telegram/InputInvoice.h" +#include "td/telegram/MessageEntity.hpp" #include "td/telegram/MessageExtendedMedia.hpp" #include "td/telegram/Photo.hpp" #include "td/telegram/Version.h" @@ -84,6 +85,7 @@ void InputInvoice::store(StorerT &storer) const { bool has_total_amount = total_amount_ != 0; bool has_receipt_message_id = receipt_message_id_.is_valid(); bool has_extended_media = !extended_media_.is_empty(); + bool has_extended_media_caption = !extended_media_caption_.text.empty(); BEGIN_STORE_FLAGS(); STORE_FLAG(has_description); STORE_FLAG(has_photo); @@ -94,6 +96,7 @@ void InputInvoice::store(StorerT &storer) const { STORE_FLAG(has_total_amount); STORE_FLAG(has_receipt_message_id); STORE_FLAG(has_extended_media); + STORE_FLAG(has_extended_media_caption); END_STORE_FLAGS(); store(title_, storer); if (has_description) { @@ -124,6 +127,9 @@ void InputInvoice::store(StorerT &storer) const { if (has_extended_media) { store(extended_media_, storer); } + if (has_extended_media_caption) { + store(extended_media_caption_, storer); + } } template @@ -138,6 +144,7 @@ void InputInvoice::parse(ParserT &parser) { bool has_total_amount; bool has_receipt_message_id; bool has_extended_media; + bool has_extended_media_caption = false; if (parser.version() >= static_cast(Version::AddInputInvoiceFlags)) { BEGIN_PARSE_FLAGS(); PARSE_FLAG(has_description); @@ -149,6 +156,7 @@ void InputInvoice::parse(ParserT &parser) { PARSE_FLAG(has_total_amount); PARSE_FLAG(has_receipt_message_id); PARSE_FLAG(has_extended_media); + PARSE_FLAG(has_extended_media_caption); END_PARSE_FLAGS(); } else { has_description = true; @@ -190,6 +198,9 @@ void InputInvoice::parse(ParserT &parser) { if (has_extended_media) { parse(extended_media_, parser); } + if (has_extended_media_caption) { + parse(extended_media_caption_, parser); + } } } // namespace td diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 509fc6ec5..57c9c56fc 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -6405,7 +6405,7 @@ unique_ptr get_message_content(Td *td, FormattedText message, case telegram_api::messageMediaPaidMedia::ID: { auto media = telegram_api::move_object_as(media_ptr); auto extended_media = transform(std::move(media->extended_media_), [&](auto &&extended_media) { - return MessageExtendedMedia(td, std::move(extended_media), FormattedText(), owner_dialog_id); + return MessageExtendedMedia(td, std::move(extended_media), owner_dialog_id); }); return td::make_unique(std::move(extended_media), std::move(message), StarManager::get_star_count(media->stars_amount_)); diff --git a/td/telegram/MessageExtendedMedia.cpp b/td/telegram/MessageExtendedMedia.cpp index 50633bc12..2fe4bad38 100644 --- a/td/telegram/MessageExtendedMedia.cpp +++ b/td/telegram/MessageExtendedMedia.cpp @@ -27,12 +27,10 @@ namespace td { MessageExtendedMedia::MessageExtendedMedia( - Td *td, telegram_api::object_ptr &&extended_media, FormattedText &&caption, - DialogId owner_dialog_id) { + Td *td, telegram_api::object_ptr &&extended_media, DialogId owner_dialog_id) { if (extended_media == nullptr) { return; } - caption_ = std::move(caption); switch (extended_media->get_id()) { case telegram_api::messageExtendedMediaPreview::ID: { @@ -114,8 +112,7 @@ void MessageExtendedMedia::init_from_media(Td *td, telegram_api::object_ptr MessageExtendedMedia::get_message_extended_media( - Td *td, td_api::object_ptr &&paid_media, FormattedText &&caption, - DialogId owner_dialog_id) { + Td *td, td_api::object_ptr &&paid_media, DialogId owner_dialog_id) { if (paid_media == nullptr) { return MessageExtendedMedia(); } @@ -127,7 +124,6 @@ Result MessageExtendedMedia::get_message_extended_media( } MessageExtendedMedia result; - result.caption_ = std::move(caption); auto file_type = FileType::None; switch (paid_media->type_->get_id()) { @@ -190,7 +186,7 @@ void MessageExtendedMedia::update_from(const MessageExtendedMedia &old_extended_ bool MessageExtendedMedia::update_to(Td *td, telegram_api::object_ptr extended_media_ptr, DialogId owner_dialog_id) { - MessageExtendedMedia new_extended_media(td, std::move(extended_media_ptr), FormattedText(caption_), owner_dialog_id); + MessageExtendedMedia new_extended_media(td, std::move(extended_media_ptr), owner_dialog_id); if (!new_extended_media.is_media() && is_media()) { return false; } @@ -226,14 +222,6 @@ td_api::object_ptr MessageExtendedMedia::get_messa } } -td_api::object_ptr MessageExtendedMedia::get_caption_object(bool skip_bot_commands, - int32 max_media_timestamp) const { - if (type_ == Type::Empty) { - return nullptr; - } - return get_formatted_text_object(caption_, skip_bot_commands, max_media_timestamp); -} - void MessageExtendedMedia::append_file_ids(const Td *td, vector &file_ids) const { switch (type_) { case Type::Empty: @@ -354,7 +342,7 @@ bool MessageExtendedMedia::is_equal_but_different(const MessageExtendedMedia &ot } bool operator==(const MessageExtendedMedia &lhs, const MessageExtendedMedia &rhs) { - if (lhs.type_ != rhs.type_ || lhs.caption_ != rhs.caption_) { + if (lhs.type_ != rhs.type_) { return false; } switch (lhs.type_) { diff --git a/td/telegram/MessageExtendedMedia.h b/td/telegram/MessageExtendedMedia.h index 574b14391..bb63cc764 100644 --- a/td/telegram/MessageExtendedMedia.h +++ b/td/telegram/MessageExtendedMedia.h @@ -9,7 +9,6 @@ #include "td/telegram/DialogId.h" #include "td/telegram/Dimensions.h" #include "td/telegram/files/FileId.h" -#include "td/telegram/MessageEntity.h" #include "td/telegram/Photo.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -24,7 +23,6 @@ class Td; class MessageExtendedMedia { enum class Type : int32 { Empty, Unsupported, Preview, Photo, Video }; Type type_ = Type::Empty; - FormattedText caption_; static constexpr int32 CURRENT_VERSION = 1; @@ -54,13 +52,12 @@ class MessageExtendedMedia { MessageExtendedMedia() = default; MessageExtendedMedia(Td *td, telegram_api::object_ptr &&extended_media, - FormattedText &&caption, DialogId owner_dialog_id); + DialogId owner_dialog_id); MessageExtendedMedia(Td *td, telegram_api::object_ptr &&media, DialogId owner_dialog_id); static Result get_message_extended_media( - Td *td, td_api::object_ptr &&paid_media, FormattedText &&caption, - DialogId owner_dialog_id); + Td *td, td_api::object_ptr &&paid_media, DialogId owner_dialog_id); bool is_empty() const { return type_ == Type::Empty; @@ -73,8 +70,6 @@ class MessageExtendedMedia { td_api::object_ptr get_message_extended_media_object(Td *td) const; - td_api::object_ptr get_caption_object(bool skip_bot_commands, int32 max_media_timestamp) const; - void append_file_ids(const Td *td, vector &file_ids) const; void delete_thumbnail(Td *td); @@ -95,10 +90,6 @@ class MessageExtendedMedia { int32 get_duration(const Td *td) const; - const FormattedText *get_caption() const { - return &caption_; - } - FileId get_upload_file_id() const; FileId get_any_file_id() const; diff --git a/td/telegram/MessageExtendedMedia.hpp b/td/telegram/MessageExtendedMedia.hpp index d206b30ed..eaa4a9e75 100644 --- a/td/telegram/MessageExtendedMedia.hpp +++ b/td/telegram/MessageExtendedMedia.hpp @@ -18,7 +18,6 @@ namespace td { template void MessageExtendedMedia::store(StorerT &storer) const { - bool has_caption = !caption_.text.empty(); bool has_unsupported_version = unsupported_version_ != 0; bool has_duration = duration_ != 0; bool has_dimensions = dimensions_.width != 0 || dimensions_.height != 0; @@ -26,7 +25,7 @@ void MessageExtendedMedia::store(StorerT &storer) const { bool has_photo = !photo_.is_empty(); bool has_video = video_file_id_.is_valid(); BEGIN_STORE_FLAGS(); - STORE_FLAG(has_caption); + STORE_FLAG(false); // has_caption STORE_FLAG(has_unsupported_version); STORE_FLAG(has_duration); STORE_FLAG(has_dimensions); @@ -35,9 +34,6 @@ void MessageExtendedMedia::store(StorerT &storer) const { STORE_FLAG(has_video); END_STORE_FLAGS(); td::store(type_, storer); - if (has_caption) { - td::store(caption_, storer); - } if (has_unsupported_version) { td::store(unsupported_version_, storer); } @@ -79,7 +75,8 @@ void MessageExtendedMedia::parse(ParserT &parser) { END_PARSE_FLAGS(); td::parse(type_, parser); if (has_caption) { - td::parse(caption_, parser); + FormattedText caption; + td::parse(caption, parser); } if (has_unsupported_version) { td::parse(unsupported_version_, parser); @@ -103,8 +100,10 @@ void MessageExtendedMedia::parse(ParserT &parser) { video_file_id_ = td->videos_manager_->parse_video(parser); is_bad = !video_file_id_.is_valid(); } - if (is_bad) { - LOG(ERROR) << "Failed to parse MessageExtendedMedia"; + if (is_bad || has_caption) { + if (is_bad) { + LOG(ERROR) << "Failed to parse MessageExtendedMedia"; + } photo_ = Photo(); video_file_id_ = FileId(); type_ = Type::Unsupported;