From 917c9dc0137ab0b58880a8a023e3d5bf0c03f13d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 4 Sep 2023 15:34:54 +0300 Subject: [PATCH] Add ForumTopicManager::read_forum_topic_messages. --- td/telegram/ForumTopicManager.cpp | 46 +++++++++++++++++++++++++++++++ td/telegram/ForumTopicManager.h | 3 ++ td/telegram/MessagesManager.cpp | 6 +++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index 1c73036db..117d44be7 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -414,6 +414,35 @@ class GetForumTopicsQuery final : public Td::ResultHandler { } }; +class ReadForumTopicQuery final : public Td::ResultHandler { + DialogId dialog_id_; + + public: + void send(DialogId dialog_id, MessageId top_thread_message_id, MessageId max_message_id) { + dialog_id_ = dialog_id; + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chat")); + } + send_query(G()->net_query_creator().create( + telegram_api::messages_readDiscussion(std::move(input_peer), + top_thread_message_id.get_server_message_id().get(), + max_message_id.get_server_message_id().get()), + {{dialog_id}})); + } + + 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()); + } + } + + void on_error(Status status) final { + td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "ReadForumTopicQuery"); + } +}; + template void ForumTopicManager::Topic::store(StorerT &storer) const { CHECK(info_ != nullptr); @@ -538,6 +567,23 @@ void ForumTopicManager::edit_forum_topic(DialogId dialog_id, MessageId top_threa ->send(channel_id, top_thread_message_id, edit_title, new_title, edit_icon_custom_emoji, icon_custom_emoji_id); } +void ForumTopicManager::read_forum_topic_messages(DialogId dialog_id, MessageId top_thread_message_id, + MessageId last_read_inbox_message_id) { + CHECK(!td_->auth_manager_->is_bot()); + auto topic = get_topic(dialog_id, top_thread_message_id); + if (topic == nullptr || topic->topic_ == nullptr) { + return; + } + + if (topic->topic_->update_last_read_inbox_message_id(last_read_inbox_message_id, -1)) { + // TODO send updates + auto max_message_id = last_read_inbox_message_id.get_prev_server_message_id(); + LOG(INFO) << "Send read topic history request in topic of " << top_thread_message_id << " in " << dialog_id + << " up to " << max_message_id; + td_->create_handler()->send(dialog_id, top_thread_message_id, max_message_id); + } +} + void ForumTopicManager::on_update_forum_topic_unread(DialogId dialog_id, MessageId top_thread_message_id, MessageId last_message_id, MessageId last_read_inbox_message_id, MessageId last_read_outbox_message_id, int32 unread_count) { diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index 0eaf529b6..c414a35f9 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -87,6 +87,9 @@ class ForumTopicManager final : public Actor { void delete_all_dialog_topics(DialogId dialog_id); + void read_forum_topic_messages(DialogId dialog_id, MessageId top_thread_message_id, + MessageId last_read_inbox_message_id); + void on_update_forum_topic_unread(DialogId dialog_id, MessageId top_thread_message_id, MessageId last_message_id, MessageId last_read_inbox_message_id, MessageId last_read_outbox_message_id, int32 unread_count); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index b615b442f..150e12465 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -20268,7 +20268,11 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess return Status::OK(); } if (source == MessageSource::ForumTopicHistory) { - // TODO read forum topic history + if (!forum_topic_id.is_valid() || !max_message_id.is_valid()) { + return Status::OK(); + } + + td_->forum_topic_manager_->read_forum_topic_messages(dialog_id, forum_topic_id, max_message_id); return Status::OK(); }