From c9d906dcb78af3bcd13ad101fdea8090d89122bb Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 5 Dec 2022 13:22:32 +0300 Subject: [PATCH] Add ForumTopicManager::can_be_message_thread_id. --- td/telegram/ForumTopicManager.cpp | 38 ++++++++++++------------------- td/telegram/ForumTopicManager.h | 2 ++ 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index 1919b068d..b22f958ca 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -350,12 +350,9 @@ void ForumTopicManager::edit_forum_topic(DialogId dialog_id, MessageId top_threa bool edit_icon_custom_emoji, CustomEmojiId icon_custom_emoji_id, Promise &&promise) { TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); + TRY_STATUS_PROMISE(promise, can_be_message_thread_id(top_thread_message_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")); - } - if (!td_->contacts_manager_->get_channel_permissions(channel_id).can_edit_topics()) { auto topic_info = get_topic_info(dialog_id, top_thread_message_id); if (topic_info != nullptr && !topic_info->is_outgoing()) { @@ -379,24 +376,18 @@ void ForumTopicManager::edit_forum_topic(DialogId dialog_id, MessageId top_threa void ForumTopicManager::get_forum_topic(DialogId dialog_id, MessageId top_thread_message_id, Promise> &&promise) { TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); + TRY_STATUS_PROMISE(promise, can_be_message_thread_id(top_thread_message_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::get_forum_topic_link(DialogId dialog_id, MessageId top_thread_message_id, Promise &&promise) { TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); + TRY_STATUS_PROMISE(promise, can_be_message_thread_id(top_thread_message_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")); - } - SliceBuilder sb; sb << td_->option_manager_->get_option_string("t_me_url", "https://t.me/"); @@ -414,12 +405,9 @@ void ForumTopicManager::get_forum_topic_link(DialogId dialog_id, MessageId top_t 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)); + TRY_STATUS_PROMISE(promise, can_be_message_thread_id(top_thread_message_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")); - } - if (!td_->contacts_manager_->get_channel_permissions(channel_id).can_edit_topics()) { auto topic_info = get_topic_info(dialog_id, top_thread_message_id); if (topic_info != nullptr && !topic_info->is_outgoing()) { @@ -444,12 +432,9 @@ void ForumTopicManager::toggle_forum_topic_is_hidden(DialogId dialog_id, bool is void ForumTopicManager::delete_forum_topic(DialogId dialog_id, MessageId top_thread_message_id, Promise &&promise) { TRY_STATUS_PROMISE(promise, is_forum(dialog_id)); + TRY_STATUS_PROMISE(promise, can_be_message_thread_id(top_thread_message_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")); - } - if (!td_->contacts_manager_->get_channel_permissions(channel_id).can_delete_messages()) { auto topic_info = get_topic_info(dialog_id, top_thread_message_id); if (topic_info != nullptr && !topic_info->is_outgoing()) { @@ -513,7 +498,7 @@ void ForumTopicManager::on_get_forum_topic_info(DialogId dialog_id, const ForumT CHECK(dialog_topics != nullptr); auto forum_topic_info = td::make_unique(topic_info); MessageId top_thread_message_id = forum_topic_info->get_top_thread_message_id(); - CHECK(top_thread_message_id.is_valid()); + CHECK(can_be_message_thread_id(top_thread_message_id).is_ok()); auto topic = add_topic(dialog_topics, top_thread_message_id); if (topic->info_ == nullptr || *topic->info_ != *forum_topic_info) { topic->info_ = std::move(forum_topic_info); @@ -538,7 +523,7 @@ void ForumTopicManager::on_get_forum_topics(DialogId dialog_id, for (auto &forum_topic : forum_topics) { auto forum_topic_info = td::make_unique(forum_topic); MessageId top_thread_message_id = forum_topic_info->get_top_thread_message_id(); - if (!top_thread_message_id.is_valid()) { + if (can_be_message_thread_id(top_thread_message_id).is_error()) { continue; } auto topic = add_topic(dialog_topics, top_thread_message_id); @@ -566,6 +551,13 @@ bool ForumTopicManager::can_be_forum(DialogId dialog_id) const { td_->contacts_manager_->is_megagroup_channel(dialog_id.get_channel_id()); } +Status ForumTopicManager::can_be_message_thread_id(MessageId top_thread_message_id) { + if (!top_thread_message_id.is_valid() || !top_thread_message_id.is_server()) { + return Status::Error(400, "Invalid message thread identifier specified"); + } + return Status::OK(); +} + ForumTopicManager::DialogTopics *ForumTopicManager::add_dialog_topics(DialogId dialog_id) { auto *dialog_topics = dialog_topics_.get_pointer(dialog_id); if (dialog_topics == nullptr) { @@ -663,7 +655,7 @@ void ForumTopicManager::delete_topic_from_database(DialogId dialog_id, MessageId } 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) || can_be_message_thread_id(top_thread_message_id).is_error()) { LOG(ERROR) << "Change by " << diff << " number of loaded messages in thread of " << top_thread_message_id << " in " << dialog_id; return; diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index dd28bb595..577594af9 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -93,6 +93,8 @@ class ForumTopicManager final : public Actor { bool can_be_forum(DialogId dialog_id) const; + static Status can_be_message_thread_id(MessageId top_thread_message_id); + DialogTopics *add_dialog_topics(DialogId dialog_id); static Topic *add_topic(DialogTopics *dialog_topics, MessageId top_thread_message_id);