From 25574476a3bec903ab681c984c55f4cd3cf61db7 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 2 Apr 2024 15:17:31 +0300 Subject: [PATCH] Add td_api::toggleSupergroupCanHaveSponsoredMessages. --- td/generate/scheme/td_api.tl | 5 +++ td/telegram/ChatManager.cpp | 81 ++++++++++++++++++++++++++++++++++++ td/telegram/ChatManager.h | 5 +++ td/telegram/Td.cpp | 7 ++++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 6 +++ 6 files changed, 106 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 51921781c..bdd8d7876 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9834,6 +9834,11 @@ toggleSupergroupJoinByRequest supergroup_id:int53 join_by_request:Bool = Ok; //@description Toggles whether the message history of a supergroup is available to new members; requires can_change_info member right @supergroup_id The identifier of the supergroup @is_all_history_available The new value of is_all_history_available toggleSupergroupIsAllHistoryAvailable supergroup_id:int53 is_all_history_available:Bool = Ok; +//@description Toggles whether sponsored messages are shown in the channel chat; requires owner privileges in the channel. The chat must have at least chatBoostFeatures.min_sponsored_message_disable_boost_level boost level to disable sponsored messages +//@supergroup_id The identifier of the channel +//@can_have_sponsored_messages The new value of can_have_sponsored_messages +toggleSupergroupCanHaveSponsoredMessages supergroup_id:int53 can_have_sponsored_messages:Bool = Ok; + //@description Toggles whether non-administrators can receive only administrators and bots using getSupergroupMembers or searchChatMembers. Can be called only if supergroupFullInfo.can_hide_members == true //@supergroup_id Identifier of the supergroup //@has_hidden_members New value of has_hidden_members diff --git a/td/telegram/ChatManager.cpp b/td/telegram/ChatManager.cpp index 948ff3f46..306c60013 100644 --- a/td/telegram/ChatManager.cpp +++ b/td/telegram/ChatManager.cpp @@ -728,6 +728,57 @@ class TogglePrehistoryHiddenQuery final : public Td::ResultHandler { } }; +class RestrictSponsoredMessagesQuery final : public Td::ResultHandler { + Promise promise_; + ChannelId channel_id_; + bool can_have_sponsored_messages_; + + public: + explicit RestrictSponsoredMessagesQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(ChannelId channel_id, bool can_have_sponsored_messages) { + channel_id_ = channel_id; + can_have_sponsored_messages_ = can_have_sponsored_messages; + + auto input_channel = td_->chat_manager_->get_input_channel(channel_id); + CHECK(input_channel != nullptr); + send_query(G()->net_query_creator().create( + telegram_api::channels_restrictSponsoredMessages(std::move(input_channel), !can_have_sponsored_messages), + {{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 RestrictSponsoredMessagesQuery: " << to_string(ptr); + + td_->updates_manager_->on_get_updates( + std::move(ptr), + PromiseCreator::lambda([actor_id = G()->chat_manager(), promise = std::move(promise_), channel_id = channel_id_, + can_have_sponsored_messages = can_have_sponsored_messages_](Unit result) mutable { + send_closure(actor_id, &ChatManager::on_update_channel_can_have_sponsored_messages, channel_id, + can_have_sponsored_messages, 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_->chat_manager_->on_get_channel_error(channel_id_, status, "RestrictSponsoredMessagesQuery"); + } + promise_.set_error(std::move(status)); + } +}; + class ToggleParticipantsHiddenQuery final : public Td::ResultHandler { Promise promise_; ChannelId channel_id_; @@ -3093,6 +3144,23 @@ void ChatManager::toggle_channel_is_all_history_available(ChannelId channel_id, td_->create_handler(std::move(promise))->send(channel_id, is_all_history_available); } +void ChatManager::toggle_channel_can_have_sponsored_messages(ChannelId channel_id, bool can_have_sponsored_messages, + Promise &&promise) { + auto c = get_channel(channel_id); + if (c == nullptr) { + return promise.set_error(Status::Error(400, "Supergroup not found")); + } + if (!get_channel_status(c).is_creator()) { + return promise.set_error(Status::Error(400, "Not enough rights to disable sponsored messages")); + } + if (get_channel_type(c) != ChannelType::Broadcast) { + return promise.set_error(Status::Error(400, "Sponsored messages can be disabled only in channels")); + } + + td_->create_handler(std::move(promise)) + ->send(channel_id, can_have_sponsored_messages); +} + Status ChatManager::can_hide_chat_participants(ChatId chat_id) const { auto c = get_chat(chat_id); if (c == nullptr) { @@ -7271,6 +7339,19 @@ void ChatManager::on_update_channel_is_all_history_available(ChannelId channel_i promise.set_value(Unit()); } +void ChatManager::on_update_channel_can_have_sponsored_messages(ChannelId channel_id, bool can_have_sponsored_messages, + Promise &&promise) { + TRY_STATUS_PROMISE(promise, G()->close_status()); + CHECK(channel_id.is_valid()); + auto channel_full = get_channel_full_force(channel_id, true, "on_update_channel_can_have_sponsored_messages"); + if (channel_full != nullptr && channel_full->can_have_sponsored_messages != can_have_sponsored_messages) { + channel_full->can_have_sponsored_messages = can_have_sponsored_messages; + channel_full->is_changed = true; + update_channel_full(channel_full, channel_id, "on_update_channel_can_have_sponsored_messages"); + } + promise.set_value(Unit()); +} + void ChatManager::on_update_channel_has_hidden_participants(ChannelId channel_id, bool has_hidden_participants, Promise &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); diff --git a/td/telegram/ChatManager.h b/td/telegram/ChatManager.h index 0f0453a0e..114394955 100644 --- a/td/telegram/ChatManager.h +++ b/td/telegram/ChatManager.h @@ -167,6 +167,8 @@ class ChatManager final : public Actor { void on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 slow_mode_next_send_date); void on_update_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available, Promise &&promise); + void on_update_channel_can_have_sponsored_messages(ChannelId channel_id, bool can_have_sponsored_messages, + Promise &&promise); void on_update_channel_has_hidden_participants(ChannelId channel_id, bool has_hidden_participants, Promise &&promise); void on_update_channel_has_aggressive_anti_spam_enabled(ChannelId channel_id, bool has_aggressive_anti_spam_enabled, @@ -246,6 +248,9 @@ class ChatManager final : public Actor { void toggle_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available, Promise &&promise); + void toggle_channel_can_have_sponsored_messages(ChannelId channel_id, bool can_have_sponsored_messages, + Promise &&promise); + void toggle_channel_has_hidden_participants(ChannelId channel_id, bool has_hidden_participants, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 18f709225..b665a6b51 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8047,6 +8047,13 @@ void Td::on_request(uint64 id, const td_api::toggleSupergroupIsAllHistoryAvailab request.is_all_history_available_, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::toggleSupergroupCanHaveSponsoredMessages &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + chat_manager_->toggle_channel_can_have_sponsored_messages(ChannelId(request.supergroup_id_), + request.can_have_sponsored_messages_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::toggleSupergroupHasHiddenMembers &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 7faca6a2f..04e64cf58 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1461,6 +1461,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::toggleSupergroupIsAllHistoryAvailable &request); + void on_request(uint64 id, const td_api::toggleSupergroupCanHaveSponsoredMessages &request); + void on_request(uint64 id, const td_api::toggleSupergroupHasHiddenMembers &request); void on_request(uint64 id, const td_api::toggleSupergroupHasAggressiveAntiSpamEnabled &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 6ae4337be..f8a344b5f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5919,6 +5919,12 @@ class CliClient final : public Actor { get_args(args, supergroup_id, is_all_history_available); send_request(td_api::make_object(as_supergroup_id(supergroup_id), is_all_history_available)); + } else if (op == "tsgchsm") { + string supergroup_id; + bool can_have_sponsored_messages; + get_args(args, supergroup_id, can_have_sponsored_messages); + send_request(td_api::make_object( + as_supergroup_id(supergroup_id), can_have_sponsored_messages)); } else if (op == "tsghhm") { string supergroup_id; bool has_hidden_members;