Allow to get synchronously options "version" and "commit_hash".

This commit is contained in:
levlam 2022-07-11 13:33:56 +03:00
parent 61f7da7215
commit 17a548292b
6 changed files with 42 additions and 12 deletions

View File

@ -6196,7 +6196,7 @@ setUserPrivacySettingRules setting:UserPrivacySetting rules:userPrivacySettingRu
getUserPrivacySettingRules setting:UserPrivacySetting = UserPrivacySettingRules; getUserPrivacySettingRules setting:UserPrivacySetting = UserPrivacySettingRules;
//@description Returns the value of an option by its name. (Check the list of available options on https://core.telegram.org/tdlib/options.) Can be called before authorization //@description Returns the value of an option by its name. (Check the list of available options on https://core.telegram.org/tdlib/options.) Can be called before authorization. Can be called synchronously for options "version" and "commit_hash"
//@name The name of the option //@name The name of the option
getOption name:string = OptionValue; getOption name:string = OptionValue;

View File

@ -210,6 +210,10 @@ bool OptionManager::is_internal_option(Slice name) {
} }
} }
bool OptionManager::is_synchronous_option(Slice name) {
return name == "version" || name == "commit_hash";
}
void OptionManager::on_option_updated(const string &name) { void OptionManager::on_option_updated(const string &name) {
if (G()->close_flag()) { if (G()->close_flag()) {
return; return;
@ -398,9 +402,6 @@ void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td
if (!is_bot && name == "can_ignore_sensitive_content_restrictions") { if (!is_bot && name == "can_ignore_sensitive_content_restrictions") {
return send_closure_later(td_->config_manager_, &ConfigManager::get_content_settings, wrap_promise()); return send_closure_later(td_->config_manager_, &ConfigManager::get_content_settings, wrap_promise());
} }
if (name == "commit_hash") {
return promise.set_value(Td::get_commit_hash_option_value_object());
}
break; break;
case 'd': case 'd':
if (!is_bot && name == "disable_contact_registered_notifications") { if (!is_bot && name == "disable_contact_registered_notifications") {
@ -427,13 +428,24 @@ void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td
return promise.set_value(get_unix_time_option_value_object()); return promise.set_value(get_unix_time_option_value_object());
} }
break; break;
}
wrap_promise().set_value(Unit());
}
td_api::object_ptr<td_api::OptionValue> OptionManager::get_option_synchronously(const string &name) {
switch (name[0]) {
case 'c':
if (name == "commit_hash") {
return Td::get_commit_hash_option_value_object();
}
break;
case 'v': case 'v':
if (name == "version") { if (name == "version") {
return promise.set_value(Td::get_version_option_value_object()); return Td::get_version_option_value_object();
} }
break; break;
} }
wrap_promise().set_value(Unit()); UNREACHABLE();
} }
void OptionManager::set_option(const string &name, td_api::object_ptr<td_api::OptionValue> &&value, void OptionManager::set_option(const string &name, td_api::object_ptr<td_api::OptionValue> &&value,
@ -764,7 +776,8 @@ td_api::object_ptr<td_api::OptionValue> OptionManager::get_option_value_object(S
void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const { void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
updates.push_back(td_api::make_object<td_api::updateOption>("version", Td::get_version_option_value_object())); updates.push_back(td_api::make_object<td_api::updateOption>("version", Td::get_version_option_value_object()));
updates.push_back(td_api::make_object<td_api::updateOption>("commit_hash", Td::get_commit_hash_option_value_object())); updates.push_back(
td_api::make_object<td_api::updateOption>("commit_hash", Td::get_commit_hash_option_value_object()));
updates.push_back(td_api::make_object<td_api::updateOption>( updates.push_back(td_api::make_object<td_api::updateOption>(
"online", td_api::make_object<td_api::optionValueBoolean>(td_->is_online()))); "online", td_api::make_object<td_api::optionValueBoolean>(td_->is_online())));

View File

@ -38,6 +38,10 @@ class OptionManager final : public Actor {
static void clear_options(); static void clear_options();
static bool is_synchronous_option(Slice name);
static td_api::object_ptr<td_api::OptionValue> get_option_synchronously(const string &name);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const; void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private: private:

View File

@ -2894,8 +2894,8 @@ bool Td::is_authentication_request(int32 id) {
} }
} }
bool Td::is_synchronous_request(int32 id) { bool Td::is_synchronous_request(const td_api::Function *function) {
switch (id) { switch (function->get_id()) {
case td_api::getTextEntities::ID: case td_api::getTextEntities::ID:
case td_api::parseTextEntities::ID: case td_api::parseTextEntities::ID:
case td_api::parseMarkdown::ID: case td_api::parseMarkdown::ID:
@ -2920,6 +2920,8 @@ bool Td::is_synchronous_request(int32 id) {
case td_api::addLogMessage::ID: case td_api::addLogMessage::ID:
case td_api::testReturnError::ID: case td_api::testReturnError::ID:
return true; return true;
case td_api::getOption::ID:
return OptionManager::is_synchronous_option(static_cast<const td_api::getOption *>(function)->name_);
default: default:
return false; return false;
} }
@ -3032,7 +3034,7 @@ void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
} }
VLOG(td_requests) << "Receive request " << id << ": " << to_string(function); VLOG(td_requests) << "Receive request " << id << ": " << to_string(function);
if (is_synchronous_request(function->get_id())) { if (is_synchronous_request(function.get())) {
// send response synchronously // send response synchronously
return send_result(id, static_request(std::move(function))); return send_result(id, static_request(std::move(function)));
} }
@ -3059,7 +3061,8 @@ void Td::run_request(uint64 id, tl_object_ptr<td_api::Function> function) {
case td_api::getCurrentState::ID: { case td_api::getCurrentState::ID: {
vector<td_api::object_ptr<td_api::Update>> updates; vector<td_api::object_ptr<td_api::Update>> updates;
updates.push_back(td_api::make_object<td_api::updateOption>("version", get_version_option_value_object())); updates.push_back(td_api::make_object<td_api::updateOption>("version", get_version_option_value_object()));
updates.push_back(td_api::make_object<td_api::updateOption>("commit_hash", get_commit_hash_option_value_object())); updates.push_back(
td_api::make_object<td_api::updateOption>("commit_hash", get_commit_hash_option_value_object()));
updates.push_back(td_api::make_object<td_api::updateAuthorizationState>(get_fake_authorization_state_object())); updates.push_back(td_api::make_object<td_api::updateAuthorizationState>(get_fake_authorization_state_object()));
// send response synchronously to prevent "Request aborted" // send response synchronously to prevent "Request aborted"
return send_result(id, td_api::make_object<td_api::updates>(std::move(updates))); return send_result(id, td_api::make_object<td_api::updates>(std::move(updates)));
@ -8208,6 +8211,13 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::parseMarkdown &
return get_formatted_text_object(parsed_text, false, std::numeric_limits<int32>::max()); return get_formatted_text_object(parsed_text, false, std::numeric_limits<int32>::max());
} }
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getOption &request) {
if (!is_synchronous_request(&request)) {
return make_error(400, "The option can't be get synchronously");
}
return OptionManager::get_option_synchronously(request.name_);
}
td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::getMarkdownText &request) { td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::getMarkdownText &request) {
if (request.text_ == nullptr) { if (request.text_ == nullptr) {
return make_error(400, "Text must be non-empty"); return make_error(400, "Text must be non-empty");

View File

@ -384,7 +384,7 @@ class Td final : public Actor {
static bool is_authentication_request(int32 id); static bool is_authentication_request(int32 id);
static bool is_synchronous_request(int32 id); static bool is_synchronous_request(const td_api::Function *function);
static bool is_preinitialization_request(int32 id); static bool is_preinitialization_request(int32 id);
@ -1412,6 +1412,7 @@ class Td final : public Actor {
static td_api::object_ptr<td_api::Object> do_static_request(const T &request) { static td_api::object_ptr<td_api::Object> do_static_request(const T &request) {
return td_api::make_object<td_api::error>(400, "The method can't be executed synchronously"); return td_api::make_object<td_api::error>(400, "The method can't be executed synchronously");
} }
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getOption &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getTextEntities &request); static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getTextEntities &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::parseTextEntities &request); static td_api::object_ptr<td_api::Object> do_static_request(td_api::parseTextEntities &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::parseMarkdown &request); static td_api::object_ptr<td_api::Object> do_static_request(td_api::parseMarkdown &request);

View File

@ -2400,6 +2400,8 @@ class CliClient final : public Actor {
td_api::make_object<td_api::optionValueBoolean>(op == "on"))); td_api::make_object<td_api::optionValueBoolean>(op == "on")));
} else if (op == "go") { } else if (op == "go") {
send_request(td_api::make_object<td_api::getOption>(args)); send_request(td_api::make_object<td_api::getOption>(args));
} else if (op == "gos") {
execute(td_api::make_object<td_api::getOption>(args));
} else if (op == "sob") { } else if (op == "sob") {
string name; string name;
bool value; bool value;