Add class LastForwardedMessageInfo.

This commit is contained in:
levlam 2024-01-11 22:33:12 +03:00
parent b287f2f177
commit 1dca0272be
4 changed files with 157 additions and 72 deletions

View File

@ -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<UserId> &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<ChannelId> &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> MessageForwardInfo::get_message_forward_info(
Td *td, telegram_api::object_ptr<telegram_api::messageFwdHeader> &&forward_header) {
if (forward_header == nullptr) {
@ -26,18 +72,18 @@ unique_ptr<MessageForwardInfo> 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> MessageForwardInfo::get_message_forward_info(
return nullptr;
}
return td::make_unique<MessageForwardInfo>(r_origin.move_as_ok(), date, from_dialog_id, from_message_id,
return td::make_unique<MessageForwardInfo>(r_origin.move_as_ok(), date, std::move(last_message_info),
std::move(psa_type), is_imported);
}
unique_ptr<MessageForwardInfo> 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> MessageForwardInfo::copy_message_forward_info(
Td *td, const MessageForwardInfo &forward_info, LastForwardedMessageInfo &&last_message_info) {
last_message_info.validate();
auto result = make_unique<MessageForwardInfo>(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<td_api::messageForwardInfo> MessageForwardInfo::get_message_f
if (is_imported_) {
return nullptr;
}
auto last_message_full_id = get_last_message_full_id();
return td_api::make_object<td_api::messageForwardInfo>(
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<td_api::messageImportInfo> MessageForwardInfo::get_message_import_info_object() const {
@ -85,19 +127,17 @@ td_api::object_ptr<td_api::messageImportInfo> 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<UserId> &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<ChannelId> &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<MessageForwardInfo> &lhs, const unique_ptr<Mess
StringBuilder &operator<<(StringBuilder &string_builder, const MessageForwardInfo &forward_info) {
string_builder << "MessageForwardInfo[" << (forward_info.is_imported_ ? "imported " : "") << forward_info.origin_;
if (!forward_info.psa_type_.empty()) {
string_builder << ", psa_type_ " << forward_info.psa_type_;
string_builder << ", psa_type " << forward_info.psa_type_;
}
if (forward_info.from_dialog_id_.is_valid() || forward_info.from_message_id_.is_valid()) {
string_builder << ", from " << MessageFullId(forward_info.from_dialog_id_, forward_info.from_message_id_);
if (!forward_info.last_message_info_.is_empty()) {
string_builder << ", from " << forward_info.last_message_info_;
}
return string_builder << " at " << forward_info.date_ << ']';
}

View File

@ -23,11 +23,49 @@ namespace td {
class Dependencies;
class Td;
class LastForwardedMessageInfo {
DialogId dialog_id_;
MessageId message_id_;
friend bool operator==(const LastForwardedMessageInfo &lhs, const LastForwardedMessageInfo &rhs);
friend StringBuilder &operator<<(StringBuilder &string_builder, const LastForwardedMessageInfo &last_message_info);
public:
LastForwardedMessageInfo() = default;
LastForwardedMessageInfo(DialogId dialog_id, MessageId message_id) : dialog_id_(dialog_id), message_id_(message_id) {
}
bool is_empty() const;
bool validate();
void add_dependencies(Dependencies &dependencies) const;
void add_min_user_ids(vector<UserId> &user_ids) const;
void add_min_channel_ids(vector<ChannelId> &channel_ids) const;
DialogId get_dialog_id() const {
return dialog_id_;
}
MessageFullId get_message_full_id() const {
return {dialog_id_, message_id_};
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
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<MessageForwardInfo> get_message_forward_info(
Td *td, telegram_api::object_ptr<telegram_api::messageFwdHeader> &&forward_header);
static unique_ptr<MessageForwardInfo> 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<td_api::messageForwardInfo> 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 <class StorerT>
@ -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);

View File

@ -16,20 +16,36 @@
namespace td {
template <class StorerT>
void LastForwardedMessageInfo::store(StorerT &storer) const {
BEGIN_STORE_FLAGS();
END_STORE_FLAGS();
td::store(dialog_id_, storer);
td::store(message_id_, storer);
}
template <class ParserT>
void LastForwardedMessageInfo::parse(ParserT &parser) {
BEGIN_PARSE_FLAGS();
END_PARSE_FLAGS();
td::parse(dialog_id_, parser);
td::parse(message_id_, parser);
validate();
}
template <class StorerT>
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 <class ParserT>
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);

View File

@ -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<MessageForwardInfo>(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<MessageForwardInfo>(
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<MessageForwardInfo> 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<MessageForwardInfo>(std::move(origin), m->date, saved_from_dialog_id,
saved_from_message_id, "", false);
return td::make_unique<MessageForwardInfo>(std::move(origin), m->date, std::move(last_message_info), "", false);
}
}
return nullptr;