2020-09-07 13:07:40 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
MessageReplyInfo::MessageReplyInfo(tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot) {
|
2020-09-12 16:10:06 +02:00
|
|
|
if (reply_info == nullptr || is_bot) {
|
2020-09-07 13:07:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply_info->replies_ < 0) {
|
|
|
|
LOG(ERROR) << "Receive wrong " << to_string(reply_info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reply_count = reply_info->replies_;
|
|
|
|
pts = reply_info->replies_pts_;
|
|
|
|
|
|
|
|
is_comment = reply_info->comments_;
|
2020-09-15 18:45:45 +02:00
|
|
|
|
2020-09-07 13:07:40 +02: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();
|
2020-09-15 16:07:34 +02:00
|
|
|
is_comment = false;
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 18:45:45 +02:00
|
|
|
|
|
|
|
if (is_comment) {
|
|
|
|
for (auto &peer : reply_info->recent_repliers_) {
|
|
|
|
DialogId dialog_id(peer);
|
|
|
|
if (dialog_id.is_valid()) {
|
|
|
|
recent_replier_dialog_ids.push_back(dialog_id);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive " << dialog_id << " as a recent replier";
|
|
|
|
}
|
|
|
|
}
|
2020-09-18 17:32:59 +02:00
|
|
|
if ((reply_info->flags_ & telegram_api::messageReplies::MAX_ID_MASK) != 0 &&
|
|
|
|
ServerMessageId(reply_info->max_id_).is_valid()) {
|
2020-09-15 18:45:45 +02:00
|
|
|
max_message_id = MessageId(ServerMessageId(reply_info->max_id_));
|
|
|
|
}
|
2020-09-18 17:32:59 +02:00
|
|
|
if ((reply_info->flags_ & telegram_api::messageReplies::READ_MAX_ID_MASK) != 0 &&
|
|
|
|
ServerMessageId(reply_info->read_max_id_).is_valid()) {
|
|
|
|
last_read_inbox_message_id = MessageId(ServerMessageId(reply_info->read_max_id_));
|
2020-09-15 18:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageReplyInfo::need_update_to(const MessageReplyInfo &other) const {
|
|
|
|
if (other.pts < pts) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-11 19:12:31 +02:00
|
|
|
return reply_count != other.reply_count || recent_replier_dialog_ids != other.recent_replier_dialog_ids;
|
|
|
|
}
|
|
|
|
|
2020-09-15 18:45:45 +02:00
|
|
|
bool MessageReplyInfo::update_max_message_ids(const MessageReplyInfo &other) {
|
2020-09-16 15:41:08 +02:00
|
|
|
if (!other.is_comment) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:32:59 +02: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-16 15:41:08 +02:00
|
|
|
if (!is_comment) {
|
2020-09-15 18:45:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = false;
|
2020-09-16 15:41:08 +02:00
|
|
|
if (other_max_message_id > max_message_id) {
|
|
|
|
max_message_id = other_max_message_id;
|
2020-09-15 18:45:45 +02:00
|
|
|
result = true;
|
|
|
|
}
|
2020-09-18 17:32:59 +02:00
|
|
|
if (other_last_read_inbox_message_id > last_read_inbox_message_id) {
|
|
|
|
last_read_inbox_message_id = other_last_read_inbox_message_id;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageReplyInfo::add_reply(DialogId replier_dialog_id, MessageId reply_message_id) {
|
2020-09-11 19:12:31 +02:00
|
|
|
CHECK(!is_empty());
|
|
|
|
|
|
|
|
reply_count++;
|
2020-09-15 18:45:45 +02:00
|
|
|
if (is_comment && replier_dialog_id.is_valid() &&
|
2020-09-11 19:12:31 +02:00
|
|
|
(recent_replier_dialog_ids.empty() || recent_replier_dialog_ids[0] != replier_dialog_id)) {
|
|
|
|
td::remove(recent_replier_dialog_ids, replier_dialog_id);
|
|
|
|
recent_replier_dialog_ids.insert(recent_replier_dialog_ids.begin(), replier_dialog_id);
|
|
|
|
if (recent_replier_dialog_ids.size() > 3) {
|
|
|
|
recent_replier_dialog_ids.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 18:45:45 +02:00
|
|
|
|
|
|
|
if (is_comment && reply_message_id > max_message_id) {
|
|
|
|
max_message_id = reply_message_id;
|
|
|
|
}
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReplyInfo &reply_info) {
|
2020-09-15 18:45:45 +02:00
|
|
|
if (reply_info.is_comment) {
|
|
|
|
return string_builder << reply_info.reply_count << " comments in " << reply_info.channel_id << " by "
|
2020-09-18 17:32:59 +02:00
|
|
|
<< reply_info.recent_replier_dialog_ids << " read up to "
|
|
|
|
<< reply_info.last_read_inbox_message_id << "/" << reply_info.last_read_outbox_message_id;
|
2020-09-15 18:45:45 +02:00
|
|
|
} else {
|
|
|
|
return string_builder << reply_info.reply_count << " replies";
|
|
|
|
}
|
2020-09-07 13:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|