2023-05-18 17:41:07 +02: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/StoryInteractionInfo.h"
|
|
|
|
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
2023-07-03 19:03:26 +02:00
|
|
|
#include "td/telegram/Dependencies.h"
|
2023-05-18 17:41:07 +02:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
2023-07-02 18:10:40 +02:00
|
|
|
#include "td/utils/algorithm.h"
|
2023-05-18 17:41:07 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
StoryInteractionInfo::StoryInteractionInfo(Td *td, telegram_api::object_ptr<telegram_api::storyViews> &&story_views) {
|
|
|
|
if (story_views == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &viewer_id : story_views->recent_viewers_) {
|
|
|
|
UserId user_id(viewer_id);
|
|
|
|
if (user_id.is_valid() && td->contacts_manager_->have_min_user(user_id)) {
|
|
|
|
if (recent_viewer_user_ids_.size() == MAX_RECENT_VIEWERS) {
|
|
|
|
LOG(ERROR) << "Receive too many recent story viewers: " << story_views->recent_viewers_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
recent_viewer_user_ids_.push_back(user_id);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive " << user_id << " as recent viewer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view_count_ = story_views->views_count_;
|
|
|
|
if (view_count_ < 0) {
|
|
|
|
LOG(ERROR) << "Receive " << view_count_ << " story views";
|
|
|
|
view_count_ = 0;
|
|
|
|
}
|
2023-08-04 14:32:24 +02:00
|
|
|
reaction_count_ = story_views->reactions_count_;
|
|
|
|
if (reaction_count_ < 0) {
|
|
|
|
LOG(ERROR) << "Receive " << reaction_count_ << " story reactions";
|
|
|
|
reaction_count_ = 0;
|
|
|
|
}
|
2023-08-13 23:29:57 +02:00
|
|
|
has_viewers_ = story_views->has_viewers_;
|
2023-05-18 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2023-07-03 19:03:26 +02:00
|
|
|
void StoryInteractionInfo::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
for (auto user_id : recent_viewer_user_ids_) {
|
|
|
|
dependencies.add(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 16:19:12 +02:00
|
|
|
bool StoryInteractionInfo::set_recent_viewer_user_ids(vector<UserId> &&user_ids) {
|
2023-08-13 23:40:46 +02:00
|
|
|
if (recent_viewer_user_ids_.empty() && view_count_ > 0) {
|
|
|
|
// don't update recent viewers for stories with expired viewers
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-20 15:12:12 +02:00
|
|
|
if (user_ids.size() > MAX_RECENT_VIEWERS) {
|
|
|
|
user_ids.resize(MAX_RECENT_VIEWERS);
|
|
|
|
}
|
2023-08-07 16:19:12 +02:00
|
|
|
if (recent_viewer_user_ids_ != user_ids) {
|
|
|
|
recent_viewer_user_ids_ = std::move(user_ids);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-06-20 15:12:12 +02:00
|
|
|
}
|
|
|
|
|
2023-07-02 18:10:40 +02:00
|
|
|
bool StoryInteractionInfo::definitely_has_no_user(UserId user_id) const {
|
2023-08-13 23:29:57 +02:00
|
|
|
return !is_empty() && view_count_ == static_cast<int32>(recent_viewer_user_ids_.size()) &&
|
2023-07-06 19:55:02 +02:00
|
|
|
!contains(recent_viewer_user_ids_, user_id);
|
2023-07-02 18:10:40 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 12:41:15 +02:00
|
|
|
td_api::object_ptr<td_api::storyInteractionInfo> StoryInteractionInfo::get_story_interaction_info_object(Td *td) const {
|
|
|
|
if (is_empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::storyInteractionInfo>(
|
2023-08-04 14:32:24 +02:00
|
|
|
view_count_, reaction_count_,
|
2023-05-19 12:41:15 +02:00
|
|
|
td->contacts_manager_->get_user_ids_object(recent_viewer_user_ids_, "get_story_interaction_info_object"));
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:41:07 +02:00
|
|
|
bool operator==(const StoryInteractionInfo &lhs, const StoryInteractionInfo &rhs) {
|
2023-08-04 14:32:24 +02:00
|
|
|
return lhs.recent_viewer_user_ids_ == rhs.recent_viewer_user_ids_ && lhs.view_count_ == rhs.view_count_ &&
|
2023-08-13 23:29:57 +02:00
|
|
|
lhs.reaction_count_ == rhs.reaction_count_ && lhs.has_viewers_ == rhs.has_viewers_;
|
2023-05-18 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const StoryInteractionInfo &info) {
|
2023-08-04 14:32:24 +02:00
|
|
|
return string_builder << info.view_count_ << " views with " << info.reaction_count_ << " reactions by "
|
|
|
|
<< info.recent_viewer_user_ids_;
|
2023-05-18 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|