From 77379bd20f2353880fcea2d68ec14bf462c76eab Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 15 Jun 2023 19:30:28 +0300 Subject: [PATCH] Update interaction info of opened owned stories. --- td/telegram/StoryManager.cpp | 60 +++++++++++++++++++++++++++++++++++- td/telegram/StoryManager.h | 3 ++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index 0bc12fd43..2e883dc27 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -352,6 +352,32 @@ class DeleteStoriesQuery final : public Td::ResultHandler { } }; +class GetStoriesViewsQuery final : public Td::ResultHandler { + vector story_ids_; + + public: + void send(vector story_ids) { + story_ids_ = std::move(story_ids); + send_query(G()->net_query_creator().create( + telegram_api::stories_getStoriesViews(StoryId::get_input_story_ids(story_ids_)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(DEBUG) << "Receive result for GetStoriesViewsQuery: " << to_string(ptr); + td_->story_manager_->on_get_story_views(story_ids_, std::move(ptr)); + } + + void on_error(Status status) final { + LOG(INFO) << "Failed to get views of " << story_ids_ << ": " << status; + } +}; + class StoryManager::SendStoryQuery final : public Td::ResultHandler { FileId file_id_; unique_ptr pending_story_; @@ -759,7 +785,11 @@ void StoryManager::close_story(DialogId owner_dialog_id, StoryId story_id, Promi } void StoryManager::on_owned_story_opened(StoryFullId story_full_id) { - // TODO reget story view counter + CHECK(is_story_owned(story_full_id.get_dialog_id())); + auto story_id = story_full_id.get_story_id(); + if (story_id.is_server()) { + td_->create_handler()->send({story_id}); + } } void StoryManager::increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views) { @@ -1387,6 +1417,34 @@ bool StoryManager::on_update_read_stories(DialogId owner_dialog_id, StoryId max_ return false; } +void StoryManager::on_get_story_views(const vector &story_ids, + telegram_api::object_ptr &&story_views) { + td_->contacts_manager_->on_get_users(std::move(story_views->users_), "on_get_story_views"); + if (story_ids.size() != story_views->views_.size()) { + LOG(ERROR) << "Receive invalid views for " << story_ids << ": " << to_string(story_views); + return; + } + DialogId owner_dialog_id(td_->contacts_manager_->get_my_id()); + for (size_t i = 0; i < story_ids.size(); i++) { + auto story_id = story_ids[i]; + CHECK(story_id.is_server()); + + StoryFullId story_full_id{owner_dialog_id, story_id}; + Story *story = get_story_editable(story_full_id); + if (story == nullptr || story->content_ == nullptr) { + continue; + } + + bool is_changed = false; + StoryInteractionInfo interaction_info(td_, std::move(story_views->views_[i])); + CHECK(!interaction_info.is_empty()); + if (story->interaction_info_ != interaction_info) { + story->interaction_info_ = std::move(interaction_info); + on_story_changed(story_full_id, story, is_changed, false); + } + } +} + FileSourceId StoryManager::get_story_file_source_id(StoryFullId story_full_id) { if (td_->auth_manager_->is_bot()) { return FileSourceId(); diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index c7b80a937..ce9f463ea 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -129,6 +129,9 @@ class StoryManager final : public Actor { bool on_update_read_stories(DialogId owner_dialog_id, StoryId max_read_story_id); + void on_get_story_views(const vector &story_ids, + telegram_api::object_ptr &&story_views); + bool have_story(StoryFullId story_full_id) const; bool have_story_force(StoryFullId story_full_id) const;