aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/MessageReplyInfo.cpp
blob: 831482c27fef033ee3f53c64aa4441a1464ee04a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// 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/telegram/ContactsManager.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h"

#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

namespace td {

MessageReplyInfo::MessageReplyInfo(Td *td, tl_object_ptr<telegram_api::messageReplies> &&reply_info, bool is_bot) {
  if (reply_info == nullptr) {
    return;
  }
  if (reply_info->replies_ < 0) {
    LOG(ERROR) << "Receive wrong " << to_string(reply_info);
    return;
  }
  if (is_bot || reply_info->channel_id_ == 777) {
    is_dropped_ = true;
    return;
  }
  reply_count_ = reply_info->replies_;
  pts_ = reply_info->replies_pts_;

  is_comment_ = reply_info->comments_;

  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;
    }
  }

  if (is_comment_) {
    for (const auto &peer : reply_info->recent_repliers_) {
      DialogId dialog_id(peer);
      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) << "Receive unknown 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) << "Receive unknown replied " << replier_channel_id;
            continue;
          }
          replier_min_channels_.emplace_back(replier_channel_id, *min_channel);
        } else {
          LOG(ERROR) << "Receive unknown replied " << dialog_id;
          continue;
        }
      }
      recent_replier_dialog_ids_.push_back(dialog_id);
      if (recent_replier_dialog_ids_.size() == MAX_RECENT_REPLIERS) {
        break;
      }
    }
  }
  if ((reply_info->flags_ & telegram_api::messageReplies::MAX_ID_MASK) != 0 &&
      ServerMessageId(reply_info->max_id_).is_valid()) {
    max_message_id_ = MessageId(ServerMessageId(reply_info->max_id_));
  }
  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_));
  }
  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_;
  }
  LOG(DEBUG) << "Parsed " << oneline(to_string(reply_info)) << " to " << *this;
}

bool MessageReplyInfo::need_update_to(const MessageReplyInfo &other) const {
  if (other.is_empty() && !is_empty()) {
    // ignore updates to empty reply info, because we will hide the info ourselves
    // return true;
  }
  if (other.pts_ < pts_ && !other.was_dropped()) {
    return false;
  }
  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_;
}

bool MessageReplyInfo::update_max_message_ids(const MessageReplyInfo &other) {
  return update_max_message_ids(other.max_message_id_, other.last_read_inbox_message_id_,
                                other.last_read_outbox_message_id_);
}

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) {
  bool result = false;
  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;
    result = true;
  }
  if (other_max_message_id.is_valid() ||
      (!other_last_read_inbox_message_id.is_valid() && !other_last_read_outbox_message_id.is_valid())) {
    if (other_max_message_id < last_read_inbox_message_id_) {
      other_max_message_id = last_read_inbox_message_id_;
    }
    if (other_max_message_id < last_read_outbox_message_id_) {
      other_max_message_id = last_read_outbox_message_id_;
    }
    if (other_max_message_id != max_message_id_) {
      max_message_id_ = other_max_message_id;
      result = true;
    }
  }
  return result;
}

bool MessageReplyInfo::add_reply(DialogId replier_dialog_id, MessageId reply_message_id, int diff) {
  CHECK(!is_empty());
  CHECK(diff == +1 || diff == -1);

  if (diff == -1 && reply_count_ == 0) {
    return false;
  }

  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);
      if (recent_replier_dialog_ids_.size() > MAX_RECENT_REPLIERS) {
        recent_replier_dialog_ids_.pop_back();
      }
    } else {
      auto max_repliers = static_cast<size_t>(reply_count_);
      if (recent_replier_dialog_ids_.size() > max_repliers) {
        recent_replier_dialog_ids_.resize(max_repliers);
      }
    }
  }

  if (diff > 0 && reply_message_id > max_message_id_) {
    max_message_id_ = reply_message_id;
  }
  return true;
}

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, MessageId dialog_last_read_inbox_message_id) const {
  if (is_empty()) {
    return nullptr;
  }

  vector<td_api::object_ptr<td_api::MessageSender>> recent_repliers;
  for (auto dialog_id : recent_replier_dialog_ids_) {
    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));
    }
  }
  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_);
  }
  return td_api::make_object<td_api::messageReplyInfo>(reply_count_, std::move(recent_repliers),
                                                       last_read_inbox_message_id.get(),
                                                       last_read_outbox_message_id_.get(), max_message_id_.get());
}

StringBuilder &operator<<(StringBuilder &string_builder, const MessageReplyInfo &reply_info) {
  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 "
                          << reply_info.last_read_inbox_message_id_ << "/" << reply_info.last_read_outbox_message_id_;
  } else {
    return string_builder << reply_info.reply_count_ << " replies read up to " << reply_info.last_read_inbox_message_id_
                          << "/" << reply_info.last_read_outbox_message_id_;
  }
}

}  // namespace td