From 94f999435f85d860fb68a5b67f7a80ef078ac9be Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 16 Nov 2022 17:26:51 +0300 Subject: [PATCH] Save information about forum topics to database. --- CMakeLists.txt | 1 + td/telegram/ForumTopicInfo.h | 6 ++++ td/telegram/ForumTopicInfo.hpp | 44 +++++++++++++++++++++++++ td/telegram/ForumTopicManager.cpp | 55 ++++++++++++++++++++++++++++--- td/telegram/ForumTopicManager.h | 10 ++++++ 5 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 td/telegram/ForumTopicInfo.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b683c20b..6f3676c68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -759,6 +759,7 @@ set(TDLIB_SOURCE td/telegram/files/FileSourceId.hpp td/telegram/ForumTopicEditedData.hpp td/telegram/ForumTopicIcon.hpp + td/telegram/ForumTopicInfo.hpp td/telegram/Game.hpp td/telegram/InputInvoice.hpp td/telegram/InputMessageText.hpp diff --git a/td/telegram/ForumTopicInfo.h b/td/telegram/ForumTopicInfo.h index 507ec9c38..788df7e6c 100644 --- a/td/telegram/ForumTopicInfo.h +++ b/td/telegram/ForumTopicInfo.h @@ -73,6 +73,12 @@ class ForumTopicInfo { bool apply_edited_data(const ForumTopicEditedData &edited_data); td_api::object_ptr get_forum_topic_info_object(Td *td) const; + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); }; bool operator==(const ForumTopicInfo &lhs, const ForumTopicInfo &rhs); diff --git a/td/telegram/ForumTopicInfo.hpp b/td/telegram/ForumTopicInfo.hpp new file mode 100644 index 000000000..d128cf1a2 --- /dev/null +++ b/td/telegram/ForumTopicInfo.hpp @@ -0,0 +1,44 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// 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) +// +#pragma once + +#include "td/telegram/ForumTopicInfo.h" + +#include "td/telegram/ForumTopicIcon.hpp" + +#include "td/utils/common.h" +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void ForumTopicInfo::store(StorerT &storer) const { + BEGIN_STORE_FLAGS(); + STORE_FLAG(is_outgoing_); + STORE_FLAG(is_closed_); + END_STORE_FLAGS(); + td::store(top_thread_message_id_, storer); + td::store(title_, storer); + td::store(icon_, storer); + td::store(creation_date_, storer); + td::store(creator_dialog_id_, storer); +} + +template +void ForumTopicInfo::parse(ParserT &parser) { + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(is_outgoing_); + PARSE_FLAG(is_closed_); + END_PARSE_FLAGS(); + td::parse(top_thread_message_id_, parser); + td::parse(title_, parser); + td::parse(icon_, parser); + td::parse(creation_date_, parser); + td::parse(creator_dialog_id_, parser); +} + +} // namespace td diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index 26a4f5491..cd49120fa 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -12,11 +12,14 @@ #include "td/telegram/ContactsManager.h" #include "td/telegram/CustomEmojiId.h" #include "td/telegram/ForumTopicIcon.h" +#include "td/telegram/ForumTopicInfo.hpp" #include "td/telegram/Global.h" #include "td/telegram/MessagesManager.h" +#include "td/telegram/MessageThreadDb.h" #include "td/telegram/misc.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/Td.h" +#include "td/telegram/TdDb.h" #include "td/telegram/telegram_api.h" #include "td/telegram/UpdatesManager.h" @@ -170,6 +173,32 @@ class EditForumTopicQuery final : public Td::ResultHandler { } }; +template +void ForumTopicManager::Topic::store(StorerT &storer) const { + CHECK(info_ != nullptr); + using td::store; + + store(MAGIC, storer); + BEGIN_STORE_FLAGS(); + END_STORE_FLAGS(); + store(info_, storer); +} + +template +void ForumTopicManager::Topic::parse(ParserT &parser) { + CHECK(info_ != nullptr); + using td::parse; + + int32 magic; + parse(magic, parser); + if (magic != MAGIC) { + return parser.set_error("Invalid magic"); + } + BEGIN_PARSE_FLAGS(); + END_PARSE_FLAGS(); + parse(info_, parser); +} + ForumTopicManager::ForumTopicManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -222,6 +251,7 @@ void ForumTopicManager::on_forum_topic_created(DialogId dialog_id, unique_ptrinfo_ == nullptr) { topic->info_ = std::move(forum_topic_info); send_update_forum_topic_info(dialog_id, topic->info_.get()); + save_topic_to_database(dialog_id, topic); } promise.set_value(topic->info_->get_forum_topic_info_object(td_)); } @@ -291,12 +321,13 @@ void ForumTopicManager::delete_forum_topic(DialogId dialog_id, MessageId top_thr void ForumTopicManager::on_forum_topic_edited(DialogId dialog_id, MessageId top_thread_message_id, const ForumTopicEditedData &edited_data) { - auto topic_info = get_topic_info(dialog_id, top_thread_message_id); - if (topic_info == nullptr) { + auto topic = get_topic(dialog_id, top_thread_message_id); + if (topic == nullptr || topic->info_ == nullptr) { return; } - if (topic_info->apply_edited_data(edited_data)) { - send_update_forum_topic_info(dialog_id, topic_info); + if (topic->info_->apply_edited_data(edited_data)) { + send_update_forum_topic_info(dialog_id, topic->info_.get()); + save_topic_to_database(dialog_id, topic); } } @@ -323,6 +354,7 @@ void ForumTopicManager::on_get_forum_topics(DialogId dialog_id, if (topic->info_ == nullptr || *topic->info_ != *forum_topic_info) { topic->info_ = std::move(forum_topic_info); send_update_forum_topic_info(dialog_id, topic->info_.get()); + save_topic_to_database(dialog_id, topic); } } } @@ -413,6 +445,21 @@ void ForumTopicManager::send_update_forum_topic_info(DialogId dialog_id, const F send_closure(G()->td(), &Td::send_update, get_update_forum_topic_info(dialog_id, topic_info)); } +void ForumTopicManager::save_topic_to_database(DialogId dialog_id, const Topic *topic) { + if (topic->info_ == nullptr) { + return; + } + + auto message_thread_db = G()->td_db()->get_message_thread_db_async(); + if (message_thread_db == nullptr) { + return; + } + + auto top_thread_message_id = topic->info_->get_top_thread_message_id(); + LOG(INFO) << "Save topic of " << top_thread_message_id << " in " << dialog_id << " to database"; + message_thread_db->add_message_thread(dialog_id, top_thread_message_id, 0, log_event_store(*topic), Auto()); +} + void ForumTopicManager::on_topic_message_count_changed(DialogId dialog_id, MessageId top_thread_message_id, int diff) { if (!can_be_forum(dialog_id) || !top_thread_message_id.is_valid()) { LOG(ERROR) << "Change by " << diff << " number of loaded messages in thread of " << top_thread_message_id << " in " diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index a70563e4a..219156c26 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -62,6 +62,14 @@ class ForumTopicManager final : public Actor { struct Topic { unique_ptr info_; int32 message_count_ = 0; + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); + + int32 MAGIC = 0x1fac3901; }; struct DialogTopics { @@ -93,6 +101,8 @@ class ForumTopicManager final : public Actor { void send_update_forum_topic_info(DialogId dialog_id, const ForumTopicInfo *topic_info) const; + void save_topic_to_database(DialogId dialog_id, const Topic *topic); + Td *td_; ActorShared<> parent_;