Add some MessageForwardInfo methods.

This commit is contained in:
levlam 2024-01-11 18:46:10 +03:00
parent 3cb8ef9807
commit 3ba660a655
4 changed files with 102 additions and 77 deletions

View File

@ -6,8 +6,84 @@
//
#include "td/telegram/MessageForwardInfo.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Td.h"
#include "td/utils/logging.h"
namespace td {
unique_ptr<MessageForwardInfo> MessageForwardInfo::get_message_forward_info(
Td *td, telegram_api::object_ptr<telegram_api::messageFwdHeader> &&forward_header) {
if (forward_header == nullptr) {
return nullptr;
}
auto date = forward_header->date_;
if (date <= 0) {
LOG(ERROR) << "Wrong date in message forward header: " << oneline(to_string(forward_header));
return nullptr;
}
DialogId from_dialog_id;
MessageId from_message_id;
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();
} else {
td->dialog_manager_->force_create_dialog(from_dialog_id, "get_message_forward_info", true);
}
}
bool is_imported = forward_header->imported_;
auto psa_type = std::move(forward_header->psa_type_);
auto r_origin = MessageOrigin::get_message_origin(td, std::move(forward_header));
if (r_origin.is_error()) {
return nullptr;
}
return td::make_unique<MessageForwardInfo>(r_origin.move_as_ok(), date, from_dialog_id, from_message_id,
std::move(psa_type), is_imported);
}
td_api::object_ptr<td_api::messageForwardInfo> MessageForwardInfo::get_message_forward_info_object(Td *td) const {
if (is_imported) {
return nullptr;
}
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_api::object_ptr<td_api::messageImportInfo> MessageForwardInfo::get_message_import_info_object() const {
if (!is_imported) {
return nullptr;
}
return td_api::make_object<td_api::messageImportInfo>(origin.get_sender_name(), date);
}
void MessageForwardInfo::add_dependencies(Dependencies &dependencies) const {
origin.add_dependencies(dependencies);
dependencies.add_dialog_and_dependencies(from_dialog_id);
}
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
}
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());
}
}
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 &&

View File

@ -6,15 +6,20 @@
//
#pragma once
#include "td/telegram/ChannelId.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/MessageOrigin.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h"
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
namespace td {
class Dependencies;
class Td;
struct MessageForwardInfo {
@ -37,6 +42,19 @@ struct MessageForwardInfo {
, is_imported(is_imported) {
}
static unique_ptr<MessageForwardInfo> get_message_forward_info(
Td *td, telegram_api::object_ptr<telegram_api::messageFwdHeader> &&forward_header);
td_api::object_ptr<td_api::messageForwardInfo> get_message_forward_info_object(Td *td) const;
td_api::object_ptr<td_api::messageImportInfo> get_message_import_info_object() const;
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;
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -11615,7 +11615,7 @@ vector<UserId> MessagesManager::get_message_user_ids(const Message *m) const {
user_ids.push_back(m->via_bot_user_id);
}
if (m->forward_info != nullptr) {
m->forward_info->origin.add_user_ids(user_ids);
m->forward_info->add_min_user_ids(user_ids);
}
append(user_ids, get_message_content_min_user_ids(td_, m->content.get()));
if (!m->replied_message_info.is_empty()) {
@ -11630,11 +11630,7 @@ vector<ChannelId> MessagesManager::get_message_channel_ids(const Message *m) con
channel_ids.push_back(m->sender_dialog_id.get_channel_id());
}
if (m->forward_info != nullptr) {
m->forward_info->origin.add_channel_ids(channel_ids);
}
if (m->forward_info != nullptr && m->forward_info->from_dialog_id.is_valid() &&
m->forward_info->from_dialog_id.get_type() == DialogType::Channel) {
channel_ids.push_back(m->forward_info->from_dialog_id.get_channel_id());
m->forward_info->add_min_channel_ids(channel_ids);
}
append(channel_ids, get_message_content_min_channel_ids(td_, m->content.get()));
if (!m->replied_message_info.is_empty()) {
@ -13076,7 +13072,7 @@ std::pair<DialogId, unique_ptr<MessagesManager::Message>> MessagesManager::creat
message->disable_web_page_preview = message_info.disable_web_page_preview;
message->edit_date = edit_date;
message->random_id = message_info.random_id;
message->forward_info = get_message_forward_info(std::move(message_info.forward_header));
message->forward_info = MessageForwardInfo::get_message_forward_info(td_, std::move(message_info.forward_header));
message->replied_message_info = std::move(message_info.reply_header.replied_message_info_);
message->top_thread_message_id = top_thread_message_id;
message->is_topic_message = is_topic_message;
@ -22184,8 +22180,8 @@ td_api::object_ptr<td_api::message> MessagesManager::get_dialog_event_log_messag
auto sender = get_message_sender_object_const(td_, m->sender_user_id, m->sender_dialog_id,
"get_dialog_event_log_message_object");
auto forward_info = get_message_forward_info_object(m->forward_info);
auto import_info = get_message_import_info_object(m->forward_info);
auto forward_info = m->forward_info == nullptr ? nullptr : m->forward_info->get_message_forward_info_object(td_);
auto import_info = m->forward_info == nullptr ? nullptr : m->forward_info->get_message_import_info_object();
auto interaction_info = get_message_interaction_info_object(dialog_id, m);
auto can_be_saved = can_save_message(dialog_id, m);
auto via_bot_user_id = td_->contacts_manager_->get_user_id_object(m->via_bot_user_id, "via_bot_user_id");
@ -22260,8 +22256,8 @@ tl_object_ptr<td_api::message> MessagesManager::get_message_object(DialogId dial
m->ttl_period == 0 ? 0.0 : clamp(m->date + m->ttl_period - G()->server_time(), 1e-3, m->ttl_period - 1e-3);
auto sender = get_message_sender_object_const(td_, m->sender_user_id, m->sender_dialog_id, source);
auto scheduling_state = is_scheduled ? get_message_scheduling_state_object(m->date) : nullptr;
auto forward_info = get_message_forward_info_object(m->forward_info);
auto import_info = get_message_import_info_object(m->forward_info);
auto forward_info = m->forward_info == nullptr ? nullptr : m->forward_info->get_message_forward_info_object(td_);
auto import_info = m->forward_info == nullptr ? nullptr : m->forward_info->get_message_import_info_object();
auto interaction_info = is_bot ? nullptr : get_message_interaction_info_object(dialog_id, m);
auto unread_reactions = get_unread_reactions_object(dialog_id, m);
auto can_be_saved = can_save_message(dialog_id, m);
@ -22876,8 +22872,7 @@ void MessagesManager::add_message_dependencies(Dependencies &dependencies, const
dependencies.add_dialog_and_dependencies(m->real_forward_from_dialog_id);
dependencies.add(m->via_bot_user_id);
if (m->forward_info != nullptr) {
m->forward_info->origin.add_dependencies(dependencies);
dependencies.add_dialog_and_dependencies(m->forward_info->from_dialog_id);
m->forward_info->add_dependencies(dependencies);
}
for (const auto &replier_min_channel : m->reply_info.replier_min_channels_) {
LOG(INFO) << "Add min replied " << replier_min_channel.first;
@ -25635,61 +25630,6 @@ bool MessagesManager::can_set_game_score(DialogId dialog_id, const Message *m) c
return true;
}
unique_ptr<MessageForwardInfo> MessagesManager::get_message_forward_info(
tl_object_ptr<telegram_api::messageFwdHeader> &&forward_header) {
if (forward_header == nullptr) {
return nullptr;
}
auto date = forward_header->date_;
if (date <= 0) {
LOG(ERROR) << "Wrong date in message forward header: " << oneline(to_string(forward_header));
return nullptr;
}
DialogId from_dialog_id;
MessageId from_message_id;
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();
} else {
force_create_dialog(from_dialog_id, "get_message_forward_info", true);
}
}
bool is_imported = forward_header->imported_;
auto psa_type = std::move(forward_header->psa_type_);
auto r_origin = MessageOrigin::get_message_origin(td_, std::move(forward_header));
if (r_origin.is_error()) {
return nullptr;
}
return td::make_unique<MessageForwardInfo>(r_origin.move_as_ok(), date, from_dialog_id, from_message_id,
std::move(psa_type), is_imported);
}
td_api::object_ptr<td_api::messageForwardInfo> MessagesManager::get_message_forward_info_object(
const unique_ptr<MessageForwardInfo> &forward_info) const {
if (forward_info == nullptr || forward_info->is_imported) {
return nullptr;
}
return td_api::make_object<td_api::messageForwardInfo>(
forward_info->origin.get_message_origin_object(td_), forward_info->date, forward_info->psa_type,
get_chat_id_object(forward_info->from_dialog_id, "messageForwardInfo"), forward_info->from_message_id.get());
}
td_api::object_ptr<td_api::messageImportInfo> MessagesManager::get_message_import_info_object(
const unique_ptr<MessageForwardInfo> &forward_info) const {
if (forward_info == nullptr || !forward_info->is_imported) {
return nullptr;
}
return td_api::make_object<td_api::messageImportInfo>(forward_info->origin.get_sender_name(), forward_info->date);
}
Result<unique_ptr<ReplyMarkup>> MessagesManager::get_dialog_reply_markup(
DialogId dialog_id, tl_object_ptr<td_api::ReplyMarkup> &&reply_markup_ptr) const {
if (reply_markup_ptr == nullptr) {

View File

@ -2673,15 +2673,6 @@ class MessagesManager final : public Actor {
tl_object_ptr<telegram_api::InputPeer> get_send_message_as_input_peer(const Message *m) const;
unique_ptr<MessageForwardInfo> get_message_forward_info(
tl_object_ptr<telegram_api::messageFwdHeader> &&forward_header);
td_api::object_ptr<td_api::messageForwardInfo> get_message_forward_info_object(
const unique_ptr<MessageForwardInfo> &forward_info) const;
td_api::object_ptr<td_api::messageImportInfo> get_message_import_info_object(
const unique_ptr<MessageForwardInfo> &forward_info) const;
void ttl_read_history(Dialog *d, bool is_outgoing, MessageId from_message_id, MessageId till_message_id,
double view_date);
void ttl_read_history_impl(DialogId dialog_id, bool is_outgoing, MessageId from_message_id, MessageId till_message_id,