From a8a343704ae2e95e6c6cd077e4f0b7518c54e5a9 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 4 Apr 2022 15:35:09 +0300 Subject: [PATCH] Add ChannelType.h. --- CMakeLists.txt | 1 + td/telegram/BotCommandScope.cpp | 4 +-- td/telegram/ChannelType.h | 15 +++++++++++ td/telegram/ContactsManager.cpp | 4 +-- td/telegram/ContactsManager.h | 3 +-- td/telegram/DialogActionBar.cpp | 5 ++-- td/telegram/InlineQueriesManager.cpp | 4 +-- td/telegram/MessageContent.cpp | 8 +++--- td/telegram/MessagesManager.cpp | 34 ++++++++++++------------- td/telegram/PrivacyManager.cpp | 3 ++- td/telegram/SponsoredMessageManager.cpp | 2 +- 11 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 td/telegram/ChannelType.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 565f6a8a2..6e612228b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -483,6 +483,7 @@ set(TDLIB_SOURCE td/telegram/ChainId.h td/telegram/ChannelId.h td/telegram/ChannelParticipantFilter.h + td/telegram/ChannelType.h td/telegram/ChatId.h td/telegram/ClientActor.h td/telegram/ConfigManager.h diff --git a/td/telegram/BotCommandScope.cpp b/td/telegram/BotCommandScope.cpp index 938305217..e8f1dc67e 100644 --- a/td/telegram/BotCommandScope.cpp +++ b/td/telegram/BotCommandScope.cpp @@ -8,6 +8,7 @@ #include "td/telegram/AccessRights.h" #include "td/telegram/AuthManager.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/Td.h" @@ -78,8 +79,7 @@ Result BotCommandScope::get_bot_command_scope(Td *td, // ok break; case DialogType::Channel: - if (td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != - ContactsManager::ChannelType::Megagroup) { + if (td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ChannelType::Megagroup) { return Status::Error(400, "Can't change commands in channel chats"); } break; diff --git a/td/telegram/ChannelType.h b/td/telegram/ChannelType.h new file mode 100644 index 000000000..7c66e67a5 --- /dev/null +++ b/td/telegram/ChannelType.h @@ -0,0 +1,15 @@ +// +// 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/utils/common.h" + +namespace td { + +enum class ChannelType : uint8 { Broadcast, Megagroup, Unknown }; + +} // namespace td diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index c80c3e977..f5cdd2e00 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -14626,7 +14626,7 @@ bool ContactsManager::is_channel_public(const Channel *c) { return c != nullptr && (!c->username.empty() || c->has_location); } -ContactsManager::ChannelType ContactsManager::get_channel_type(ChannelId channel_id) const { +ChannelType ContactsManager::get_channel_type(ChannelId channel_id) const { auto c = get_channel(channel_id); if (c == nullptr) { auto min_channel = get_min_channel(channel_id); @@ -14638,7 +14638,7 @@ ContactsManager::ChannelType ContactsManager::get_channel_type(ChannelId channel return get_channel_type(c); } -ContactsManager::ChannelType ContactsManager::get_channel_type(const Channel *c) { +ChannelType ContactsManager::get_channel_type(const Channel *c) { if (c->is_megagroup) { return ChannelType::Megagroup; } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index eb797eaf4..128d62a68 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -9,6 +9,7 @@ #include "td/telegram/AccessRights.h" #include "td/telegram/BotCommand.h" #include "td/telegram/ChannelId.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ChatId.h" #include "td/telegram/Contact.h" #include "td/telegram/DialogAdministrator.h" @@ -518,8 +519,6 @@ class ContactsManager final : public Actor { bool get_secret_chat(SecretChatId secret_chat_id, bool force, Promise &&promise); bool get_secret_chat_full(SecretChatId secret_chat_id, Promise &&promise); - enum class ChannelType : uint8 { Broadcast, Megagroup, Unknown }; - ChannelType get_channel_type(ChannelId channel_id) const; int32 get_channel_date(ChannelId channel_id) const; DialogParticipantStatus get_channel_status(ChannelId channel_id) const; diff --git a/td/telegram/DialogActionBar.cpp b/td/telegram/DialogActionBar.cpp index 5f0dc65bf..c134bf18c 100644 --- a/td/telegram/DialogActionBar.cpp +++ b/td/telegram/DialogActionBar.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/DialogActionBar.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Td.h" @@ -97,8 +98,8 @@ void DialogActionBar::fix(Td *td, DialogId dialog_id, bool is_dialog_blocked, Fo } if (can_invite_members_) { if (dialog_type != DialogType::Chat && - (dialog_type != DialogType::Channel || td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Broadcast)) { + (dialog_type != DialogType::Channel || + td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast)) { LOG(ERROR) << "Receive can_invite_members in " << dialog_id; can_invite_members_ = false; } else if (can_report_spam_ || can_add_contact_ || can_block_user_ || can_share_phone_number_ || can_unarchive_) { diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 8ea9b3f1b..86b36d6b4 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -10,6 +10,7 @@ #include "td/telegram/AnimationsManager.h" #include "td/telegram/AudiosManager.h" #include "td/telegram/AuthManager.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/Contact.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Document.h" @@ -1462,8 +1463,7 @@ void InlineQueriesManager::on_get_inline_query_results(DialogId dialog_id, UserI break; } if (dialog_type == DialogType::Channel && - td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Broadcast) { + td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast) { continue; } if (dialog_type == DialogType::SecretChat) { diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 7d3714fa0..2edb72f36 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -13,6 +13,7 @@ #include "td/telegram/AuthManager.h" #include "td/telegram/CallDiscardReason.h" #include "td/telegram/ChannelId.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ChatId.h" #include "td/telegram/Contact.h" #include "td/telegram/ContactsManager.h" @@ -2593,8 +2594,8 @@ Status can_send_message_content(DialogId dialog_id, const MessageContent *conten } break; case MessageContentType::Game: - if (dialog_type == DialogType::Channel && td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Broadcast) { + if (dialog_type == DialogType::Channel && + td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast) { // return Status::Error(400, "Games can't be sent to channel chats"); } if (dialog_type == DialogType::SecretChat) { @@ -2632,8 +2633,7 @@ Status can_send_message_content(DialogId dialog_id, const MessageContent *conten return Status::Error(400, "Not enough rights to send polls to the chat"); } if (dialog_type == DialogType::Channel && - td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Broadcast && + td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast && !td->poll_manager_->get_poll_is_anonymous(static_cast(content)->poll_id)) { return Status::Error(400, "Non-anonymous polls can't be sent to channel chats"); } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0860f1158..1cffe300e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -8,6 +8,7 @@ #include "td/telegram/AuthManager.h" #include "td/telegram/ChainId.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ChatId.h" #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" @@ -2365,8 +2366,7 @@ class SearchMessagesQuery final : public Td::ResultHandler { } else if (top_thread_message_id.is_valid() && query.empty() && !sender_dialog_id.is_valid() && filter == MessageSearchFilter::Empty) { handle_errors_ = dialog_id.get_type() != DialogType::Channel || - td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != - ContactsManager::ChannelType::Broadcast; + td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ChannelType::Broadcast; send_query(G()->net_query_creator().create(telegram_api::messages_getReplies( std::move(input_peer), top_thread_message_id.get_server_message_id().get(), from_message_id.get_server_message_id().get(), 0, offset, limit, std::numeric_limits::max(), 0, 0))); @@ -11660,7 +11660,7 @@ void MessagesManager::delete_dialog_messages_by_sender(DialogId dialog_id, Dialo case DialogType::Channel: { channel_id = dialog_id.get_channel_id(); auto channel_type = td_->contacts_manager_->get_channel_type(channel_id); - if (channel_type != ContactsManager::ChannelType::Megagroup) { + if (channel_type != ChannelType::Megagroup) { return promise.set_error(Status::Error(400, "The method is available only for supergroup chats")); } channel_status = td_->contacts_manager_->get_channel_permissions(channel_id); @@ -21344,7 +21344,7 @@ td_api::object_ptr MessagesManager::get_chat_type_object(Dialo auto channel_type = td_->contacts_manager_->get_channel_type(channel_id); return td_api::make_object( td_->contacts_manager_->get_supergroup_id_object(channel_id, "chatTypeSupergroup"), - channel_type != ContactsManager::ChannelType::Megagroup); + channel_type != ChannelType::Megagroup); } case DialogType::SecretChat: { auto secret_chat_id = dialog_id.get_secret_chat_id(); @@ -25202,13 +25202,13 @@ Status MessagesManager::can_send_message(DialogId dialog_id) const { auto channel_status = td_->contacts_manager_->get_channel_permissions(channel_id); switch (channel_type) { - case ContactsManager::ChannelType::Unknown: - case ContactsManager::ChannelType::Megagroup: + case ChannelType::Unknown: + case ChannelType::Megagroup: if (!channel_status.can_send_messages()) { return Status::Error(400, "Have no rights to send a message"); } break; - case ContactsManager::ChannelType::Broadcast: { + case ChannelType::Broadcast: { if (!channel_status.can_post_messages()) { return Status::Error(400, "Need administrator rights in the channel chat"); } @@ -26573,14 +26573,14 @@ Result MessagesManager::send_bot_start_message(UserId bot_user_id, Di return Status::Error(400, "Can't access the chat"); } switch (td_->contacts_manager_->get_channel_type(channel_id)) { - case ContactsManager::ChannelType::Megagroup: + case ChannelType::Megagroup: if (!bot_data.can_join_groups) { return Status::Error(400, "The bot can't join groups"); } break; - case ContactsManager::ChannelType::Broadcast: + case ChannelType::Broadcast: return Status::Error(400, "Bots can't be invited to channel chats. Add them as administrators instead"); - case ContactsManager::ChannelType::Unknown: + case ChannelType::Unknown: default: UNREACHABLE(); } @@ -27047,8 +27047,7 @@ bool MessagesManager::is_group_dialog(DialogId dialog_id) const { case DialogType::Chat: return true; case DialogType::Channel: - return td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Megagroup; + return td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Megagroup; default: return false; } @@ -27059,8 +27058,7 @@ bool MessagesManager::is_broadcast_channel(DialogId dialog_id) const { return false; } - return td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == - ContactsManager::ChannelType::Broadcast; + return td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast; } bool MessagesManager::is_deleted_secret_chat(DialogId dialog_id) const { @@ -36384,7 +36382,7 @@ MessagesManager::Dialog *MessagesManager::add_new_dialog(unique_ptr &&d, break; case DialogType::Channel: { auto channel_type = td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()); - if (channel_type == ContactsManager::ChannelType::Broadcast) { + if (channel_type == ChannelType::Broadcast) { d->last_read_outbox_message_id = MessageId::max(); d->is_last_read_outbox_message_id_inited = true; } @@ -38885,13 +38883,13 @@ void MessagesManager::update_top_dialogs(DialogId dialog_id, const Message *m) { break; case DialogType::Channel: switch (td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id())) { - case ContactsManager::ChannelType::Broadcast: + case ChannelType::Broadcast: category = TopDialogCategory::Channel; break; - case ContactsManager::ChannelType::Megagroup: + case ChannelType::Megagroup: category = TopDialogCategory::Group; break; - case ContactsManager::ChannelType::Unknown: + case ChannelType::Unknown: break; default: UNREACHABLE(); diff --git a/td/telegram/PrivacyManager.cpp b/td/telegram/PrivacyManager.cpp index 22e9dc070..4c501772d 100644 --- a/td/telegram/PrivacyManager.cpp +++ b/td/telegram/PrivacyManager.cpp @@ -7,6 +7,7 @@ #include "td/telegram/PrivacyManager.h" #include "td/telegram/ChannelId.h" +#include "td/telegram/ChannelType.h" #include "td/telegram/ChatId.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/DialogId.h" @@ -159,7 +160,7 @@ void PrivacyManager::UserPrivacySettingRule::set_chat_ids(const vector &d break; case DialogType::Channel: { auto channel_id = dialog_id.get_channel_id(); - if (td->contacts_manager_->get_channel_type(channel_id) != ContactsManager::ChannelType::Megagroup) { + if (td->contacts_manager_->get_channel_type(channel_id) != ChannelType::Megagroup) { LOG(ERROR) << "Ignore broadcast " << channel_id; break; } diff --git a/td/telegram/SponsoredMessageManager.cpp b/td/telegram/SponsoredMessageManager.cpp index abed9abba..cf8df37e7 100644 --- a/td/telegram/SponsoredMessageManager.cpp +++ b/td/telegram/SponsoredMessageManager.cpp @@ -206,7 +206,7 @@ void SponsoredMessageManager::get_dialog_sponsored_message( return promise.set_error(Status::Error(400, "Chat not found")); } if (dialog_id.get_type() != DialogType::Channel || - td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ContactsManager::ChannelType::Broadcast) { + td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ChannelType::Broadcast) { return promise.set_value(nullptr); }