From 17012dff1c76cf3745d92fd71cdaf9cd6cad5343 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 4 Jan 2024 20:20:18 +0300 Subject: [PATCH] Move set_dialog_permissions to DialogManager. --- td/telegram/DialogManager.cpp | 95 +++++++++++++++++++++++++++++++++ td/telegram/DialogManager.h | 3 ++ td/telegram/MessagesManager.cpp | 95 --------------------------------- td/telegram/MessagesManager.h | 3 -- td/telegram/Td.cpp | 2 +- 5 files changed, 99 insertions(+), 99 deletions(-) diff --git a/td/telegram/DialogManager.cpp b/td/telegram/DialogManager.cpp index bbebc5f2f..89693e9e5 100644 --- a/td/telegram/DialogManager.cpp +++ b/td/telegram/DialogManager.cpp @@ -81,6 +81,46 @@ class EditDialogTitleQuery final : public Td::ResultHandler { } }; +class EditChatDefaultBannedRightsQuery final : public Td::ResultHandler { + Promise promise_; + DialogId dialog_id_; + + public: + explicit EditChatDefaultBannedRightsQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id, RestrictedRights permissions) { + dialog_id_ = dialog_id; + auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write); + CHECK(input_peer != nullptr); + send_query(G()->net_query_creator().create(telegram_api::messages_editChatDefaultBannedRights( + std::move(input_peer), permissions.get_chat_banned_rights()))); + } + + 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 EditChatDefaultBannedRightsQuery: " << to_string(ptr); + td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_)); + } + + void on_error(Status status) final { + if (status.message() == "CHAT_NOT_MODIFIED") { + if (!td_->auth_manager_->is_bot()) { + promise_.set_value(Unit()); + return; + } + } else { + td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "EditChatDefaultBannedRightsQuery"); + } + promise_.set_error(std::move(status)); + } +}; + DialogManager::DialogManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -796,6 +836,61 @@ void DialogManager::set_dialog_profile_accent_color(DialogId dialog_id, AccentCo promise.set_error(Status::Error(400, "Can't change profile accent color in the chat")); } +void DialogManager::set_dialog_permissions(DialogId dialog_id, + const td_api::object_ptr &permissions, + Promise &&promise) { + if (!have_dialog_force(dialog_id, "set_dialog_permissions")) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + if (!have_input_peer(dialog_id, AccessRights::Write)) { + return promise.set_error(Status::Error(400, "Can't access the chat")); + } + + if (permissions == nullptr) { + return promise.set_error(Status::Error(400, "New permissions must be non-empty")); + } + + ChannelType channel_type = ChannelType::Unknown; + switch (dialog_id.get_type()) { + case DialogType::User: + return promise.set_error(Status::Error(400, "Can't change private chat permissions")); + case DialogType::Chat: { + auto chat_id = dialog_id.get_chat_id(); + auto status = td_->contacts_manager_->get_chat_permissions(chat_id); + if (!status.can_restrict_members()) { + return promise.set_error(Status::Error(400, "Not enough rights to change chat permissions")); + } + break; + } + case DialogType::Channel: { + if (is_broadcast_channel(dialog_id)) { + return promise.set_error(Status::Error(400, "Can't change channel chat permissions")); + } + auto status = td_->contacts_manager_->get_channel_permissions(dialog_id.get_channel_id()); + if (!status.can_restrict_members()) { + return promise.set_error(Status::Error(400, "Not enough rights to change chat permissions")); + } + channel_type = ChannelType::Megagroup; + break; + } + case DialogType::SecretChat: + return promise.set_error(Status::Error(400, "Can't change secret chat permissions")); + case DialogType::None: + default: + UNREACHABLE(); + } + + RestrictedRights new_permissions(permissions, channel_type); + + // TODO this can be wrong if there were previous change permissions requests + if (get_dialog_default_permissions(dialog_id) == new_permissions) { + return promise.set_value(Unit()); + } + + // TODO invoke after + td_->create_handler(std::move(promise))->send(dialog_id, new_permissions); +} + void DialogManager::set_dialog_emoji_status(DialogId dialog_id, const EmojiStatus &emoji_status, Promise &&promise) { if (!have_dialog_force(dialog_id, "set_dialog_emoji_status")) { diff --git a/td/telegram/DialogManager.h b/td/telegram/DialogManager.h index 82f4d0b1d..df7a8fc07 100644 --- a/td/telegram/DialogManager.h +++ b/td/telegram/DialogManager.h @@ -126,6 +126,9 @@ class DialogManager final : public Actor { void set_dialog_profile_accent_color(DialogId dialog_id, AccentColorId profile_accent_color_id, CustomEmojiId profile_background_custom_emoji_id, Promise &&promise); + void set_dialog_permissions(DialogId dialog_id, const td_api::object_ptr &permissions, + Promise &&promise); + void set_dialog_emoji_status(DialogId dialog_id, const EmojiStatus &emoji_status, Promise &&promise); void set_dialog_description(DialogId dialog_id, const string &description, Promise &&promise); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index bec6afba2..710948c5d 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -1227,46 +1227,6 @@ class SetHistoryTtlQuery final : public Td::ResultHandler { } }; -class EditChatDefaultBannedRightsQuery final : public Td::ResultHandler { - Promise promise_; - DialogId dialog_id_; - - public: - explicit EditChatDefaultBannedRightsQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(DialogId dialog_id, RestrictedRights permissions) { - dialog_id_ = dialog_id; - auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write); - CHECK(input_peer != nullptr); - send_query(G()->net_query_creator().create(telegram_api::messages_editChatDefaultBannedRights( - std::move(input_peer), permissions.get_chat_banned_rights()))); - } - - 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 EditChatDefaultBannedRightsQuery: " << to_string(ptr); - td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_)); - } - - void on_error(Status status) final { - if (status.message() == "CHAT_NOT_MODIFIED") { - if (!td_->auth_manager_->is_bot()) { - promise_.set_value(Unit()); - return; - } - } else { - td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "EditChatDefaultBannedRightsQuery"); - } - promise_.set_error(std::move(status)); - } -}; - class ToggleNoForwardsQuery final : public Td::ResultHandler { Promise promise_; DialogId dialog_id_; @@ -33213,61 +33173,6 @@ void MessagesManager::set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Prom } } -void MessagesManager::set_dialog_permissions(DialogId dialog_id, - const td_api::object_ptr &permissions, - Promise &&promise) { - if (!have_dialog_force(dialog_id, "set_dialog_permissions")) { - return promise.set_error(Status::Error(400, "Chat not found")); - } - if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Write)) { - return promise.set_error(Status::Error(400, "Can't access the chat")); - } - - if (permissions == nullptr) { - return promise.set_error(Status::Error(400, "New permissions must be non-empty")); - } - - ChannelType channel_type = ChannelType::Unknown; - switch (dialog_id.get_type()) { - case DialogType::User: - return promise.set_error(Status::Error(400, "Can't change private chat permissions")); - case DialogType::Chat: { - auto chat_id = dialog_id.get_chat_id(); - auto status = td_->contacts_manager_->get_chat_permissions(chat_id); - if (!status.can_restrict_members()) { - return promise.set_error(Status::Error(400, "Not enough rights to change chat permissions")); - } - break; - } - case DialogType::Channel: { - if (td_->dialog_manager_->is_broadcast_channel(dialog_id)) { - return promise.set_error(Status::Error(400, "Can't change channel chat permissions")); - } - auto status = td_->contacts_manager_->get_channel_permissions(dialog_id.get_channel_id()); - if (!status.can_restrict_members()) { - return promise.set_error(Status::Error(400, "Not enough rights to change chat permissions")); - } - channel_type = ChannelType::Megagroup; - break; - } - case DialogType::SecretChat: - return promise.set_error(Status::Error(400, "Can't change secret chat permissions")); - case DialogType::None: - default: - UNREACHABLE(); - } - - RestrictedRights new_permissions(permissions, channel_type); - - // TODO this can be wrong if there were previous change permissions requests - if (td_->dialog_manager_->get_dialog_default_permissions(dialog_id) == new_permissions) { - return promise.set_value(Unit()); - } - - // TODO invoke after - td_->create_handler(std::move(promise))->send(dialog_id, new_permissions); -} - void MessagesManager::toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content, Promise &&promise) { if (!have_dialog_force(dialog_id, "toggle_dialog_has_protected_content")) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index ee6801d23..961190ea9 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -531,9 +531,6 @@ class MessagesManager final : public Actor { td_api::object_ptr &&available_reactions_ptr, Promise &&promise); - void set_dialog_permissions(DialogId dialog_id, const td_api::object_ptr &permissions, - Promise &&promise); - void toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content, Promise &&promise); void set_dialog_theme(DialogId dialog_id, const string &theme_name, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 841492944..3cc925ea0 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6576,7 +6576,7 @@ void Td::on_request(uint64 id, const td_api::setChatEmojiStatus &request) { void Td::on_request(uint64 id, const td_api::setChatPermissions &request) { CREATE_OK_REQUEST_PROMISE(); - messages_manager_->set_dialog_permissions(DialogId(request.chat_id_), request.permissions_, std::move(promise)); + dialog_manager_->set_dialog_permissions(DialogId(request.chat_id_), request.permissions_, std::move(promise)); } void Td::on_request(uint64 id, td_api::setChatBackground &request) {