diff --git a/td/telegram/MessageForwardInfo.cpp b/td/telegram/MessageForwardInfo.cpp index 0a54c70fd..1f6edabc2 100644 --- a/td/telegram/MessageForwardInfo.cpp +++ b/td/telegram/MessageForwardInfo.cpp @@ -15,6 +15,52 @@ namespace td { +bool LastForwardedMessageInfo::is_empty() const { + return dialog_id_ == DialogId() && message_id_ == MessageId(); +} + +bool LastForwardedMessageInfo::validate() { + if (is_empty()) { + return true; + } + if (!dialog_id_.is_valid() || !message_id_.is_valid()) { + *this = {}; + return false; + } + return true; +} + +void LastForwardedMessageInfo::add_dependencies(Dependencies &dependencies) const { + dependencies.add_dialog_and_dependencies(dialog_id_); +} + +void LastForwardedMessageInfo::add_min_user_ids(vector &user_ids) const { + if (dialog_id_.get_type() == DialogType::User) { + user_ids.push_back(dialog_id_.get_user_id()); + } +} + +void LastForwardedMessageInfo::add_min_channel_ids(vector &channel_ids) const { + if (dialog_id_.get_type() == DialogType::Channel) { + channel_ids.push_back(dialog_id_.get_channel_id()); + } +} + +bool operator==(const LastForwardedMessageInfo &lhs, const LastForwardedMessageInfo &rhs) { + return lhs.dialog_id_ == rhs.dialog_id_ && lhs.message_id_ == rhs.message_id_; +} + +bool operator!=(const LastForwardedMessageInfo &lhs, const LastForwardedMessageInfo &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const LastForwardedMessageInfo &last_message_info) { + if (last_message_info.is_empty()) { + return string_builder; + } + return string_builder << MessageFullId(last_message_info.dialog_id_, last_message_info.message_id_); +} + unique_ptr MessageForwardInfo::get_message_forward_info( Td *td, telegram_api::object_ptr &&forward_header) { if (forward_header == nullptr) { @@ -26,18 +72,18 @@ unique_ptr MessageForwardInfo::get_message_forward_info( return nullptr; } - DialogId from_dialog_id; - MessageId from_message_id; + LastForwardedMessageInfo last_message_info; if (forward_header->saved_from_peer_ != nullptr) { - from_dialog_id = DialogId(forward_header->saved_from_peer_); - from_message_id = MessageId(ServerMessageId(forward_header->saved_from_msg_id_)); - if (!from_dialog_id.is_valid() || !from_message_id.is_valid()) { - LOG(ERROR) << "Receive " << from_message_id << " in " << from_dialog_id - << " in message forward header: " << oneline(to_string(forward_header)); - from_dialog_id = DialogId(); - from_message_id = MessageId(); + last_message_info = LastForwardedMessageInfo(DialogId(forward_header->saved_from_peer_), + MessageId(ServerMessageId(forward_header->saved_from_msg_id_))); + if (last_message_info.is_empty() || !last_message_info.validate()) { + LOG(ERROR) << "Receive wrong last message in message forward header: " << oneline(to_string(forward_header)); } else { - td->dialog_manager_->force_create_dialog(from_dialog_id, "get_message_forward_info", true); + Dependencies dependencies; + last_message_info.add_dependencies(dependencies); + for (auto dialog_id : dependencies.get_dialog_ids()) { + td->dialog_manager_->force_create_dialog(dialog_id, "get_message_forward_info", true); + } } } bool is_imported = forward_header->imported_; @@ -47,22 +93,16 @@ unique_ptr MessageForwardInfo::get_message_forward_info( return nullptr; } - return td::make_unique(r_origin.move_as_ok(), date, from_dialog_id, from_message_id, + return td::make_unique(r_origin.move_as_ok(), date, std::move(last_message_info), std::move(psa_type), is_imported); } -unique_ptr MessageForwardInfo::copy_message_forward_info(Td *td, - const MessageForwardInfo &forward_info, - DialogId from_dialog_id, - MessageId from_message_id) { - if (from_dialog_id.is_valid() != from_message_id.is_valid()) { - from_dialog_id = DialogId(); - from_message_id = MessageId(); - } +unique_ptr MessageForwardInfo::copy_message_forward_info( + Td *td, const MessageForwardInfo &forward_info, LastForwardedMessageInfo &&last_message_info) { + last_message_info.validate(); auto result = make_unique(forward_info); - result->from_dialog_id_ = from_dialog_id; - result->from_message_id_ = from_message_id; + result->last_message_info_ = std::move(last_message_info); result->origin_.hide_sender_if_needed(td); return result; } @@ -71,9 +111,11 @@ td_api::object_ptr MessageForwardInfo::get_message_f if (is_imported_) { return nullptr; } + auto last_message_full_id = get_last_message_full_id(); return td_api::make_object( origin_.get_message_origin_object(td), date_, psa_type_, - td->messages_manager_->get_chat_id_object(from_dialog_id_, "messageForwardInfo"), from_message_id_.get()); + td->messages_manager_->get_chat_id_object(last_message_full_id.get_dialog_id(), "messageForwardInfo"), + last_message_full_id.get_message_id().get()); } td_api::object_ptr MessageForwardInfo::get_message_import_info_object() const { @@ -85,19 +127,17 @@ td_api::object_ptr MessageForwardInfo::get_message_im void MessageForwardInfo::add_dependencies(Dependencies &dependencies) const { origin_.add_dependencies(dependencies); - dependencies.add_dialog_and_dependencies(from_dialog_id_); + last_message_info_.add_dependencies(dependencies); } void MessageForwardInfo::add_min_user_ids(vector &user_ids) const { origin_.add_user_ids(user_ids); - // from_dialog_id_ can be a user only in Saved Messages + last_message_info_.add_min_user_ids(user_ids); } void MessageForwardInfo::add_min_channel_ids(vector &channel_ids) const { origin_.add_channel_ids(channel_ids); - if (from_dialog_id_.get_type() == DialogType::Channel) { - channel_ids.push_back(from_dialog_id_.get_channel_id()); - } + last_message_info_.add_min_channel_ids(channel_ids); } bool MessageForwardInfo::need_change_warning(const MessageForwardInfo *lhs, const MessageForwardInfo *rhs, @@ -114,9 +154,8 @@ bool MessageForwardInfo::need_change_warning(const MessageForwardInfo *lhs, cons } bool operator==(const MessageForwardInfo &lhs, const MessageForwardInfo &rhs) { - return lhs.origin_ == rhs.origin_ && lhs.date_ == rhs.date_ && lhs.from_dialog_id_ == rhs.from_dialog_id_ && - lhs.from_message_id_ == rhs.from_message_id_ && lhs.psa_type_ == rhs.psa_type_ && - lhs.is_imported_ == rhs.is_imported_; + return lhs.origin_ == rhs.origin_ && lhs.date_ == rhs.date_ && lhs.last_message_info_ == rhs.last_message_info_ && + lhs.psa_type_ == rhs.psa_type_ && lhs.is_imported_ == rhs.is_imported_; } bool operator!=(const MessageForwardInfo &lhs, const MessageForwardInfo &rhs) { @@ -137,10 +176,10 @@ bool operator!=(const unique_ptr &lhs, const unique_ptr &user_ids) const; + + void add_min_channel_ids(vector &channel_ids) const; + + DialogId get_dialog_id() const { + return dialog_id_; + } + + MessageFullId get_message_full_id() const { + return {dialog_id_, message_id_}; + } + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); +}; + class MessageForwardInfo { MessageOrigin origin_; int32 date_ = 0; - DialogId from_dialog_id_; - MessageId from_message_id_; + LastForwardedMessageInfo last_message_info_; string psa_type_; bool is_imported_ = false; @@ -38,25 +76,21 @@ class MessageForwardInfo { public: MessageForwardInfo() = default; - MessageForwardInfo(MessageOrigin &&origin, int32 date, DialogId from_dialog_id, MessageId from_message_id, + MessageForwardInfo(MessageOrigin &&origin, int32 date, LastForwardedMessageInfo &&last_message_info, string &&psa_type, bool is_imported) : origin_(std::move(origin)) , date_(date) - , from_dialog_id_(from_dialog_id) - , from_message_id_(from_message_id) + , last_message_info_(std::move(last_message_info)) , psa_type_(std::move(psa_type)) , is_imported_(is_imported) { - if (from_dialog_id_.is_valid() != from_message_id_.is_valid()) { - from_dialog_id_ = DialogId(); - from_message_id_ = MessageId(); - } + last_message_info_.validate(); } static unique_ptr get_message_forward_info( Td *td, telegram_api::object_ptr &&forward_header); static unique_ptr copy_message_forward_info(Td *td, const MessageForwardInfo &forward_info, - DialogId from_dialog_id, MessageId from_message_id); + LastForwardedMessageInfo &&last_message_info); td_api::object_ptr get_message_forward_info_object(Td *td) const; @@ -87,11 +121,11 @@ class MessageForwardInfo { } DialogId get_last_dialog_id() const { - return from_dialog_id_; + return last_message_info_.get_dialog_id(); } MessageFullId get_last_message_full_id() const { - return {from_dialog_id_, from_message_id_}; + return last_message_info_.get_message_full_id(); } template @@ -101,6 +135,10 @@ class MessageForwardInfo { void parse(ParserT &parser); }; +bool operator==(const LastForwardedMessageInfo &lhs, const LastForwardedMessageInfo &rhs); + +bool operator!=(const LastForwardedMessageInfo &lhs, const LastForwardedMessageInfo &rhs); + bool operator==(const MessageForwardInfo &lhs, const MessageForwardInfo &rhs); bool operator!=(const MessageForwardInfo &lhs, const MessageForwardInfo &rhs); diff --git a/td/telegram/MessageForwardInfo.hpp b/td/telegram/MessageForwardInfo.hpp index 56c44ee9c..9bdf8bb1e 100644 --- a/td/telegram/MessageForwardInfo.hpp +++ b/td/telegram/MessageForwardInfo.hpp @@ -16,20 +16,36 @@ namespace td { +template +void LastForwardedMessageInfo::store(StorerT &storer) const { + BEGIN_STORE_FLAGS(); + END_STORE_FLAGS(); + td::store(dialog_id_, storer); + td::store(message_id_, storer); +} + +template +void LastForwardedMessageInfo::parse(ParserT &parser) { + BEGIN_PARSE_FLAGS(); + END_PARSE_FLAGS(); + td::parse(dialog_id_, parser); + td::parse(message_id_, parser); + validate(); +} + template void MessageForwardInfo::store(StorerT &storer) const { - bool has_from = from_dialog_id_.is_valid() && from_message_id_.is_valid(); + bool has_last_message_info = !last_message_info_.is_empty(); bool has_psa_type = !psa_type_.empty(); BEGIN_STORE_FLAGS(); STORE_FLAG(is_imported_); - STORE_FLAG(has_from); + STORE_FLAG(has_last_message_info); STORE_FLAG(has_psa_type); END_STORE_FLAGS(); td::store(origin_, storer); td::store(date_, storer); - if (has_from) { - td::store(from_dialog_id_, storer); - td::store(from_message_id_, storer); + if (has_last_message_info) { + td::store(last_message_info_, storer); } if (has_psa_type) { td::store(psa_type_, storer); @@ -38,22 +54,17 @@ void MessageForwardInfo::store(StorerT &storer) const { template void MessageForwardInfo::parse(ParserT &parser) { - bool has_from; + bool has_last_message_info; bool has_psa_type; BEGIN_PARSE_FLAGS(); PARSE_FLAG(is_imported_); - PARSE_FLAG(has_from); + PARSE_FLAG(has_last_message_info); PARSE_FLAG(has_psa_type); END_PARSE_FLAGS(); td::parse(origin_, parser); td::parse(date_, parser); - if (has_from) { - td::parse(from_dialog_id_, parser); - td::parse(from_message_id_, parser); - if (!from_dialog_id_.is_valid() || !from_message_id_.is_valid()) { - from_dialog_id_ = DialogId(); - from_message_id_ = MessageId(); - } + if (has_last_message_info) { + td::parse(last_message_info_, parser); } if (has_psa_type) { td::parse(psa_type_, parser); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index caf53952b..18554b0e1 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4284,19 +4284,20 @@ void MessagesManager::Message::parse(ParserT &parser) { forward_origin = MessageOrigin(forward_sender_user_id, forward_sender_dialog_id, forward_message_id, std::move(forward_author_signature), std::move(forward_sender_name)); } - DialogId forward_from_dialog_id; - MessageId forward_from_message_id; + LastForwardedMessageInfo last_message_info; if (legacy_has_forward_from) { + DialogId forward_from_dialog_id; + MessageId forward_from_message_id; parse(forward_from_dialog_id, parser); parse(forward_from_message_id, parser); + last_message_info = LastForwardedMessageInfo(forward_from_dialog_id, forward_from_message_id); } string psa_type; if (legacy_has_forward_psa_type) { parse(psa_type, parser); } - forward_info = - td::make_unique(std::move(forward_origin), forward_date, forward_from_dialog_id, - forward_from_message_id, std::move(psa_type), legacy_is_imported); + forward_info = td::make_unique( + std::move(forward_origin), forward_date, std::move(last_message_info), std::move(psa_type), legacy_is_imported); } if (has_real_forward_from) { parse(real_forward_from_dialog_id, parser); @@ -25813,25 +25814,21 @@ unique_ptr MessagesManager::create_message_forward_info(Dial auto my_dialog_id = td_->dialog_manager_->get_my_dialog_id(); - DialogId saved_from_dialog_id; - MessageId saved_from_message_id; + LastForwardedMessageInfo last_message_info; if (to_dialog_id == my_dialog_id) { - saved_from_dialog_id = from_dialog_id; - saved_from_message_id = m->message_id; + last_message_info = LastForwardedMessageInfo(from_dialog_id, m->message_id); } else if (content_type == MessageContentType::Audio || content_type == MessageContentType::Story) { return nullptr; } if (m->forward_info != nullptr) { - return MessageForwardInfo::copy_message_forward_info(td_, *m->forward_info, saved_from_dialog_id, - saved_from_message_id); + return MessageForwardInfo::copy_message_forward_info(td_, *m->forward_info, std::move(last_message_info)); } if (from_dialog_id != my_dialog_id || content_type == MessageContentType::Dice) { auto origin = get_forwarded_message_origin(from_dialog_id, m); if (!origin.is_empty()) { - return td::make_unique(std::move(origin), m->date, saved_from_dialog_id, - saved_from_message_id, "", false); + return td::make_unique(std::move(origin), m->date, std::move(last_message_info), "", false); } } return nullptr;