Fix handling of min-channels in recent repliers.

This commit is contained in:
levlam 2021-12-22 20:15:18 +03:00
parent 1fd2e3b629
commit 160ab905f1
9 changed files with 140 additions and 36 deletions

View File

@ -748,7 +748,7 @@ messageForwardInfo origin:MessageForwardOrigin date:int32 public_service_announc
//@description Contains information about replies to a message
//@reply_count Number of times the message was directly or indirectly replied
//@recent_replier_ids Identifiers of at most 3 recent repliers to the message; available in channels with a discussion supergroup
//@recent_replier_ids Identifiers of at most 3 recent repliers to the message; available in channels with a discussion supergroup. The users and chats are expected to be inaccessible: only their photo and name will be available
//@last_read_inbox_message_id Identifier of the last read incoming reply to the message
//@last_read_outbox_message_id Identifier of the last read outgoing reply to the message
//@last_message_id Identifier of the last reply to the message

View File

@ -14573,6 +14573,13 @@ const MinChannel *ContactsManager::get_min_channel(ChannelId channel_id) const {
return it->second.get();
}
void ContactsManager::add_min_channel(ChannelId channel_id, const MinChannel &min_channel) {
if (have_channel(channel_id) || have_min_channel(channel_id)) {
return;
}
min_channels_[channel_id] = td::make_unique<MinChannel>(min_channel);
}
const ContactsManager::Channel *ContactsManager::get_channel(ChannelId channel_id) const {
auto p = channels_.find(channel_id);
if (p == channels_.end()) {
@ -16121,7 +16128,7 @@ td_api::object_ptr<td_api::updateSupergroup> ContactsManager::get_update_unknown
auto min_channel = get_min_channel(channel_id);
return td_api::make_object<td_api::updateSupergroup>(td_api::make_object<td_api::supergroup>(
channel_id.get(), string(), 0, DialogParticipantStatus::Banned(0).get_chat_member_status_object(), 0, false,
false, false, false, true, min_channel == nullptr ? false : !min_channel->is_megagroup_, false, string(), false,
false, false, false, min_channel == nullptr ? true : !min_channel->is_megagroup_, false, false, string(), false,
false));
}

View File

@ -494,6 +494,7 @@ class ContactsManager final : public Actor {
bool have_min_channel(ChannelId channel_id) const;
const MinChannel *get_min_channel(ChannelId channel_id) const;
void add_min_channel(ChannelId channel_id, const MinChannel &min_channel);
bool have_channel(ChannelId channel_id) const;
bool have_channel_force(ChannelId channel_id);

View File

@ -67,6 +67,10 @@ bool resolve_dependencies_force(Td *td, const Dependencies &dependencies, const
}
for (auto channel_id : dependencies.channel_ids) {
if (channel_id.is_valid() && !td->contacts_manager_->have_channel_force(channel_id)) {
if (td->contacts_manager_->have_min_channel(channel_id)) {
LOG(INFO) << "Can't find " << channel_id << " from " << source << ", but have it as a min-channel";
continue;
}
LOG(ERROR) << "Can't find " << channel_id << " from " << source;
success = false;
}

View File

@ -8,7 +8,9 @@
#include "td/telegram/ContactsManager.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/MinChannel.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
@ -16,7 +18,7 @@
namespace td {
MessageReplyInfo::MessageReplyInfo(tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot) {
MessageReplyInfo::MessageReplyInfo(Td *td, tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot) {
if (reply_info == nullptr || is_bot || reply_info->channel_id_ == 777) {
return;
}
@ -41,15 +43,39 @@ MessageReplyInfo::MessageReplyInfo(tl_object_ptr<telegram_api::messageReplies> &
if (is_comment) {
for (const auto &peer : reply_info->recent_repliers_) {
DialogId dialog_id(peer);
if (dialog_id.is_valid()) {
// save all valid dialog_id, despite we can have no info about some of them
recent_replier_dialog_ids.push_back(dialog_id);
} else {
if (!dialog_id.is_valid()) {
LOG(ERROR) << "Receive " << dialog_id << " as a recent replier";
continue;
}
if (td::contains(recent_replier_dialog_ids, dialog_id)) {
LOG(ERROR) << "Receive duplicate " << dialog_id << " as a recent replier";
continue;
}
if (!td->messages_manager_->have_dialog_info(dialog_id)) {
auto dialog_type = dialog_id.get_type();
if (dialog_type == DialogType::User) {
auto replier_user_id = dialog_id.get_user_id();
if (!td->contacts_manager_->have_min_user(replier_user_id)) {
LOG(ERROR) << "Have no info about replied " << replier_user_id;
continue;
}
} else if (dialog_type == DialogType::Channel) {
auto replier_channel_id = dialog_id.get_channel_id();
auto min_channel = td->contacts_manager_->get_min_channel(replier_channel_id);
if (min_channel == nullptr) {
LOG(ERROR) << "Have no info about replied " << replier_channel_id;
continue;
}
replier_min_channels.emplace_back(replier_channel_id, *min_channel);
} else {
LOG(ERROR) << "Have no info about replied " << dialog_id;
continue;
}
}
recent_replier_dialog_ids.push_back(dialog_id);
if (recent_replier_dialog_ids.size() == MAX_RECENT_REPLIERS) {
break;
}
}
if (recent_replier_dialog_ids.size() > MAX_RECENT_REPLIERS) {
recent_replier_dialog_ids.resize(MAX_RECENT_REPLIERS);
}
}
if ((reply_info->flags_ & telegram_api::messageReplies::MAX_ID_MASK) != 0 &&
@ -75,7 +101,8 @@ bool MessageReplyInfo::need_update_to(const MessageReplyInfo &other) const {
return false;
}
return reply_count != other.reply_count || recent_replier_dialog_ids != other.recent_replier_dialog_ids ||
is_comment != other.is_comment || channel_id != other.channel_id;
replier_min_channels.size() != other.replier_min_channels.size() || is_comment != other.is_comment ||
channel_id != other.channel_id;
}
bool MessageReplyInfo::update_max_message_ids(const MessageReplyInfo &other) {
@ -120,6 +147,16 @@ bool MessageReplyInfo::add_reply(DialogId replier_dialog_id, MessageId reply_mes
reply_count += diff;
if (is_comment && replier_dialog_id.is_valid()) {
if (replier_dialog_id.get_type() == DialogType::Channel) {
// the replier_dialog_id is never min, because it is the sender of a message
for (auto it = replier_min_channels.begin(); it != replier_min_channels.end(); ++it) {
if (it->first == replier_dialog_id.get_channel_id()) {
replier_min_channels.erase(it);
break;
}
}
}
td::remove(recent_replier_dialog_ids, replier_dialog_id);
if (diff > 0) {
recent_replier_dialog_ids.insert(recent_replier_dialog_ids.begin(), replier_dialog_id);
@ -140,23 +177,48 @@ bool MessageReplyInfo::add_reply(DialogId replier_dialog_id, MessageId reply_mes
return true;
}
td_api::object_ptr<td_api::messageReplyInfo> MessageReplyInfo::get_message_reply_info_object(
ContactsManager *contacts_manager, const MessagesManager *messages_manager) const {
bool MessageReplyInfo::need_reget(const Td *td) const {
for (auto &dialog_id : recent_replier_dialog_ids) {
if (dialog_id.get_type() != DialogType::User && !td->messages_manager_->have_dialog_info(dialog_id)) {
if (dialog_id.get_type() == DialogType::Channel &&
td->contacts_manager_->have_min_channel(dialog_id.get_channel_id())) {
return false;
}
LOG(INFO) << "Reget a message because of replied " << dialog_id;
return true;
}
}
return false;
}
td_api::object_ptr<td_api::messageReplyInfo> MessageReplyInfo::get_message_reply_info_object(Td *td) const {
if (is_empty()) {
return nullptr;
}
vector<td_api::object_ptr<td_api::MessageSender>> recent_repliers;
for (auto recent_replier_dialog_id : recent_replier_dialog_ids) {
if (recent_replier_dialog_id.get_type() == DialogType::User) {
auto user_id = recent_replier_dialog_id.get_user_id();
if (contacts_manager->have_min_user(user_id)) {
for (auto dialog_id : recent_replier_dialog_ids) {
auto dialog_type = dialog_id.get_type();
if (dialog_type == DialogType::User) {
auto user_id = dialog_id.get_user_id();
if (td->contacts_manager_->have_min_user(user_id)) {
recent_repliers.push_back(td_api::make_object<td_api::messageSenderUser>(
contacts_manager->get_user_id_object(user_id, "get_message_reply_info_object")));
td->contacts_manager_->get_user_id_object(user_id, "get_message_reply_info_object")));
} else {
LOG(ERROR) << "Skip unknown replied " << user_id;
}
} else {
if (messages_manager->have_dialog(recent_replier_dialog_id)) {
recent_repliers.push_back(td_api::make_object<td_api::messageSenderChat>(recent_replier_dialog_id.get()));
if (!td->messages_manager_->have_dialog(dialog_id) &&
(td->messages_manager_->have_dialog_info(dialog_id) ||
(dialog_type == DialogType::Channel &&
td->contacts_manager_->have_min_channel(dialog_id.get_channel_id())))) {
LOG(INFO) << "Force creation of " << dialog_id;
td->messages_manager_->force_create_dialog(dialog_id, "get_message_reply_info_object", true);
}
if (td->messages_manager_->have_dialog(dialog_id)) {
recent_repliers.push_back(td_api::make_object<td_api::messageSenderChat>(dialog_id.get()));
} else {
LOG(ERROR) << "Skip unknown replied " << dialog_id;
}
}
}

View File

@ -9,6 +9,7 @@
#include "td/telegram/ChannelId.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/MinChannel.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
@ -16,16 +17,18 @@
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
#include <utility>
namespace td {
class ContactsManager;
class MessagesManager;
class Td;
struct MessageReplyInfo {
int32 reply_count = -1;
int32 pts = -1;
vector<DialogId> recent_replier_dialog_ids; // comments only
ChannelId channel_id; // comments only
vector<DialogId> recent_replier_dialog_ids; // comments only
vector<std::pair<ChannelId, MinChannel>> replier_min_channels; // comments only
ChannelId channel_id; // comments only
MessageId max_message_id;
MessageId last_read_inbox_message_id;
MessageId last_read_outbox_message_id;
@ -35,7 +38,7 @@ struct MessageReplyInfo {
MessageReplyInfo() = default;
MessageReplyInfo(tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot);
MessageReplyInfo(Td *td, tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot);
bool is_empty() const {
return reply_count < 0;
@ -50,8 +53,9 @@ struct MessageReplyInfo {
bool add_reply(DialogId replier_dialog_id, MessageId reply_message_id, int diff);
td_api::object_ptr<td_api::messageReplyInfo> get_message_reply_info_object(
ContactsManager *contacts_manager, const MessagesManager *messages_manager) const;
bool need_reget(const Td *td) const;
td_api::object_ptr<td_api::messageReplyInfo> get_message_reply_info_object(Td *td) const;
template <class StorerT>
void store(StorerT &storer) const {
@ -61,6 +65,7 @@ struct MessageReplyInfo {
bool has_max_message_id = max_message_id.is_valid();
bool has_last_read_inbox_message_id = last_read_inbox_message_id.is_valid();
bool has_last_read_outbox_message_id = last_read_outbox_message_id.is_valid();
bool has_replier_min_channels = !replier_min_channels.empty();
BEGIN_STORE_FLAGS();
STORE_FLAG(is_comment);
STORE_FLAG(has_recent_replier_dialog_ids);
@ -68,6 +73,7 @@ struct MessageReplyInfo {
STORE_FLAG(has_max_message_id);
STORE_FLAG(has_last_read_inbox_message_id);
STORE_FLAG(has_last_read_outbox_message_id);
STORE_FLAG(has_replier_min_channels);
END_STORE_FLAGS();
td::store(reply_count, storer);
td::store(pts, storer);
@ -86,6 +92,9 @@ struct MessageReplyInfo {
if (has_last_read_outbox_message_id) {
td::store(last_read_outbox_message_id, storer);
}
if (has_replier_min_channels) {
td::store(replier_min_channels, storer);
}
}
template <class ParserT>
@ -95,6 +104,7 @@ struct MessageReplyInfo {
bool has_max_message_id;
bool has_last_read_inbox_message_id;
bool has_last_read_outbox_message_id;
bool has_replier_min_channels;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_comment);
PARSE_FLAG(has_recent_replier_dialog_ids);
@ -102,6 +112,7 @@ struct MessageReplyInfo {
PARSE_FLAG(has_max_message_id);
PARSE_FLAG(has_last_read_inbox_message_id);
PARSE_FLAG(has_last_read_outbox_message_id);
PARSE_FLAG(has_replier_min_channels);
END_PARSE_FLAGS();
td::parse(reply_count, parser);
td::parse(pts, parser);
@ -120,6 +131,10 @@ struct MessageReplyInfo {
if (has_last_read_outbox_message_id) {
td::parse(last_read_outbox_message_id, parser);
}
if (has_replier_min_channels) {
td::parse(replier_min_channels, parser);
}
if (channel_id.get() == 777) {
*this = MessageReplyInfo();
}

View File

@ -35,6 +35,7 @@
#include "td/telegram/MessageEntity.hpp"
#include "td/telegram/MessagesDb.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/MinChannel.h"
#include "td/telegram/misc.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/net/NetActor.h"
@ -6760,7 +6761,7 @@ void MessagesManager::update_message_interaction_info(FullMessageId full_message
forward_count = m->forward_count;
}
bool is_empty_reply_info = reply_info == nullptr;
MessageReplyInfo new_reply_info(std::move(reply_info), td_->auth_manager_->is_bot());
MessageReplyInfo new_reply_info(td_, std::move(reply_info), td_->auth_manager_->is_bot());
if (new_reply_info.is_empty() && !is_empty_reply_info) {
has_reply_info = false;
}
@ -6842,7 +6843,7 @@ td_api::object_ptr<td_api::messageInteractionInfo> MessagesManager::get_message_
td_api::object_ptr<td_api::messageReplyInfo> reply_info;
if (is_visible_reply_info) {
reply_info = m->reply_info.get_message_reply_info_object(td_->contacts_manager_.get(), this);
reply_info = m->reply_info.get_message_reply_info_object(td_);
CHECK(reply_info != nullptr);
}
@ -13732,7 +13733,7 @@ std::pair<DialogId, unique_ptr<MessagesManager::Message>> MessagesManager::creat
LOG(ERROR) << "Wrong forward_count = " << forward_count << " received in " << message_id << " in " << dialog_id;
forward_count = 0;
}
MessageReplyInfo reply_info(std::move(message_info.reply_info), td_->auth_manager_->is_bot());
MessageReplyInfo reply_info(td_, std::move(message_info.reply_info), td_->auth_manager_->is_bot());
if (!top_thread_message_id.is_valid() && !is_broadcast_channel(dialog_id) &&
is_active_message_reply_info(dialog_id, reply_info) && !message_id.is_scheduled()) {
top_thread_message_id = message_id;
@ -17410,7 +17411,7 @@ td_api::object_ptr<td_api::messageThreadInfo> MessagesManager::get_message_threa
auto message = get_message_object(d->dialog_id, m, "get_message_thread_info_object");
if (message != nullptr) {
if (message->interaction_info_ != nullptr && message->interaction_info_->reply_info_ != nullptr) {
reply_info = m->reply_info.get_message_reply_info_object(td_->contacts_manager_.get(), this);
reply_info = m->reply_info.get_message_reply_info_object(td_);
CHECK(reply_info != nullptr);
}
messages.push_back(std::move(message));
@ -24112,8 +24113,14 @@ void MessagesManager::add_message_dependencies(Dependencies &dependencies, const
add_dialog_and_dependencies(dependencies, m->forward_info->sender_dialog_id);
add_dialog_and_dependencies(dependencies, m->forward_info->from_dialog_id);
}
for (const auto &replier_min_channel : m->reply_info.replier_min_channels) {
LOG(INFO) << "Add min " << replier_min_channel.first;
td_->contacts_manager_->add_min_channel(replier_min_channel.first, replier_min_channel.second);
}
for (auto recent_replier_dialog_id : m->reply_info.recent_replier_dialog_ids) {
add_message_sender_dependencies(dependencies, recent_replier_dialog_id);
// don't load the dialog itself
// it will be created in get_message_reply_info_object if needed
add_dialog_dependencies(dependencies, recent_replier_dialog_id);
}
add_message_content_dependencies(dependencies, m->content.get());
add_reply_markup_dependencies(dependencies, m->reply_markup.get());
@ -34764,7 +34771,13 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source
}
if (!have_input_peer(dialog_id, AccessRights::Read)) {
if (!have_dialog_info(dialog_id)) {
LOG(ERROR) << "Have no info about " << dialog_id << " received from " << source << ", but forced to create it";
if (expect_no_access && dialog_id.get_type() == DialogType::Channel &&
td_->contacts_manager_->have_min_channel(dialog_id.get_channel_id())) {
LOG(INFO) << "Created " << dialog_id << " for min-channel from " << source;
} else {
LOG(ERROR) << "Have no info about " << dialog_id << " received from " << source
<< ", but forced to create it";
}
} else if (!expect_no_access) {
LOG(ERROR) << "Have no access to " << dialog_id << " received from " << source << ", but forced to create it";
}
@ -37194,7 +37207,8 @@ void MessagesManager::reget_message_from_server_if_needed(DialogId dialog_id, co
return;
}
if (need_reget_message_content(m->content.get()) || (m->legacy_layer != 0 && m->legacy_layer < MTPROTO_LAYER)) {
if (need_reget_message_content(m->content.get()) || (m->legacy_layer != 0 && m->legacy_layer < MTPROTO_LAYER) ||
m->reply_info.need_reget(td_)) {
FullMessageId full_message_id{dialog_id, m->message_id};
LOG(INFO) << "Reget from server " << full_message_id;
get_message_from_server(full_message_id, Auto(), "reget_message_from_server_if_needed");

View File

@ -3109,9 +3109,9 @@ class MessagesManager final : public Actor {
return !LOG_IS_STRIPPED(ERROR) && false;
}
static void dump_debug_message_op(const Dialog *d, int priority = 0);
void add_message_dependencies(Dependencies &dependencies, const Message *m);
static void add_message_dependencies(Dependencies &dependencies, const Message *m);
static void dump_debug_message_op(const Dialog *d, int priority = 0);
static void save_send_message_log_event(DialogId dialog_id, const Message *m);

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/telegram/Photo.h"
#include "td/telegram/Photo.hpp"
#include "td/utils/common.h"
#include "td/utils/tl_helpers.h"