2020-09-07 13:07:40 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2020-09-07 13:07:40 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/MessageReplyInfo.h"
|
|
|
|
|
2020-10-08 20:17:24 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2022-01-22 21:17:41 +01:00
|
|
|
#include "td/telegram/MessageSender.h"
|
2020-10-17 23:07:39 +02:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2021-12-22 18:15:18 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-09-07 13:07:40 +02:00
|
|
|
#include "td/utils/logging.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
#include "td/utils/misc.h"
|
2020-09-07 13:07:40 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-12-22 18:15:18 +01:00
|
|
|
MessageReplyInfo::MessageReplyInfo(Td *td, tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot) {
|
2022-10-29 19:18:49 +02:00
|
|
|
if (reply_info == nullptr) {
|
2020-09-07 13:07:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply_info->replies_ < 0) {
|
|
|
|
LOG(ERROR) << "Receive wrong " << to_string(reply_info);
|
|
|
|
return;
|
|
|
|
}
|
2022-10-29 19:18:49 +02:00
|
|
|
if (is_bot || reply_info->channel_id_ == 777) {
|
2022-11-21 16:35:28 +01:00
|
|
|
is_dropped_ = true;
|
2022-10-29 19:18:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
reply_count_ = reply_info->replies_;
|
|
|
|
pts_ = reply_info->replies_pts_;
|
2020-09-07 13:07:40 +02:00
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
is_comment_ = reply_info->comments_;
|
2020-09-15 18:45:45 +02:00
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
if (is_comment_) {
|
|
|
|
channel_id_ = ChannelId(reply_info->channel_id_);
|
|
|
|
if (!channel_id_.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << channel_id_;
|
|
|
|
channel_id_ = ChannelId();
|
|
|
|
is_comment_ = false;
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 18:45:45 +02:00
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
if (is_comment_) {
|
2020-10-01 21:28:54 +02:00
|
|
|
for (const auto &peer : reply_info->recent_repliers_) {
|
2020-09-15 18:45:45 +02:00
|
|
|
DialogId dialog_id(peer);
|
2021-12-22 18:15:18 +01:00
|
|
|
if (!dialog_id.is_valid()) {
|
2020-09-15 18:45:45 +02:00
|
|
|
LOG(ERROR) << "Receive " << dialog_id << " as a recent replier";
|
2021-12-22 18:15:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (td::contains(recent_replier_dialog_ids_, dialog_id)) {
|
2021-12-22 18:15:18 +01:00
|
|
|
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)) {
|
2022-10-05 20:29:05 +02:00
|
|
|
LOG(ERROR) << "Receive unknown replied " << replier_user_id;
|
2021-12-22 18:15:18 +01:00
|
|
|
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) {
|
2022-10-05 20:29:05 +02:00
|
|
|
LOG(ERROR) << "Receive unknown replied " << replier_channel_id;
|
2021-12-22 18:15:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
replier_min_channels_.emplace_back(replier_channel_id, *min_channel);
|
2021-12-22 18:15:18 +01:00
|
|
|
} else {
|
2022-10-05 20:29:05 +02:00
|
|
|
LOG(ERROR) << "Receive unknown replied " << dialog_id;
|
2021-12-22 18:15:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
recent_replier_dialog_ids_.push_back(dialog_id);
|
|
|
|
if (recent_replier_dialog_ids_.size() == MAX_RECENT_REPLIERS) {
|
2021-12-22 18:15:18 +01:00
|
|
|
break;
|
2020-09-15 18:45:45 +02:00
|
|
|
}
|
2021-12-20 13:47:03 +01:00
|
|
|
}
|
2020-09-19 21:01:28 +02:00
|
|
|
}
|
|
|
|
if ((reply_info->flags_ & telegram_api::messageReplies::MAX_ID_MASK) != 0 &&
|
|
|
|
ServerMessageId(reply_info->max_id_).is_valid()) {
|
2022-11-21 16:35:28 +01:00
|
|
|
max_message_id_ = MessageId(ServerMessageId(reply_info->max_id_));
|
2020-09-19 21:01:28 +02:00
|
|
|
}
|
|
|
|
if ((reply_info->flags_ & telegram_api::messageReplies::READ_MAX_ID_MASK) != 0 &&
|
|
|
|
ServerMessageId(reply_info->read_max_id_).is_valid()) {
|
2022-11-21 16:35:28 +01:00
|
|
|
last_read_inbox_message_id_ = MessageId(ServerMessageId(reply_info->read_max_id_));
|
2020-09-15 18:45:45 +02:00
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (last_read_inbox_message_id_ > max_message_id_) { // possible if last thread message was deleted after it was read
|
|
|
|
max_message_id_ = last_read_inbox_message_id_;
|
2020-09-21 15:37:07 +02:00
|
|
|
}
|
2020-10-01 21:28:54 +02:00
|
|
|
LOG(DEBUG) << "Parsed " << oneline(to_string(reply_info)) << " to " << *this;
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageReplyInfo::need_update_to(const MessageReplyInfo &other) const {
|
2020-09-30 14:47:42 +02:00
|
|
|
if (other.is_empty() && !is_empty()) {
|
|
|
|
// ignore updates to empty reply info, because we will hide the info ourselves
|
|
|
|
// return true;
|
|
|
|
}
|
2023-07-24 14:56:03 +02:00
|
|
|
if (other.is_comment_ != is_comment_ && !other.was_dropped()) {
|
|
|
|
LOG(ERROR) << "Reply info has changed from " << *this << " to " << other;
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other.pts_ < pts_ && !other.was_dropped()) {
|
2020-09-07 13:07:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
return reply_count_ != other.reply_count_ || recent_replier_dialog_ids_ != other.recent_replier_dialog_ids_ ||
|
|
|
|
replier_min_channels_.size() != other.replier_min_channels_.size() || is_comment_ != other.is_comment_ ||
|
|
|
|
channel_id_ != other.channel_id_;
|
2020-09-11 19:12:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 18:45:45 +02:00
|
|
|
bool MessageReplyInfo::update_max_message_ids(const MessageReplyInfo &other) {
|
2022-11-21 16:35:28 +01:00
|
|
|
return update_max_message_ids(other.max_message_id_, other.last_read_inbox_message_id_,
|
|
|
|
other.last_read_outbox_message_id_);
|
2020-09-16 15:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:32:59 +02:00
|
|
|
bool MessageReplyInfo::update_max_message_ids(MessageId other_max_message_id,
|
|
|
|
MessageId other_last_read_inbox_message_id,
|
|
|
|
MessageId other_last_read_outbox_message_id) {
|
2020-09-15 18:45:45 +02:00
|
|
|
bool result = false;
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other_last_read_inbox_message_id > last_read_inbox_message_id_) {
|
|
|
|
last_read_inbox_message_id_ = other_last_read_inbox_message_id;
|
2020-09-18 17:32:59 +02:00
|
|
|
result = true;
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other_last_read_outbox_message_id > last_read_outbox_message_id_) {
|
|
|
|
last_read_outbox_message_id_ = other_last_read_outbox_message_id;
|
2020-09-15 18:45:45 +02:00
|
|
|
result = true;
|
|
|
|
}
|
2022-11-19 09:21:41 +01:00
|
|
|
if (other_max_message_id.is_valid() ||
|
|
|
|
(!other_last_read_inbox_message_id.is_valid() && !other_last_read_outbox_message_id.is_valid())) {
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other_max_message_id < last_read_inbox_message_id_) {
|
|
|
|
other_max_message_id = last_read_inbox_message_id_;
|
2022-11-19 09:21:41 +01:00
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other_max_message_id < last_read_outbox_message_id_) {
|
|
|
|
other_max_message_id = last_read_outbox_message_id_;
|
2022-11-19 09:21:41 +01:00
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
if (other_max_message_id != max_message_id_) {
|
|
|
|
max_message_id_ = other_max_message_id;
|
2022-11-19 09:21:41 +01:00
|
|
|
result = true;
|
|
|
|
}
|
2020-09-21 15:37:07 +02:00
|
|
|
}
|
2020-09-15 18:45:45 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:55:53 +01:00
|
|
|
bool MessageReplyInfo::add_reply(DialogId replier_dialog_id, MessageId reply_message_id, int diff) {
|
2020-09-11 19:12:31 +02:00
|
|
|
CHECK(!is_empty());
|
2020-10-31 11:55:53 +01:00
|
|
|
CHECK(diff == +1 || diff == -1);
|
2020-09-11 19:12:31 +02:00
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
if (diff == -1 && reply_count_ == 0) {
|
2020-10-31 11:55:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
reply_count_ += diff;
|
|
|
|
if (is_comment_ && replier_dialog_id.is_valid()) {
|
2021-12-22 18:15:18 +01:00
|
|
|
if (replier_dialog_id.get_type() == DialogType::Channel) {
|
|
|
|
// the replier_dialog_id is never min, because it is the sender of a message
|
2022-11-21 16:35:28 +01:00
|
|
|
for (auto it = replier_min_channels_.begin(); it != replier_min_channels_.end(); ++it) {
|
2021-12-22 18:15:18 +01:00
|
|
|
if (it->first == replier_dialog_id.get_channel_id()) {
|
2022-11-21 16:35:28 +01:00
|
|
|
replier_min_channels_.erase(it);
|
2021-12-22 18:15:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
td::remove(recent_replier_dialog_ids_, replier_dialog_id);
|
2020-10-31 11:55:53 +01:00
|
|
|
if (diff > 0) {
|
2022-11-21 16:35:28 +01:00
|
|
|
recent_replier_dialog_ids_.insert(recent_replier_dialog_ids_.begin(), replier_dialog_id);
|
|
|
|
if (recent_replier_dialog_ids_.size() > MAX_RECENT_REPLIERS) {
|
|
|
|
recent_replier_dialog_ids_.pop_back();
|
2020-10-31 11:55:53 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-21 16:35:28 +01:00
|
|
|
auto max_repliers = static_cast<size_t>(reply_count_);
|
|
|
|
if (recent_replier_dialog_ids_.size() > max_repliers) {
|
|
|
|
recent_replier_dialog_ids_.resize(max_repliers);
|
2020-10-31 11:55:53 +01:00
|
|
|
}
|
2020-09-11 19:12:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 18:45:45 +02:00
|
|
|
|
2022-11-21 16:35:28 +01:00
|
|
|
if (diff > 0 && reply_message_id > max_message_id_) {
|
|
|
|
max_message_id_ = reply_message_id;
|
2020-09-15 18:45:45 +02:00
|
|
|
}
|
2020-10-31 11:55:53 +01:00
|
|
|
return true;
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 18:15:18 +01:00
|
|
|
bool MessageReplyInfo::need_reget(const Td *td) const {
|
2022-11-21 16:35:28 +01:00
|
|
|
for (auto &dialog_id : recent_replier_dialog_ids_) {
|
2021-12-22 18:15:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:11:38 +01:00
|
|
|
td_api::object_ptr<td_api::messageReplyInfo> MessageReplyInfo::get_message_reply_info_object(
|
|
|
|
Td *td, MessageId dialog_last_read_inbox_message_id) const {
|
2020-10-08 20:17:24 +02:00
|
|
|
if (is_empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-17 23:07:39 +02:00
|
|
|
vector<td_api::object_ptr<td_api::MessageSender>> recent_repliers;
|
2022-11-21 16:35:28 +01:00
|
|
|
for (auto dialog_id : recent_replier_dialog_ids_) {
|
2022-01-22 21:17:41 +01:00
|
|
|
auto recent_replier = get_min_message_sender_object(td, dialog_id, "get_message_reply_info_object");
|
|
|
|
if (recent_replier != nullptr) {
|
|
|
|
recent_repliers.push_back(std::move(recent_replier));
|
2020-10-08 20:17:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-21 17:11:38 +01:00
|
|
|
auto last_read_inbox_message_id = last_read_inbox_message_id_;
|
|
|
|
if (last_read_inbox_message_id.is_valid() && last_read_inbox_message_id < dialog_last_read_inbox_message_id) {
|
|
|
|
last_read_inbox_message_id = min(dialog_last_read_inbox_message_id, max_message_id_);
|
|
|
|
}
|
2022-11-21 16:35:28 +01:00
|
|
|
return td_api::make_object<td_api::messageReplyInfo>(reply_count_, std::move(recent_repliers),
|
2022-11-21 17:11:38 +01:00
|
|
|
last_read_inbox_message_id.get(),
|
2022-11-21 16:35:28 +01:00
|
|
|
last_read_outbox_message_id_.get(), max_message_id_.get());
|
2020-10-08 20:17:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 13:07:40 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReplyInfo &reply_info) {
|
2022-11-21 16:35:28 +01:00
|
|
|
if (reply_info.is_comment_) {
|
|
|
|
return string_builder << reply_info.reply_count_ << " comments in " << reply_info.channel_id_ << " by "
|
|
|
|
<< reply_info.recent_replier_dialog_ids_ << " read up to "
|
2023-07-24 14:56:03 +02:00
|
|
|
<< reply_info.last_read_inbox_message_id_ << '/' << reply_info.last_read_outbox_message_id_
|
|
|
|
<< " with PTS " << reply_info.pts_;
|
2020-09-15 18:45:45 +02:00
|
|
|
} else {
|
2022-11-21 16:35:28 +01:00
|
|
|
return string_builder << reply_info.reply_count_ << " replies read up to " << reply_info.last_read_inbox_message_id_
|
2023-07-24 14:56:03 +02:00
|
|
|
<< "/" << reply_info.last_read_outbox_message_id_ << " with PTS " << reply_info.pts_;
|
2020-09-15 18:45:45 +02:00
|
|
|
}
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-27 12:03:50 +01:00
|
|
|
} // namespace td
|