From 7a92ee35fcb9081a0362592d983695a1798add1b Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 20 Dec 2019 02:58:41 +0300 Subject: [PATCH] Allow to set ignore_sensitive_content_restrictions option. GitOrigin-RevId: 17899f72c0485c821c150a258232e1348195a936 --- td/telegram/ConfigManager.cpp | 48 +++++++++++++++++++++++++++++++++++ td/telegram/ConfigManager.h | 4 +++ td/telegram/ConfigShared.h | 3 ++- td/telegram/Td.cpp | 19 ++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index db619b73..1f214f82 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -951,6 +951,26 @@ void ConfigManager::get_content_settings(Promise &&promise) { } } +void ConfigManager::set_content_settings(bool ignore_sensitive_content_restrictions, Promise &&promise) { + if (G()->close_flag()) { + return promise.set_error(Status::Error(500, "Request aborted")); + } + + auto &queries = set_content_settings_queries_[ignore_sensitive_content_restrictions]; + queries.push_back(std::move(promise)); + if (!is_set_content_settings_request_sent_) { + is_set_content_settings_request_sent_ = true; + int32 flags = 0; + if (ignore_sensitive_content_restrictions) { + flags |= telegram_api::account_setContentSettings::SENSITIVE_ENABLED_MASK; + } + G()->net_query_dispatcher().dispatch_with_callback( + G()->net_query_creator().create( + create_storer(telegram_api::account_setContentSettings(flags, false /*ignored*/))), + actor_shared(this, 3 + static_cast(ignore_sensitive_content_restrictions))); + } +} + void ConfigManager::on_dc_options_update(DcOptions dc_options) { save_dc_options_update(dc_options); send_closure(config_recoverer_, &ConfigRecoverer::on_dc_options_update, std::move(dc_options)); @@ -972,6 +992,34 @@ void ConfigManager::request_config_from_dc_impl(DcId dc_id) { void ConfigManager::on_result(NetQueryPtr res) { auto token = get_link_token(); + if (token == 3 || token == 4) { + is_set_content_settings_request_sent_ = false; + bool ignore_sensitive_content_restrictions = (token == 4); + auto promises = std::move(set_content_settings_queries_[ignore_sensitive_content_restrictions]); + set_content_settings_queries_[ignore_sensitive_content_restrictions].clear(); + CHECK(!promises.empty()); + auto result_ptr = fetch_result(std::move(res)); + if (result_ptr.is_error()) { + for (auto &promise : promises) { + promise.set_error(result_ptr.error().clone()); + } + } else { + ConfigShared &shared_config = G()->shared_config(); + if (shared_config.get_option_boolean("can_ignore_sensitive_content_restrictions")) { + shared_config.set_option_boolean("ignore_sensitive_content_restrictions", + ignore_sensitive_content_restrictions); + } + + for (auto &promise : promises) { + promise.set_value(Unit()); + } + } + + if (!set_content_settings_queries_[!ignore_sensitive_content_restrictions].empty()) { + set_content_settings(!ignore_sensitive_content_restrictions, Auto()); + } + return; + } if (token == 2) { auto promises = std::move(get_content_settings_queries_); get_content_settings_queries_.clear(); diff --git a/td/telegram/ConfigManager.h b/td/telegram/ConfigManager.h index 01545620..203ed11a 100644 --- a/td/telegram/ConfigManager.h +++ b/td/telegram/ConfigManager.h @@ -87,6 +87,8 @@ class ConfigManager : public NetQueryCallback { void get_content_settings(Promise &&promise); + void set_content_settings(bool ignore_sensitive_content_restrictions, Promise &&promise); + void on_dc_options_update(DcOptions dc_options); private: @@ -98,6 +100,8 @@ class ConfigManager : public NetQueryCallback { vector>> get_app_config_queries_; vector> get_content_settings_queries_; + vector> set_content_settings_queries_[2]; + bool is_set_content_settings_request_sent_ = false; void start_up() override; void hangup_shared() override; diff --git a/td/telegram/ConfigShared.h b/td/telegram/ConfigShared.h index ab8045f9..fcba8dc8 100644 --- a/td/telegram/ConfigShared.h +++ b/td/telegram/ConfigShared.h @@ -41,7 +41,6 @@ class ConfigShared { void set_option_string(Slice name, Slice value); bool have_option(Slice name) const; - string get_option(Slice name) const; std::unordered_map get_options(Slice prefix) const; std::unordered_map get_options() const; @@ -59,6 +58,8 @@ class ConfigShared { bool set_option(Slice name, Slice value); + string get_option(Slice name) const; + void on_option_updated(Slice name) const; }; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index d4175d88..3c3d1c5e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7085,6 +7085,25 @@ void Td::on_request(uint64 id, td_api::setOption &request) { if (false && !is_bot && set_boolean_option("include_sponsored_chat_to_unread_count")) { return; } + + if (!is_bot && request.name_ == "ignore_sensitive_content_restrictions") { + if (!G()->shared_config().get_option_boolean("can_ignore_sensitive_content_restrictions")) { + return send_error_raw(id, 3, "Option \"ignore_sensitive_content_restrictions\" can't be changed by the user"); + } + + if (value_constructor_id != td_api::optionValueBoolean::ID && + value_constructor_id != td_api::optionValueEmpty::ID) { + return send_error_raw(id, 3, "Option \"ignore_sensitive_content_restrictions\" must have boolean value"); + } + + auto ignore_sensitive_content_restrictions = + value_constructor_id == td_api::optionValueBoolean::ID && + static_cast(request.value_.get())->value_; + CREATE_OK_REQUEST_PROMISE(); + send_closure_later(config_manager_, &ConfigManager::set_content_settings, ignore_sensitive_content_restrictions, + std::move(promise)); + return; + } break; case 'l': if (!is_bot && set_string_option("language_pack_database_path", [](Slice value) { return true; })) {