From 04f3432574f07f77b41b22a18b1745a17a0fd9f0 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 2 Feb 2024 16:30:11 +0300 Subject: [PATCH] Load all Saved Messages tags from database. --- td/telegram/ReactionManager.cpp | 30 ++++++++++++++++++++++++++---- td/telegram/ReactionManager.h | 3 +++ td/telegram/TdDb.cpp | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/td/telegram/ReactionManager.cpp b/td/telegram/ReactionManager.cpp index 0cca44c13..e0290d373 100644 --- a/td/telegram/ReactionManager.cpp +++ b/td/telegram/ReactionManager.cpp @@ -936,9 +936,28 @@ void ReactionManager::send_set_default_reaction_query() { ReactionType(td_->option_manager_->get_option_string("default_reaction"))); } +void ReactionManager::load_all_saved_reaction_tags_from_database() { + if (are_all_tags_loaded_from_database_ || all_tags_.is_inited_ || !G()->use_message_database()) { + return; + } + are_all_tags_loaded_from_database_ = true; + + auto value = G()->td_db()->get_sqlite_sync_pmc()->get(get_saved_messages_tags_database_key(SavedMessagesTopicId())); + if (!value.empty()) { + if (log_event_parse(all_tags_, value).is_ok()) { + send_update_saved_messages_tags(SavedMessagesTopicId(), &all_tags_, true); + } else { + LOG(ERROR) << "Failed to load all tags from database"; + all_tags_ = {}; + } + } + reget_saved_messages_tags(SavedMessagesTopicId(), Auto()); +} + ReactionManager::SavedReactionTags *ReactionManager::get_saved_reaction_tags( SavedMessagesTopicId saved_messages_topic_id) { if (saved_messages_topic_id == SavedMessagesTopicId()) { + load_all_saved_reaction_tags_from_database(); return &all_tags_; } auto it = topic_tags_.find(saved_messages_topic_id); @@ -951,6 +970,7 @@ ReactionManager::SavedReactionTags *ReactionManager::get_saved_reaction_tags( ReactionManager::SavedReactionTags *ReactionManager::add_saved_reaction_tags( SavedMessagesTopicId saved_messages_topic_id) { if (saved_messages_topic_id == SavedMessagesTopicId()) { + load_all_saved_reaction_tags_from_database(); return &all_tags_; } auto &tags = topic_tags_[saved_messages_topic_id]; @@ -1092,8 +1112,9 @@ void ReactionManager::update_saved_messages_tags(SavedMessagesTopicId saved_mess if (old_tags == new_tags) { return; } - if (all_tags_.update_saved_messages_tags(old_tags, new_tags)) { - send_update_saved_messages_tags(SavedMessagesTopicId(), &all_tags_); + auto *all_tags = get_saved_reaction_tags(SavedMessagesTopicId()); + if (all_tags->update_saved_messages_tags(old_tags, new_tags)) { + send_update_saved_messages_tags(SavedMessagesTopicId(), all_tags); } if (saved_messages_topic_id != SavedMessagesTopicId()) { auto tags = get_saved_reaction_tags(saved_messages_topic_id); @@ -1109,8 +1130,9 @@ void ReactionManager::set_saved_messages_tag_title(ReactionType reaction_type, s } title = clean_name(title, MAX_TAG_TITLE_LENGTH); - if (all_tags_.set_tag_title(reaction_type, title)) { - send_update_saved_messages_tags(SavedMessagesTopicId(), &all_tags_); + auto *all_tags = get_saved_reaction_tags(SavedMessagesTopicId()); + if (all_tags->set_tag_title(reaction_type, title)) { + send_update_saved_messages_tags(SavedMessagesTopicId(), all_tags); } auto query_promise = diff --git a/td/telegram/ReactionManager.h b/td/telegram/ReactionManager.h index d77a96e58..ad2982432 100644 --- a/td/telegram/ReactionManager.h +++ b/td/telegram/ReactionManager.h @@ -224,6 +224,8 @@ class ReactionManager final : public Actor { string get_saved_messages_tags_database_key(SavedMessagesTopicId saved_messages_topic_id); + void load_all_saved_reaction_tags_from_database(); + td_api::object_ptr get_update_saved_messages_tags_object( SavedMessagesTopicId saved_messages_topic_id, const SavedReactionTags *tags) const; @@ -235,6 +237,7 @@ class ReactionManager final : public Actor { bool is_inited_ = false; bool are_reactions_loaded_from_database_ = false; + bool are_all_tags_loaded_from_database_ = false; vector>>> pending_get_emoji_reaction_queries_; diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index b18fda8f2..9ab27343f 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -441,6 +441,7 @@ Status TdDb::init_sqlite(const Parameters ¶meters, const DbKey &key, const D binlog_pmc.erase("fetched_marks_as_unread"); binlog_pmc.erase_by_prefix("public_channels"); binlog_pmc.erase("channels_to_send_stories"); + binlog_pmc.erase_by_prefix("saved_messages_tags"); } if (user_version == 0) { binlog_pmc.erase("next_contacts_sync_date");