From cdaddd75b26cc5d9b7047d260b588510f37a72e5 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 2 Dec 2022 17:04:24 +0300 Subject: [PATCH] Add td_api::getForumTopic. --- td/generate/scheme/td_api.tl | 5 +- td/telegram/ForumTopic.h | 2 +- td/telegram/ForumTopicManager.cpp | 79 +++++++++++++++++++++++++++++++ td/telegram/ForumTopicManager.h | 3 ++ td/telegram/Td.cpp | 6 +++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 5 ++ 7 files changed, 100 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index e3d23b201..d9b6e39ab 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1327,7 +1327,7 @@ forumTopicInfo message_thread_id:int53 name:string icon:forumTopicIcon creation_ //@description Describes a forum topic //@info Basic information about the topic -//@last_message Last message in the topic; may be null +//@last_message Last message in the topic; may be null if unknown //@is_pinned True, if the topic is pinned in the topic list //@unread_count Number of unread messages in the topic //@last_read_inbox_message_id Identifier of the last read incoming message @@ -5337,6 +5337,9 @@ createForumTopic chat_id:int53 name:string icon:forumTopicIcon = ForumTopicInfo; //@icon_custom_emoji_id Identifier of the new custom emoji for topic icon; pass 0 to remove the custom emoji. Ignored if edit_icon_custom_emoji is false. Telegram Premium users can use any custom emoji, other users can use only a custom emoji returned by getForumTopicDefaultIcons editForumTopic chat_id:int53 message_thread_id:int53 name:string edit_icon_custom_emoji:Bool icon_custom_emoji_id:int64 = Ok; +//@description Returns information about a forum topic @chat_id Identifier of the chat @message_thread_id Message thread identifier of the forum topic +getForumTopic chat_id:int53 message_thread_id:int53 = ForumTopic; + //@description Toggles whether a topic is closed in a forum supergroup chat; requires can_manage_topics administrator rights in the supergroup unless the user is creator of the topic //@chat_id Identifier of the chat //@message_thread_id Message thread identifier of the forum topic diff --git a/td/telegram/ForumTopic.h b/td/telegram/ForumTopic.h index b99fcc3e0..91ff6c783 100644 --- a/td/telegram/ForumTopic.h +++ b/td/telegram/ForumTopic.h @@ -7,6 +7,7 @@ #pragma once #include "td/telegram/DialogNotificationSettings.h" +#include "td/telegram/DraftMessage.h" #include "td/telegram/MessageId.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -15,7 +16,6 @@ namespace td { -class DraftMessage; class ForumTopicInfo; class Td; diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index ad4f023ad..e01a37576 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -11,6 +11,7 @@ #include "td/telegram/ChannelId.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/CustomEmojiId.h" +#include "td/telegram/ForumTopic.h" #include "td/telegram/ForumTopicIcon.h" #include "td/telegram/ForumTopicInfo.hpp" #include "td/telegram/Global.h" @@ -193,6 +194,72 @@ class EditForumTopicQuery final : public Td::ResultHandler { } }; +class GetForumTopicQuery final : public Td::ResultHandler { + Promise> promise_; + ChannelId channel_id_; + MessageId top_thread_message_id_; + + public: + explicit GetForumTopicQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(ChannelId channel_id, MessageId top_thread_message_id) { + channel_id_ = channel_id; + top_thread_message_id_ = top_thread_message_id; + + auto input_channel = td_->contacts_manager_->get_input_channel(channel_id); + CHECK(input_channel != nullptr); + + send_query(G()->net_query_creator().create( + telegram_api::channels_getForumTopicsByID(std::move(input_channel), + {top_thread_message_id_.get_server_message_id().get()}), + {{channel_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()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetForumTopicQuery: " << to_string(ptr); + + td_->contacts_manager_->on_get_users(std::move(ptr->users_), "GetForumTopicQuery"); + td_->contacts_manager_->on_get_chats(std::move(ptr->chats_), "GetForumTopicQuery"); + td_->messages_manager_->on_get_messages(std::move(ptr->messages_), true, false, Promise(), + "GetForumTopicQuery"); + if (ptr->topics_.size() != 1u) { + return promise_.set_value(nullptr); + } + + auto topic = std::move(ptr->topics_[0]); + switch (topic->get_id()) { + case telegram_api::forumTopicDeleted::ID: + return promise_.set_value(nullptr); + case telegram_api::forumTopic::ID: { + ForumTopicInfo forum_topic_info(topic); + ForumTopic forum_topic(td_, std::move(topic)); + if (forum_topic.is_short()) { + return promise_.set_error(Status::Error(500, "Receive short forum topic")); + } + if (forum_topic_info.get_top_thread_message_id() != top_thread_message_id_) { + return promise_.set_error(Status::Error(500, "Wrong forum topic received")); + } + return promise_.set_value(forum_topic.get_forum_topic_object(td_, forum_topic_info)); + } + default: + UNREACHABLE(); + } + } + + void on_error(Status status) final { + td_->contacts_manager_->on_get_channel_error(channel_id_, status, "GetForumTopicQuery"); + promise_.set_error(std::move(status)); + } +}; + template void ForumTopicManager::Topic::store(StorerT &storer) const { CHECK(info_ != nullptr); @@ -306,6 +373,18 @@ 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::get_forum_topic(DialogId dialog_id, MessageId top_thread_message_id, + Promise> &&promise) { + TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); + auto channel_id = dialog_id.get_channel_id(); + + if (!top_thread_message_id.is_valid() || !top_thread_message_id.is_server()) { + return promise.set_error(Status::Error(400, "Invalid message thread identifier specified")); + } + + td_->create_handler(std::move(promise))->send(channel_id, top_thread_message_id); +} + void ForumTopicManager::toggle_forum_topic_is_closed(DialogId dialog_id, MessageId top_thread_message_id, bool is_closed, Promise &&promise) { TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index 4c5ed6b6e..d73823243 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -43,6 +43,9 @@ class ForumTopicManager final : public Actor { void edit_forum_topic(DialogId dialog_id, MessageId top_thread_message_id, string &&title, bool edit_icon_custom_emoji, CustomEmojiId icon_custom_emoji_id, Promise &&promise); + void get_forum_topic(DialogId dialog_id, MessageId top_thread_message_id, + Promise> &&promise); + void toggle_forum_topic_is_closed(DialogId dialog_id, MessageId top_thread_message_id, bool is_closed, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c1d59a124..25cc75866 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5562,6 +5562,12 @@ void Td::on_request(uint64 id, td_api::editForumTopic &request) { CustomEmojiId(request.icon_custom_emoji_id_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getForumTopic &request) { + CREATE_REQUEST_PROMISE(); + forum_topic_manager_->get_forum_topic(DialogId(request.chat_id_), MessageId(request.message_thread_id_), + std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::toggleForumTopicIsClosed &request) { CREATE_OK_REQUEST_PROMISE(); forum_topic_manager_->toggle_forum_topic_is_closed(DialogId(request.chat_id_), MessageId(request.message_thread_id_), diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 614cea207..b5122a29d 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -748,6 +748,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::editForumTopic &request); + void on_request(uint64 id, const td_api::getForumTopic &request); + void on_request(uint64 id, const td_api::toggleForumTopicIsClosed &request); void on_request(uint64 id, const td_api::toggleGeneralForumTopicIsHidden &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5a03a03f9..461878061 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3882,6 +3882,11 @@ class CliClient final : public Actor { get_args(args, chat_id, message_thread_id, name, edit_icon_custom_emoji, icon_custom_emoji_id); send_request(td_api::make_object(chat_id, message_thread_id, name, edit_icon_custom_emoji, icon_custom_emoji_id)); + } else if (op == "gft") { + ChatId chat_id; + MessageThreadId message_thread_id; + get_args(args, chat_id, message_thread_id); + send_request(td_api::make_object(chat_id, message_thread_id)); } else if (op == "tftic") { ChatId chat_id; MessageThreadId message_thread_id;