tdlight/td/telegram/MessageViewer.cpp

103 lines
3.5 KiB
C++
Raw Normal View History

2023-02-23 13:15:10 +01:00
//
// 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/MessageViewer.h"
#include "td/telegram/ContactsManager.h"
#include "td/utils/algorithm.h"
namespace td {
MessageViewer::MessageViewer(telegram_api::object_ptr<telegram_api::readParticipantDate> &&read_date)
2023-06-18 22:25:50 +02:00
: MessageViewer(UserId(read_date->user_id_), read_date->date_) {
2023-02-23 13:15:10 +01:00
}
td_api::object_ptr<td_api::messageViewer> MessageViewer::get_message_viewer_object(
ContactsManager *contacts_manager) const {
return td_api::make_object<td_api::messageViewer>(
contacts_manager->get_user_id_object(user_id_, "get_message_viewer_object"), date_);
}
2023-06-19 17:45:56 +02:00
bool operator==(const MessageViewer &lhs, const MessageViewer &rhs) {
return lhs.user_id_ == rhs.user_id_ && lhs.date_ == rhs.date_;
}
bool operator!=(const MessageViewer &lhs, const MessageViewer &rhs) {
return !(lhs == rhs);
}
2023-02-23 13:15:10 +01:00
StringBuilder &operator<<(StringBuilder &string_builder, const MessageViewer &viewer) {
return string_builder << '[' << viewer.user_id_ << " at " << viewer.date_ << ']';
}
2023-06-18 22:25:50 +02:00
MessageViewers::MessageViewers(vector<telegram_api::object_ptr<telegram_api::storyView>> &&story_views) {
for (auto &story_view : story_views) {
UserId user_id(story_view->user_id_);
if (!user_id.is_valid()) {
LOG(ERROR) << "Receive " << user_id << " as story viewer";
continue;
}
message_viewers_.emplace_back(user_id, story_view->date_);
}
}
2023-02-23 13:15:10 +01:00
MessageViewers::MessageViewers(vector<telegram_api::object_ptr<telegram_api::readParticipantDate>> &&read_dates)
: message_viewers_(
transform(std::move(read_dates), [](telegram_api::object_ptr<telegram_api::readParticipantDate> &&read_date) {
return MessageViewer(std::move(read_date));
})) {
}
2023-06-19 17:45:56 +02:00
MessageViewers MessageViewers::get_sublist(const MessageViewer &offset, int32 limit) const {
2023-06-20 14:54:47 +02:00
MessageViewers result;
2023-06-20 13:48:51 +02:00
bool found = offset.is_empty();
2023-06-19 17:45:56 +02:00
for (auto &message_viewer : message_viewers_) {
if (found) {
if (limit-- <= 0) {
break;
}
result.message_viewers_.push_back(message_viewer);
} else if (message_viewer == offset) {
found = true;
}
}
return result;
}
2023-06-20 14:54:47 +02:00
void MessageViewers::add_sublist(const MessageViewer &offset, const MessageViewers &sublist) {
if (offset.is_empty()) {
if (message_viewers_.empty()) {
message_viewers_ = sublist.message_viewers_;
} else {
auto old_viewers = std::move(message_viewers_);
for (auto &viewer : sublist.message_viewers_) {
if (viewer == old_viewers[0]) {
append(message_viewers_, old_viewers);
return;
}
message_viewers_.push_back(viewer);
}
}
} else if (!message_viewers_.empty() && message_viewers_.back() == offset) {
append(message_viewers_, sublist.message_viewers_);
}
}
2023-02-23 13:15:10 +01:00
td_api::object_ptr<td_api::messageViewers> MessageViewers::get_message_viewers_object(
ContactsManager *contacts_manager) const {
return td_api::make_object<td_api::messageViewers>(
transform(message_viewers_, [contacts_manager](const MessageViewer &message_viewer) {
return message_viewer.get_message_viewer_object(contacts_manager);
}));
}
StringBuilder &operator<<(StringBuilder &string_builder, const MessageViewers &viewers) {
return string_builder << viewers.message_viewers_;
}
} // namespace td