From fe2e34d8bc66fe7d913b41a351e2e42febd00188 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 14 Jul 2023 17:42:59 +0300 Subject: [PATCH] Save active stories to database. --- td/telegram/StoryManager.cpp | 59 ++++++++++++++++++++++++++++++++++++ td/telegram/StoryManager.h | 8 +++++ 2 files changed, 67 insertions(+) diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index 44ea2012b..9c14a7d29 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -835,6 +835,43 @@ void StoryManager::PendingStory::parse(ParserT &parser) { parse(story_, parser); } +template +void StoryManager::ActiveStories::store(StorerT &storer) const { + using td::store; + CHECK(!story_ids_.empty()); + bool has_max_read_story_id = max_read_story_id_ != StoryId(); + bool has_order = private_order_ != 0; + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_max_read_story_id); + STORE_FLAG(has_order); + END_STORE_FLAGS(); + store(story_ids_, storer); + if (has_max_read_story_id) { + store(max_read_story_id_, storer); + } + if (has_order) { + store(private_order_, storer); + } +} + +template +void StoryManager::ActiveStories::parse(ParserT &parser) { + using td::parse; + bool has_max_read_story_id; + bool has_order; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_max_read_story_id); + PARSE_FLAG(has_order); + END_PARSE_FLAGS(); + parse(story_ids_, parser); + if (has_max_read_story_id) { + parse(max_read_story_id_, parser); + } + if (has_order) { + parse(private_order_, parser); + } +} + StoryManager::StoryManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { upload_media_callback_ = std::make_shared(); @@ -2572,6 +2609,7 @@ void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId ma update_story_list_sent_total_count(active_stories->story_list_id_); } active_stories_.erase(owner_dialog_id); + save_active_stories(owner_dialog_id, nullptr); send_update_chat_active_stories(owner_dialog_id, nullptr); } else { max_read_story_ids_.erase(owner_dialog_id); @@ -2608,6 +2646,9 @@ void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId ma } else if (update_active_stories_order(owner_dialog_id, active_stories.get(), &need_save_to_database)) { send_update_chat_active_stories(owner_dialog_id, active_stories.get()); } + if (need_save_to_database) { + save_active_stories(owner_dialog_id, active_stories.get()); + } } bool StoryManager::update_active_stories_order(DialogId owner_dialog_id, ActiveStories *active_stories, @@ -2708,6 +2749,21 @@ void StoryManager::send_update_chat_active_stories(DialogId owner_dialog_id, send_closure(G()->td(), &Td::send_update, get_update_chat_active_stories(owner_dialog_id, active_stories)); } +void StoryManager::save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const { + if (!G()->use_message_database()) { + return; + } + if (active_stories == nullptr) { + LOG(INFO) << "Delete active stories of " << owner_dialog_id << " from database"; + G()->td_db()->get_story_db_async()->delete_active_stories(owner_dialog_id, Promise()); + } else { + LOG(INFO) << "Add active stories of " << owner_dialog_id << " to database"; + auto order = active_stories->story_list_id_.is_valid() ? active_stories->private_order_ : 0; + G()->td_db()->get_story_db_async()->add_active_stories(owner_dialog_id, active_stories->story_list_id_, order, + log_event_store(*active_stories), Promise()); + } +} + bool StoryManager::on_update_read_stories(DialogId owner_dialog_id, StoryId max_read_story_id) { if (owner_dialog_id == DialogId(td_->contacts_manager_->get_my_id())) { return false; @@ -2793,6 +2849,9 @@ void StoryManager::on_dialog_active_stories_order_updated(DialogId owner_dialog_ update_active_stories_order(owner_dialog_id, active_stories, &need_save_to_database)) { send_update_chat_active_stories(owner_dialog_id, active_stories); } + if (need_save_to_database) { + save_active_stories(owner_dialog_id, active_stories); + } } void StoryManager::on_get_story_views(const vector &story_ids, diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index ea635be00..7ac4c3dab 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -121,6 +121,12 @@ class StoryManager final : public Actor { StoryListId story_list_id_; int64 private_order_ = 0; int64 public_order_ = 0; + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); }; struct CachedStoryViewers { @@ -421,6 +427,8 @@ class StoryManager final : public Actor { void send_update_chat_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const; + void save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const; + void increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views); void on_increment_story_views(DialogId owner_dialog_id);