Move getOption handling to OptionManager.

This commit is contained in:
levlam 2021-12-16 12:07:32 +03:00
parent 4254614e97
commit a333f3fb61
5 changed files with 65 additions and 83 deletions

View File

@ -6,8 +6,6 @@
//
#include "td/telegram/ConfigShared.h"
#include "td/telegram/td_api.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/SliceBuilder.h"

View File

@ -8,6 +8,7 @@
#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/Global.h"
@ -247,6 +248,60 @@ void OptionManager::on_option_updated(const string &name) {
td_->send_update(td_api::make_object<td_api::updateOption>(name, G()->shared_config().get_option_value(name)));
}
void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td_api::OptionValue>> &&promise) {
bool is_bot = td_->auth_manager_ != nullptr && td_->auth_manager_->is_authorized() && td_->auth_manager_->is_bot();
auto wrap_promise = [&] {
return PromiseCreator::lambda([promise = std::move(promise), name](Unit result) mutable {
// the option is already updated on success, ignore errors
promise.set_value(G()->shared_config().get_option_value(name));
});
};
switch (name[0]) {
// all these options should be added to getCurrentState
case 'a':
if (!is_bot && name == "archive_and_mute_new_chats_from_unknown_users") {
return send_closure_later(td_->config_manager_, &ConfigManager::get_global_privacy_settings, wrap_promise());
}
break;
case 'c':
if (!is_bot && name == "can_ignore_sensitive_content_restrictions") {
return send_closure_later(td_->config_manager_, &ConfigManager::get_content_settings, wrap_promise());
}
break;
case 'd':
if (!is_bot && name == "disable_contact_registered_notifications") {
return send_closure_later(td_->notification_manager_actor_,
&NotificationManager::get_disable_contact_registered_notifications, wrap_promise());
}
break;
case 'i':
if (!is_bot && name == "ignore_sensitive_content_restrictions") {
return send_closure_later(td_->config_manager_, &ConfigManager::get_content_settings, wrap_promise());
}
if (!is_bot && name == "is_location_visible") {
return send_closure_later(td_->contacts_manager_actor_, &ContactsManager::get_is_location_visible,
wrap_promise());
}
break;
case 'o':
if (name == "online") {
return promise.set_value(td_api::make_object<td_api::optionValueBoolean>(td_->is_online()));
}
break;
case 'u':
if (name == "unix_time") {
return promise.set_value(td_api::make_object<td_api::optionValueInteger>(G()->unix_time()));
}
break;
case 'v':
if (name == "version") {
return promise.set_value(td_api::make_object<td_api::optionValueString>(Td::TDLIB_VERSION));
}
break;
}
wrap_promise().set_value(Unit());
}
void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) {
for (const auto &option : G()->shared_config().get_options()) {
if (!is_internal_option(option.first)) {

View File

@ -9,6 +9,7 @@
#include "td/telegram/td_api.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
#include "td/utils/Slice.h"
@ -29,6 +30,8 @@ class OptionManager final : public Actor {
void on_option_updated(const string &name);
void get_option(const string &name, Promise<td_api::object_ptr<td_api::OptionValue>> &&promise);
static void clear_options();
static void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates);

View File

@ -4398,12 +4398,13 @@ void Td::on_request(uint64 id, td_api::confirmQrCodeAuthentication &request) {
void Td::on_request(uint64 id, const td_api::getCurrentState &request) {
vector<td_api::object_ptr<td_api::Update>> updates;
updates.push_back(td_api::make_object<td_api::updateOption>(
"version", td_api::make_object<td_api::optionValueString>(TDLIB_VERSION)));
updates.push_back(
td_api::make_object<td_api::updateOption>("online", make_tl_object<td_api::optionValueBoolean>(is_online_)));
updates.push_back(td_api::make_object<td_api::updateOption>(
"unix_time", make_tl_object<td_api::optionValueInteger>(G()->unix_time())));
updates.push_back(td_api::make_object<td_api::updateOption>(
"version", td_api::make_object<td_api::optionValueString>(TDLIB_VERSION)));
OptionManager::get_current_state(updates);
@ -7120,84 +7121,8 @@ void Td::on_request(uint64 id, td_api::deleteLanguagePack &request) {
void Td::on_request(uint64 id, td_api::getOption &request) {
CLEAN_INPUT_STRING(request.name_);
tl_object_ptr<td_api::OptionValue> option_value;
bool is_bot = auth_manager_ != nullptr && auth_manager_->is_authorized() && auth_manager_->is_bot();
switch (request.name_[0]) {
// all these options should be added to getCurrentState
case 'a':
if (!is_bot && request.name_ == "archive_and_mute_new_chats_from_unknown_users") {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<Unit> &&result) {
// the option is already updated on success, ignore errors
send_closure(actor_id, &Td::send_result, id,
G()->shared_config().get_option_value("archive_and_mute_new_chats_from_unknown_users"));
});
send_closure_later(config_manager_, &ConfigManager::get_global_privacy_settings, std::move(promise));
return;
}
break;
case 'c':
if (!is_bot && request.name_ == "can_ignore_sensitive_content_restrictions") {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<Unit> &&result) {
// the option is already updated on success, ignore errors
send_closure(actor_id, &Td::send_result, id,
G()->shared_config().get_option_value("can_ignore_sensitive_content_restrictions"));
});
send_closure_later(config_manager_, &ConfigManager::get_content_settings, std::move(promise));
return;
}
break;
case 'd':
if (!is_bot && request.name_ == "disable_contact_registered_notifications") {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<Unit> &&result) {
// the option is already updated on success, ignore errors
send_closure(actor_id, &Td::send_result, id,
G()->shared_config().get_option_value("disable_contact_registered_notifications"));
});
send_closure_later(notification_manager_actor_,
&NotificationManager::get_disable_contact_registered_notifications, std::move(promise));
return;
}
break;
case 'i':
if (!is_bot && request.name_ == "ignore_sensitive_content_restrictions") {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<Unit> &&result) {
// the option is already updated on success, ignore errors
send_closure(actor_id, &Td::send_result, id,
G()->shared_config().get_option_value("ignore_sensitive_content_restrictions"));
});
send_closure_later(config_manager_, &ConfigManager::get_content_settings, std::move(promise));
return;
}
if (!is_bot && request.name_ == "is_location_visible") {
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), id](Result<Unit> &&result) {
// the option is already updated on success, ignore errors
send_closure(actor_id, &Td::send_result, id, G()->shared_config().get_option_value("is_location_visible"));
});
send_closure_later(contacts_manager_actor_, &ContactsManager::get_is_location_visible, std::move(promise));
return;
}
break;
case 'o':
if (request.name_ == "online") {
option_value = make_tl_object<td_api::optionValueBoolean>(is_online_);
}
break;
case 'u':
if (request.name_ == "unix_time") {
option_value = make_tl_object<td_api::optionValueInteger>(G()->unix_time());
}
break;
case 'v':
if (request.name_ == "version") {
option_value = make_tl_object<td_api::optionValueString>(TDLIB_VERSION);
}
break;
}
if (option_value == nullptr) {
option_value = G()->shared_config().get_option_value(request.name_);
}
send_closure(actor_id(this), &Td::send_result, id, std::move(option_value));
CREATE_REQUEST_PROMISE();
option_manager_->get_option(request.name_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::setOption &request) {

View File

@ -98,6 +98,8 @@ class Td final : public Actor {
Td &operator=(Td &&) = delete;
~Td() final;
static constexpr const char *TDLIB_VERSION = "1.7.10";
struct Options {
std::shared_ptr<NetQueryStats> net_query_stats;
};
@ -253,7 +255,6 @@ class Td final : public Actor {
static td_api::object_ptr<td_api::Object> static_request(td_api::object_ptr<td_api::Function> function);
private:
static constexpr const char *TDLIB_VERSION = "1.7.10";
static constexpr int64 ONLINE_ALARM_ID = 0;
static constexpr int64 PING_SERVER_ALARM_ID = -1;
static constexpr int32 PING_SERVER_TIMEOUT = 300;