From e44418f2b3a1533b71a20b80e201802efe55a6d9 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 11 Jul 2022 15:36:29 +0300 Subject: [PATCH] Add and use everywhere OptionsManager::get_synchronous_options(). --- td/telegram/OptionManager.cpp | 21 +++++++++++++++++---- td/telegram/OptionManager.h | 6 +++++- td/telegram/Td.cpp | 27 +++++++++++++-------------- td/telegram/Td.h | 2 ++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/td/telegram/OptionManager.cpp b/td/telegram/OptionManager.cpp index 2ad71e848..d892da653 100644 --- a/td/telegram/OptionManager.cpp +++ b/td/telegram/OptionManager.cpp @@ -29,6 +29,7 @@ #include "td/telegram/telegram_api.h" #include "td/telegram/TopDialogManager.h" +#include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/logging.h" #include "td/utils/misc.h" @@ -211,8 +212,13 @@ bool OptionManager::is_internal_option(Slice name) { } } +const vector &OptionManager::get_synchronous_options() { + static const vector options{"version", "commit_hash"}; + return options; +} + bool OptionManager::is_synchronous_option(Slice name) { - return name == "version" || name == "commit_hash"; + return td::contains(get_synchronous_options(), name); } void OptionManager::on_option_updated(const string &name) { @@ -433,7 +439,8 @@ void OptionManager::get_option(const string &name, Promise OptionManager::get_option_synchronously(const string &name) { +td_api::object_ptr OptionManager::get_option_synchronously(Slice name) { + CHECK(!name.empty()); switch (name[0]) { case 'c': if (name == "commit_hash") { @@ -775,9 +782,15 @@ td_api::object_ptr OptionManager::get_option_value_object(S return td_api::make_object(value.str()); } +void OptionManager::get_common_state(vector> &updates) { + for (auto option_name : get_synchronous_options()) { + updates.push_back( + td_api::make_object(option_name.str(), get_option_synchronously(option_name))); + } +} + void OptionManager::get_current_state(vector> &updates) const { - updates.push_back(td_api::make_object("version", get_option_synchronously("version"))); - updates.push_back(td_api::make_object("commit_hash", get_option_synchronously("commit_hash"))); + get_common_state(updates); updates.push_back(td_api::make_object( "online", td_api::make_object(td_->is_online()))); diff --git a/td/telegram/OptionManager.h b/td/telegram/OptionManager.h index 57a439117..5b21739d0 100644 --- a/td/telegram/OptionManager.h +++ b/td/telegram/OptionManager.h @@ -40,7 +40,9 @@ class OptionManager final : public Actor { static bool is_synchronous_option(Slice name); - static td_api::object_ptr get_option_synchronously(const string &name); + static td_api::object_ptr get_option_synchronously(Slice name); + + static void get_common_state(vector> &updates); void get_current_state(vector> &updates) const; @@ -49,6 +51,8 @@ class OptionManager final : public Actor { static bool is_internal_option(Slice name); + static const vector &get_synchronous_options(); + static td_api::object_ptr get_unix_time_option_value_object(); static td_api::object_ptr get_option_value_object(Slice value); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index ab90a955c..c6216e1bf 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3010,6 +3010,14 @@ td_api::object_ptr Td::get_fake_authorization_state_ } } +vector> Td::get_fake_current_state() const { + CHECK(state_ != State::Run); + vector> updates; + OptionManager::get_common_state(updates); + updates.push_back(td_api::make_object(get_fake_authorization_state_object())); + return updates; +} + DbKey Td::as_db_key(string key) { // Database will still be effectively not encrypted, but // 1. SQLite database will be protected from corruption, because that's how sqlcipher works @@ -3057,16 +3065,9 @@ void Td::run_request(uint64 id, tl_object_ptr function) { case td_api::getAuthorizationState::ID: // send response synchronously to prevent "Request aborted" return send_result(id, get_fake_authorization_state_object()); - case td_api::getCurrentState::ID: { - vector> updates; - updates.push_back( - td_api::make_object("version", OptionManager::get_option_synchronously("version"))); - updates.push_back(td_api::make_object( - "commit_hash", OptionManager::get_option_synchronously("commit_hash"))); - updates.push_back(td_api::make_object(get_fake_authorization_state_object())); + case td_api::getCurrentState::ID: // send response synchronously to prevent "Request aborted" - return send_result(id, td_api::make_object(std::move(updates))); - } + return send_result(id, td_api::make_object(get_fake_current_state())); case td_api::close::ID: // need to send response before actual closing send_closure(actor_id(this), &Td::send_result, id, td_api::make_object()); @@ -3279,11 +3280,9 @@ void Td::start_up() { alarm_timeout_.set_callback_data(static_cast(this)); CHECK(state_ == State::WaitParameters); - send_update(td_api::make_object("version", OptionManager::get_option_synchronously("version"))); - send_update( - td_api::make_object("commit_hash", OptionManager::get_option_synchronously("commit_hash"))); - send_update(td_api::make_object( - td_api::make_object())); + for (auto &update : get_fake_current_state()) { + send_update(std::move(update)); + } } void Td::tear_down() { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index c25b3ff5e..e67af1ae9 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -328,6 +328,8 @@ class Td final : public Actor { td_api::object_ptr get_fake_authorization_state_object() const; + vector> get_fake_current_state() const; + static void on_alarm_timeout_callback(void *td_ptr, int64 alarm_id); void on_alarm_timeout(int64 alarm_id);