Move get_option_value_object to OptionManager.

This commit is contained in:
levlam 2021-12-16 22:56:19 +03:00
parent b060536ae3
commit d7e559b45c
4 changed files with 34 additions and 40 deletions

View File

@ -101,10 +101,6 @@ string ConfigShared::get_option_string(Slice name, string default_value) const {
return str_value.substr(1);
}
tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value(Slice name) const {
return get_option_value_object(get_option(name));
}
bool ConfigShared::set_option(Slice name, Slice value) {
if (value.empty()) {
return config_pmc_->erase(name.str()) != 0;
@ -113,29 +109,6 @@ bool ConfigShared::set_option(Slice name, Slice value) {
}
}
tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value_object(Slice value) {
if (value.empty()) {
return make_tl_object<td_api::optionValueEmpty>();
}
switch (value[0]) {
case 'B':
if (value == "Btrue") {
return make_tl_object<td_api::optionValueBoolean>(true);
}
if (value == "Bfalse") {
return make_tl_object<td_api::optionValueBoolean>(false);
}
break;
case 'I':
return make_tl_object<td_api::optionValueInteger>(to_integer<int64>(value.substr(1)));
case 'S':
return make_tl_object<td_api::optionValueString>(value.substr(1).str());
}
return make_tl_object<td_api::optionValueString>(value.str());
}
void ConfigShared::on_option_updated(Slice name) const {
if (callback_ != nullptr) {
callback_->on_option_updated(name.str(), get_option(name));

View File

@ -6,8 +6,6 @@
//
#pragma once
#include "td/telegram/td_api.h"
#include "td/db/KeyValueSyncInterface.h"
#include "td/utils/common.h"
@ -41,24 +39,21 @@ 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<string, string> get_options() const;
bool get_option_boolean(Slice name, bool default_value = false) const;
int64 get_option_integer(Slice name, int64 default_value = 0) const;
string get_option_string(Slice name, string default_value = "") const;
tl_object_ptr<td_api::OptionValue> get_option_value(Slice name) const;
static tl_object_ptr<td_api::OptionValue> get_option_value_object(Slice value);
private:
std::shared_ptr<KeyValueSyncInterface> config_pmc_;
unique_ptr<Callback> callback_;
bool set_option(Slice name, Slice value);
string get_option(Slice name) const;
void on_option_updated(Slice name) const;
};

View File

@ -63,7 +63,7 @@ void OptionManager::on_update_server_time_difference() {
}
void OptionManager::clear_options() {
for (auto &option : G()->shared_config().get_options()) {
for (const auto &option : G()->shared_config().get_options()) {
if (!is_internal_option(option.first)) {
send_closure(
G()->td(), &Td::send_update,
@ -272,7 +272,8 @@ void OptionManager::on_option_updated(const string &name) {
}
// send_closure was already used in the callback
td_->send_update(td_api::make_object<td_api::updateOption>(name, G()->shared_config().get_option_value(name)));
td_->send_update(
td_api::make_object<td_api::updateOption>(name, get_option_value_object(G()->shared_config().get_option(name))));
}
void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td_api::OptionValue>> &&promise) {
@ -280,7 +281,7 @@ void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td
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));
promise.set_value(get_option_value_object(G()->shared_config().get_option(name)));
});
};
switch (name[0]) {
@ -623,6 +624,29 @@ void OptionManager::set_option(const string &name, td_api::object_ptr<td_api::Op
promise.set_error(Status::Error(400, "Option can't be set"));
}
td_api::object_ptr<td_api::OptionValue> OptionManager::get_option_value_object(Slice value) {
if (value.empty()) {
return td_api::make_object<td_api::optionValueEmpty>();
}
switch (value[0]) {
case 'B':
if (value == "Btrue") {
return td_api::make_object<td_api::optionValueBoolean>(true);
}
if (value == "Bfalse") {
return td_api::make_object<td_api::optionValueBoolean>(false);
}
break;
case 'I':
return td_api::make_object<td_api::optionValueInteger>(to_integer<int64>(value.substr(1)));
case 'S':
return td_api::make_object<td_api::optionValueString>(value.substr(1).str());
}
return td_api::make_object<td_api::optionValueString>(value.str());
}
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_api::make_object<td_api::optionValueString>(Td::TDLIB_VERSION)));
@ -634,8 +658,8 @@ void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>>
for (const auto &option : G()->shared_config().get_options()) {
if (!is_internal_option(option.first)) {
updates.push_back(td_api::make_object<td_api::updateOption>(
option.first, ConfigShared::get_option_value_object(option.second)));
updates.push_back(
td_api::make_object<td_api::updateOption>(option.first, get_option_value_object(option.second)));
}
}
}

View File

@ -47,6 +47,8 @@ class OptionManager final : public Actor {
static td_api::object_ptr<td_api::OptionValue> get_unix_time_option_value_object();
static td_api::object_ptr<td_api::OptionValue> get_option_value_object(Slice value);
void send_unix_time_update();
Td *td_;