tdlight/td/telegram/StoryManager.cpp

229 lines
8.5 KiB
C++
Raw Normal View History

2023-05-01 21:19:16 +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/StoryManager.h"
2023-05-08 19:09:01 +02:00
#include "td/telegram/AuthManager.h"
#include "td/telegram/ContactsManager.h"
2023-05-19 16:00:33 +02:00
#include "td/telegram/files/FileManager.h"
2023-05-19 22:34:08 +02:00
#include "td/telegram/Global.h"
2023-05-08 19:09:01 +02:00
#include "td/telegram/MessageEntity.h"
2023-05-18 16:20:22 +02:00
#include "td/telegram/StoryContent.h"
#include "td/telegram/StoryContentType.h"
2023-05-08 19:09:01 +02:00
#include "td/telegram/Td.h"
2023-05-19 22:34:08 +02:00
#include "td/utils/buffer.h"
2023-05-19 22:25:52 +02:00
#include "td/utils/logging.h"
2023-05-19 22:34:08 +02:00
#include "td/utils/Status.h"
2023-05-19 22:25:52 +02:00
2023-05-01 21:19:16 +02:00
namespace td {
2023-05-19 22:34:08 +02:00
class GetStoriesByIDQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
UserId user_id_;
public:
explicit GetStoriesByIDQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(UserId user_id, vector<int32> input_story_ids) {
user_id_ = user_id;
auto r_input_user = td_->contacts_manager_->get_input_user(user_id_);
if (r_input_user.is_error()) {
return on_error(r_input_user.move_as_error());
}
send_query(G()->net_query_creator().create(
telegram_api::stories_getStoriesByID(r_input_user.move_as_ok(), std::move(input_story_ids))));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::stories_getStoriesByID>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(DEBUG) << "Receive result for GetStoriesByIDQuery: " << to_string(result);
td_->story_manager_->on_get_stories(DialogId(user_id_), std::move(result));
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
2023-05-01 21:19:16 +02:00
StoryManager::StoryManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
2023-05-18 16:20:22 +02:00
StoryManager::~StoryManager() = default;
2023-05-01 21:19:16 +02:00
void StoryManager::tear_down() {
parent_.reset();
}
2023-05-08 19:09:01 +02:00
bool StoryManager::is_local_story_id(StoryId story_id) {
return story_id.get() < 0;
}
2023-05-19 12:41:15 +02:00
bool StoryManager::is_story_owned(DialogId owner_dialog_id) const {
return owner_dialog_id == DialogId(td_->contacts_manager_->get_my_id());
}
2023-05-18 18:57:50 +02:00
const StoryManager::Story *StoryManager::get_story(StoryFullId story_full_id) const {
return stories_.get_pointer(story_full_id);
2023-05-08 19:09:01 +02:00
}
2023-05-18 18:57:50 +02:00
StoryManager::Story *StoryManager::get_story_editable(StoryFullId story_full_id) {
return stories_.get_pointer(story_full_id);
2023-05-08 19:09:01 +02:00
}
2023-05-19 12:41:15 +02:00
td_api::object_ptr<td_api::story> StoryManager::get_story_object(StoryFullId story_full_id) const {
return get_story_object(story_full_id, get_story(story_full_id));
}
td_api::object_ptr<td_api::story> StoryManager::get_story_object(StoryFullId story_full_id, const Story *story) const {
if (story == nullptr) {
return nullptr;
}
auto dialog_id = story_full_id.get_dialog_id();
bool is_owned = is_story_owned(dialog_id);
if (!is_owned && !story->is_pinned_ && G()->unix_time() >= story->expire_date_) {
return nullptr;
}
td_api::object_ptr<td_api::userPrivacySettingRules> privacy_rules;
if (is_owned) {
privacy_rules = story->privacy_rules_.get_user_privacy_setting_rules_object(td_);
}
CHECK(dialog_id.get_type() == DialogType::User);
return td_api::make_object<td_api::story>(
story_full_id.get_story_id().get(),
td_->contacts_manager_->get_user_id_object(dialog_id.get_user_id(), "get_story_object"), story->date_,
story->is_pinned_, story->interaction_info_.get_story_interaction_info_object(td_), std::move(privacy_rules),
story->is_public_, story->is_for_close_friends_, get_story_content_object(td_, story->content_.get()),
get_formatted_text_object(story->caption_, true, -1));
}
2023-05-19 16:00:33 +02:00
vector<FileId> StoryManager::get_story_file_ids(const Story *story) const {
CHECK(story != nullptr);
return get_story_content_file_ids(td_, story->content_.get());
}
void StoryManager::delete_story_files(const Story *story) const {
for (auto file_id : get_story_file_ids(story)) {
send_closure(G()->file_manager(), &FileManager::delete_file, file_id, Promise<Unit>(), "delete_story_files");
}
}
2023-05-08 19:09:01 +02:00
StoryId StoryManager::on_get_story(DialogId owner_dialog_id,
telegram_api::object_ptr<telegram_api::storyItem> &&story_item) {
CHECK(story_item != nullptr);
StoryId story_id(story_item->id_);
if (!story_id.is_valid() || is_local_story_id(story_id)) {
2023-05-19 22:25:52 +02:00
LOG(ERROR) << "Receive " << to_string(story_item);
2023-05-08 19:09:01 +02:00
return StoryId();
}
2023-05-18 18:57:50 +02:00
StoryFullId story_full_id{owner_dialog_id, story_id};
auto story = get_story_editable(story_full_id);
2023-05-08 19:09:01 +02:00
bool is_changed = false;
bool need_save_to_database = false;
if (story == nullptr) {
auto s = make_unique<Story>();
story = s.get();
2023-05-18 18:57:50 +02:00
stories_.set(story_full_id, std::move(s));
2023-05-08 19:09:01 +02:00
}
CHECK(story != nullptr);
bool is_bot = td_->auth_manager_->is_bot();
2023-05-18 16:20:22 +02:00
auto caption =
2023-05-08 19:09:01 +02:00
get_message_text(td_->contacts_manager_.get(), std::move(story_item->caption_), std::move(story_item->entities_),
true, is_bot, story_item->date_, false, "on_get_story");
2023-05-18 16:20:22 +02:00
auto content = get_story_content(td_, std::move(story_item->media_), owner_dialog_id);
if (content == nullptr) {
2023-05-08 19:09:01 +02:00
return StoryId();
}
2023-05-18 16:20:22 +02:00
auto content_type = content->get_type();
2023-05-18 17:41:07 +02:00
if (story->content_ == nullptr || story->content_->get_type() != content_type) {
story->content_ = std::move(content);
is_changed = true;
} else {
merge_story_contents(td_, story->content_.get(), content.get(), owner_dialog_id, false, need_save_to_database,
is_changed);
2023-05-19 22:34:08 +02:00
story->content_ = std::move(content);
// story->last_edit_pts_ = 0;
2023-05-08 19:09:01 +02:00
}
2023-05-18 17:41:07 +02:00
auto privacy_rules = UserPrivacySettingRules::get_user_privacy_setting_rules(td_, std::move(story_item->privacy_));
auto interaction_info = StoryInteractionInfo(td_, std::move(story_item->views_));
2023-05-08 19:09:01 +02:00
if (story->is_pinned_ != story_item->pinned_ || story->is_public_ != story_item->public_ ||
story->is_for_close_friends_ != story_item->close_friends_ || story->date_ != story_item->date_ ||
story->expire_date_ != story_item->expire_date_ || !(story->privacy_rules_ == privacy_rules) ||
2023-05-18 17:41:07 +02:00
story->interaction_info_ != interaction_info || story->caption_ != caption) {
2023-05-08 19:09:01 +02:00
story->is_pinned_ = story_item->pinned_;
story->is_public_ = story_item->public_;
story->is_for_close_friends_ = story_item->close_friends_;
story->date_ = story_item->date_;
story->expire_date_ = story_item->expire_date_;
story->privacy_rules_ = std::move(privacy_rules);
2023-05-18 17:41:07 +02:00
story->interaction_info_ = std::move(interaction_info);
2023-05-18 16:20:22 +02:00
story->caption_ = std::move(caption);
2023-05-08 19:09:01 +02:00
is_changed = true;
}
if (is_changed || need_save_to_database) {
// save_story(story, story_id);
}
if (is_changed) {
// send_closure(G()->td(), &Td::send_update, td_api::make_object<td_api::updateStory>(get_story_object(story_id, story)));
}
return story_id;
}
2023-05-19 22:25:52 +02:00
std::pair<int32, vector<StoryId>> StoryManager::on_get_stories(
DialogId owner_dialog_id, telegram_api::object_ptr<telegram_api::stories_stories> &&stories) {
td_->contacts_manager_->on_get_users(std::move(stories->users_), "on_get_stories");
vector<StoryId> story_ids;
for (auto &story : stories->stories_) {
switch (story->get_id()) {
case telegram_api::storyItemDeleted::ID:
LOG(ERROR) << "Receive storyItemDeleted";
break;
case telegram_api::storyItemSkipped::ID:
LOG(ERROR) << "Receive storyItemSkipped";
break;
case telegram_api::storyItem::ID: {
auto story_id = on_get_story(owner_dialog_id, telegram_api::move_object_as<telegram_api::storyItem>(story));
if (story_id.is_valid()) {
story_ids.push_back(story_id);
}
break;
}
default:
UNREACHABLE();
}
}
auto total_count = stories->count_;
if (total_count < static_cast<int32>(story_ids.size())) {
LOG(ERROR) << "Expected at most " << total_count << " stories, but receive " << story_ids.size();
total_count = static_cast<int32>(story_ids.size());
}
return {total_count, std::move(story_ids)};
}
2023-05-19 22:34:08 +02:00
void StoryManager::reload_story(StoryFullId story_full_id, Promise<Unit> &&promise) {
auto dialog_id = story_full_id.get_dialog_id();
if (dialog_id.get_type() != DialogType::User) {
return promise.set_error(Status::Error(400, "Unsupported story owner"));
}
auto user_id = dialog_id.get_user_id();
td_->create_handler<GetStoriesByIDQuery>(std::move(promise))->send(user_id, {story_full_if.get_story_id().get()});
}
2023-05-01 21:19:16 +02:00
} // namespace td