tdlight/td/telegram/StoryViewer.cpp

231 lines
9.0 KiB
C++
Raw Normal View History

2023-08-07 15:03:10 +02:00
//
2024-01-01 01:07:21 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
2023-08-07 15:03:10 +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/StoryViewer.h"
2023-08-07 16:19:12 +02:00
#include "td/telegram/BlockListId.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/MessageSender.h"
2023-12-25 15:30:26 +01:00
#include "td/telegram/MessagesManager.h"
#include "td/telegram/StoryManager.h"
#include "td/telegram/Td.h"
2023-08-07 15:03:10 +02:00
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
namespace td {
2023-12-25 15:30:26 +01:00
StoryViewer::StoryViewer(Td *td, telegram_api::object_ptr<telegram_api::StoryView> &&story_view_ptr) {
CHECK(story_view_ptr != nullptr);
switch (story_view_ptr->get_id()) {
case telegram_api::storyView::ID: {
auto story_view = telegram_api::move_object_as<telegram_api::storyView>(story_view_ptr);
UserId user_id(story_view->user_id_);
2023-12-26 11:57:43 +01:00
if (!user_id.is_valid() || story_view->date_ <= 0) {
break;
}
type_ = Type::View;
actor_dialog_id_ = DialogId(user_id);
2023-12-26 11:57:43 +01:00
date_ = story_view->date_;
is_blocked_ = story_view->blocked_;
is_blocked_for_stories_ = story_view->blocked_my_stories_from_;
reaction_type_ = ReactionType(story_view->reaction_);
break;
}
case telegram_api::storyViewPublicForward::ID: {
auto story_view = telegram_api::move_object_as<telegram_api::storyViewPublicForward>(story_view_ptr);
auto date = MessagesManager::get_message_date(story_view->message_);
auto message_full_id = td->messages_manager_->on_get_message(std::move(story_view->message_), false, true, false,
"storyViewPublicForward");
if (!message_full_id.get_message_id().is_valid() || date <= 0) {
break;
}
type_ = Type::Forward;
actor_dialog_id_ = td->messages_manager_->get_dialog_message_sender(message_full_id);
date_ = date;
is_blocked_ = story_view->blocked_;
is_blocked_for_stories_ = story_view->blocked_my_stories_from_;
message_full_id_ = message_full_id;
break;
}
case telegram_api::storyViewPublicRepost::ID: {
auto story_view = telegram_api::move_object_as<telegram_api::storyViewPublicRepost>(story_view_ptr);
auto owner_dialog_id = DialogId(story_view->peer_id_);
if (!owner_dialog_id.is_valid()) {
break;
}
auto story_id = td->story_manager_->on_get_story(owner_dialog_id, std::move(story_view->story_));
auto date = td->story_manager_->get_story_date({owner_dialog_id, story_id});
if (date <= 0) {
break;
}
type_ = Type::Repost;
actor_dialog_id_ = owner_dialog_id;
date_ = date;
is_blocked_ = story_view->blocked_;
is_blocked_for_stories_ = story_view->blocked_my_stories_from_;
story_id_ = story_id;
break;
}
default:
UNREACHABLE();
break;
2023-12-25 15:30:26 +01:00
}
if (is_valid()) {
td->messages_manager_->on_update_dialog_is_blocked(actor_dialog_id_, is_blocked_, is_blocked_for_stories_);
}
2023-12-25 15:30:26 +01:00
}
2023-12-26 11:57:43 +01:00
StoryViewer::StoryViewer(Td *td, telegram_api::object_ptr<telegram_api::StoryReaction> &&story_reaction_ptr) {
CHECK(story_reaction_ptr != nullptr);
switch (story_reaction_ptr->get_id()) {
case telegram_api::storyReaction::ID: {
auto story_reaction = telegram_api::move_object_as<telegram_api::storyReaction>(story_reaction_ptr);
DialogId actor_dialog_id(story_reaction->peer_id_);
if (!actor_dialog_id.is_valid() || story_reaction->date_ <= 0) {
break;
}
type_ = Type::View;
actor_dialog_id_ = actor_dialog_id;
date_ = story_reaction->date_;
reaction_type_ = ReactionType(story_reaction->reaction_);
break;
}
case telegram_api::storyReactionPublicForward::ID: {
auto story_reaction = telegram_api::move_object_as<telegram_api::storyReactionPublicForward>(story_reaction_ptr);
auto date = MessagesManager::get_message_date(story_reaction->message_);
auto message_full_id = td->messages_manager_->on_get_message(std::move(story_reaction->message_), false, true,
false, "storyReactionPublicForward");
if (!message_full_id.get_message_id().is_valid() || date <= 0) {
break;
}
type_ = Type::Forward;
actor_dialog_id_ = td->messages_manager_->get_dialog_message_sender(message_full_id);
date_ = date;
message_full_id_ = message_full_id;
break;
}
case telegram_api::storyReactionPublicRepost::ID: {
auto story_reaction = telegram_api::move_object_as<telegram_api::storyReactionPublicRepost>(story_reaction_ptr);
auto owner_dialog_id = DialogId(story_reaction->peer_id_);
if (!owner_dialog_id.is_valid()) {
break;
}
auto story_id = td->story_manager_->on_get_story(owner_dialog_id, std::move(story_reaction->story_));
auto date = td->story_manager_->get_story_date({owner_dialog_id, story_id});
if (date <= 0) {
break;
}
type_ = Type::Repost;
actor_dialog_id_ = owner_dialog_id;
date_ = date;
story_id_ = story_id;
break;
}
default:
UNREACHABLE();
break;
}
}
td_api::object_ptr<td_api::storyInteraction> StoryViewer::get_story_interaction_object(Td *td) const {
CHECK(is_valid());
auto type = [&]() -> td_api::object_ptr<td_api::StoryInteractionType> {
switch (type_) {
case Type::View:
return td_api::make_object<td_api::storyInteractionTypeView>(reaction_type_.get_reaction_type_object());
case Type::Forward: {
auto message_object =
td->messages_manager_->get_message_object(message_full_id_, "storyInteractionTypeForward");
CHECK(message_object != nullptr);
return td_api::make_object<td_api::storyInteractionTypeForward>(std::move(message_object));
}
case Type::Repost: {
auto story_object = td->story_manager_->get_story_object({actor_dialog_id_, story_id_});
CHECK(story_object != nullptr);
return td_api::make_object<td_api::storyInteractionTypeRepost>(std::move(story_object));
}
default:
UNREACHABLE();
return nullptr;
}
}();
2023-08-07 16:19:12 +02:00
auto block_list_id = BlockListId(is_blocked_, is_blocked_for_stories_);
return td_api::make_object<td_api::storyInteraction>(
get_message_sender_object(td, actor_dialog_id_, "storyInteraction"), date_, block_list_id.get_block_list_object(),
std::move(type));
2023-08-07 15:03:10 +02:00
}
2023-12-25 14:34:38 +01:00
bool StoryViewer::is_valid() const {
return type_ != Type::None && actor_dialog_id_.is_valid() && date_ > 0;
2023-12-25 14:34:38 +01:00
}
2023-08-07 15:03:10 +02:00
StringBuilder &operator<<(StringBuilder &string_builder, const StoryViewer &viewer) {
return string_builder << '[' << viewer.actor_dialog_id_ << " with " << viewer.reaction_type_ << " at " << viewer.date_
<< ']';
2023-08-07 15:03:10 +02:00
}
StoryViewers::StoryViewers(Td *td, int32 total_count, int32 total_forward_count, int32 total_reaction_count,
2023-12-05 10:30:12 +01:00
vector<telegram_api::object_ptr<telegram_api::StoryView>> &&story_views,
2023-08-07 18:40:15 +02:00
string &&next_offset)
2023-12-25 13:10:48 +01:00
: total_count_(total_count)
, total_forward_count_(total_forward_count)
, total_reaction_count_(total_reaction_count)
, next_offset_(std::move(next_offset)) {
2023-12-25 15:30:26 +01:00
for (auto &story_view : story_views) {
StoryViewer story_viewer(td, std::move(story_view));
if (!story_viewer.is_valid()) {
2023-12-26 11:57:43 +01:00
LOG(ERROR) << "Receive invalid story interaction";
continue;
}
story_viewers_.push_back(std::move(story_viewer));
}
}
StoryViewers::StoryViewers(Td *td, int32 total_count,
vector<telegram_api::object_ptr<telegram_api::StoryReaction>> &&story_reactions,
string &&next_offset)
: total_count_(total_count), next_offset_(std::move(next_offset)) {
for (auto &story_reaction : story_reactions) {
StoryViewer story_viewer(td, std::move(story_reaction));
if (!story_viewer.is_valid()) {
LOG(ERROR) << "Receive invalid story interaction";
2023-12-05 10:30:12 +01:00
continue;
}
2023-12-25 15:30:26 +01:00
story_viewers_.push_back(std::move(story_viewer));
2023-08-07 15:03:10 +02:00
}
}
2023-12-25 14:32:41 +01:00
vector<UserId> StoryViewers::get_viewer_user_ids() const {
vector<UserId> result;
for (const auto &story_viewer : story_viewers_) {
auto user_id = story_viewer.get_viewer_user_id();
if (user_id.is_valid()) {
result.push_back(user_id);
}
}
return result;
}
vector<DialogId> StoryViewers::get_actor_dialog_ids() const {
return transform(story_viewers_, [](auto &viewer) { return viewer.get_actor_dialog_id(); });
2023-08-07 15:03:10 +02:00
}
td_api::object_ptr<td_api::storyInteractions> StoryViewers::get_story_interactions_object(Td *td) const {
return td_api::make_object<td_api::storyInteractions>(
2023-12-25 13:10:48 +01:00
total_count_, total_forward_count_, total_reaction_count_,
2023-08-07 18:40:15 +02:00
transform(story_viewers_,
[td](const StoryViewer &story_viewer) { return story_viewer.get_story_interaction_object(td); }),
2023-08-07 18:40:15 +02:00
next_offset_);
2023-08-07 15:03:10 +02:00
}
StringBuilder &operator<<(StringBuilder &string_builder, const StoryViewers &viewers) {
return string_builder << viewers.story_viewers_;
}
} // namespace td