Save information about forum topics to database.

This commit is contained in:
levlam 2022-11-16 17:26:51 +03:00
parent 65a2f1c025
commit 94f999435f
5 changed files with 112 additions and 4 deletions

View File

@ -759,6 +759,7 @@ set(TDLIB_SOURCE
td/telegram/files/FileSourceId.hpp td/telegram/files/FileSourceId.hpp
td/telegram/ForumTopicEditedData.hpp td/telegram/ForumTopicEditedData.hpp
td/telegram/ForumTopicIcon.hpp td/telegram/ForumTopicIcon.hpp
td/telegram/ForumTopicInfo.hpp
td/telegram/Game.hpp td/telegram/Game.hpp
td/telegram/InputInvoice.hpp td/telegram/InputInvoice.hpp
td/telegram/InputMessageText.hpp td/telegram/InputMessageText.hpp

View File

@ -73,6 +73,12 @@ class ForumTopicInfo {
bool apply_edited_data(const ForumTopicEditedData &edited_data); bool apply_edited_data(const ForumTopicEditedData &edited_data);
td_api::object_ptr<td_api::forumTopicInfo> get_forum_topic_info_object(Td *td) const; td_api::object_ptr<td_api::forumTopicInfo> get_forum_topic_info_object(Td *td) const;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
}; };
bool operator==(const ForumTopicInfo &lhs, const ForumTopicInfo &rhs); bool operator==(const ForumTopicInfo &lhs, const ForumTopicInfo &rhs);

View File

@ -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 <class StorerT>
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 <class ParserT>
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

View File

@ -12,11 +12,14 @@
#include "td/telegram/ContactsManager.h" #include "td/telegram/ContactsManager.h"
#include "td/telegram/CustomEmojiId.h" #include "td/telegram/CustomEmojiId.h"
#include "td/telegram/ForumTopicIcon.h" #include "td/telegram/ForumTopicIcon.h"
#include "td/telegram/ForumTopicInfo.hpp"
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
#include "td/telegram/MessagesManager.h" #include "td/telegram/MessagesManager.h"
#include "td/telegram/MessageThreadDb.h"
#include "td/telegram/misc.h" #include "td/telegram/misc.h"
#include "td/telegram/ServerMessageId.h" #include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/telegram/UpdatesManager.h" #include "td/telegram/UpdatesManager.h"
@ -170,6 +173,32 @@ class EditForumTopicQuery final : public Td::ResultHandler {
} }
}; };
template <class StorerT>
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 <class ParserT>
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)) { 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_ptr<Fo
if (topic->info_ == nullptr) { if (topic->info_ == nullptr) {
topic->info_ = std::move(forum_topic_info); topic->info_ = std::move(forum_topic_info);
send_update_forum_topic_info(dialog_id, topic->info_.get()); 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_)); 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, void ForumTopicManager::on_forum_topic_edited(DialogId dialog_id, MessageId top_thread_message_id,
const ForumTopicEditedData &edited_data) { const ForumTopicEditedData &edited_data) {
auto topic_info = get_topic_info(dialog_id, top_thread_message_id); auto topic = get_topic(dialog_id, top_thread_message_id);
if (topic_info == nullptr) { if (topic == nullptr || topic->info_ == nullptr) {
return; return;
} }
if (topic_info->apply_edited_data(edited_data)) { if (topic->info_->apply_edited_data(edited_data)) {
send_update_forum_topic_info(dialog_id, topic_info); 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) { if (topic->info_ == nullptr || *topic->info_ != *forum_topic_info) {
topic->info_ = std::move(forum_topic_info); topic->info_ = std::move(forum_topic_info);
send_update_forum_topic_info(dialog_id, topic->info_.get()); 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)); 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) { 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()) { 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 " LOG(ERROR) << "Change by " << diff << " number of loaded messages in thread of " << top_thread_message_id << " in "

View File

@ -62,6 +62,14 @@ class ForumTopicManager final : public Actor {
struct Topic { struct Topic {
unique_ptr<ForumTopicInfo> info_; unique_ptr<ForumTopicInfo> info_;
int32 message_count_ = 0; int32 message_count_ = 0;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
int32 MAGIC = 0x1fac3901;
}; };
struct DialogTopics { 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 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_; Td *td_;
ActorShared<> parent_; ActorShared<> parent_;