Add td_api::setCustomLanguageString.

GitOrigin-RevId: 0f4b7a531bacd9ab8eae0b8da631fe53dbaf1c7c
This commit is contained in:
levlam 2018-08-22 23:11:54 +03:00
parent c12ed8d9cc
commit 7e45ca25f7
8 changed files with 124 additions and 45 deletions

View File

@ -3133,9 +3133,12 @@ getLanguagePackInfo = LanguagePack;
//@description Returns strings from a language in the used language pack by their keys @language_code Language code of strings to return @keys Language pack keys of strings to return; may be empty to get all available strings
getLanguagePackStrings language_code:string keys:vector<string> = LanguagePackStrings;
//@description Adds or changes a custom language to the used language pack @info Information about the language. Language code should start with 'X' @strings New language pack strings
//@description Adds or changes a custom language to the used language pack @info Information about the language. Language code must start with 'X' @strings New language pack strings
setCustomLanguage info:languageInfo strings:vector<LanguagePackString> = Ok;
//@description Sets new value for a string in a custom language in the used language pack @language_code The code of previously added custom language, must start with 'X' @string New language pack string
setCustomLanguageString language_code:string string:LanguagePackString = Ok;
//@description Deletes all information about a language in the used language pack. Currently used language can't be deleted @language_code Language code of a language to delete
deleteLanguage language_code:string = Ok;

Binary file not shown.

View File

@ -656,7 +656,7 @@ void AnimationsManager::remove_saved_animation(const tl_object_ptr<td_api::Input
// TODO invokeAfter
auto file_view = td_->file_manager_->get_file_view(file_id);
CHECK(file_view.has_remote_location());
CHECK(file_view.remote_location().is_document());
CHECK(file_view.remote_location().is_document()) << file_view.remote_location();
CHECK(!file_view.remote_location().is_web());
td_->create_handler<SaveGifQuery>(std::move(promise))->send(file_view.remote_location().as_input_document(), true);

View File

@ -628,7 +628,7 @@ void LanguagePackManager::save_strings_to_database(Language *language, int32 new
return;
}
auto old_version = load_database_language_version(kv);
if (old_version >= new_version && (old_version != -1 || new_version != -1)) {
if (old_version > new_version || (old_version == new_version && strings.empty())) {
LOG(DEBUG) << "Language version doesn't increased from " << old_version;
return;
}
@ -662,7 +662,7 @@ void LanguagePackManager::on_get_language_pack_strings(
bool is_version_changed = false;
int32 new_database_version = -1;
vector<std::pair<string, string>> database_strings;
if (language == nullptr || (language->version_ < version || !keys.empty())) {
if (language == nullptr || language->version_ < version || !keys.empty()) {
if (language == nullptr) {
language = add_language(database_, language_pack, language_code);
CHECK(language != nullptr);
@ -792,56 +792,62 @@ void LanguagePackManager::on_failed_get_difference(string language_pack, string
}
}
Result<tl_object_ptr<telegram_api::LangPackString>> LanguagePackManager::convert_to_telegram_api(
tl_object_ptr<td_api::LanguagePackString> &&str) {
if (str == nullptr) {
return Status::Error(400, "Language strings must not be null");
}
string key;
downcast_call(*str, [&key](auto &value) { key = std::move(value.key_); });
if (!is_valid_key(key)) {
return Status::Error(400, "Key is invalid");
}
switch (str->get_id()) {
case td_api::languagePackStringValue::ID: {
auto value = static_cast<td_api::languagePackStringValue *>(str.get());
if (!clean_input_string(value->value_)) {
return Status::Error(400, "Strings must be encoded in UTF-8");
}
return make_tl_object<telegram_api::langPackString>(std::move(key), std::move(value->value_));
}
case td_api::languagePackStringPluralized::ID: {
auto value = static_cast<td_api::languagePackStringPluralized *>(str.get());
if (!clean_input_string(value->zero_value_) || !clean_input_string(value->one_value_) ||
!clean_input_string(value->two_value_) || !clean_input_string(value->few_value_) ||
!clean_input_string(value->many_value_) || !clean_input_string(value->other_value_)) {
return Status::Error(400, "Strings must be encoded in UTF-8");
}
return make_tl_object<telegram_api::langPackStringPluralized>(
31, std::move(key), std::move(value->zero_value_), std::move(value->one_value_), std::move(value->two_value_),
std::move(value->few_value_), std::move(value->many_value_), std::move(value->other_value_));
}
case td_api::languagePackStringDeleted::ID:
// there is no reason to save deleted strings in a custom language pack to database
return make_tl_object<telegram_api::langPackStringDeleted>(std::move(key));
default:
UNREACHABLE();
return nullptr;
}
}
void LanguagePackManager::set_custom_language(string language_code, string language_name, string language_native_name,
vector<tl_object_ptr<td_api::LanguagePackString>> strings,
Promise<Unit> &&promise) {
if (!is_custom_language_code(language_code)) {
return promise.set_error(Status::Error(400, "Custom language code must begin with 'X'"));
}
if (!check_language_code_name(language_code)) {
return promise.set_error(Status::Error(400, "Language code name must contain only letters and hyphen"));
}
if (!is_custom_language_code(language_code)) {
return promise.set_error(Status::Error(400, "Custom language code must begin with 'X'"));
}
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
for (auto &str : strings) {
if (str == nullptr) {
return promise.set_error(Status::Error(400, "Language strings must not be null"));
}
string key;
downcast_call(*str, [&key](auto &value) { key = std::move(value.key_); });
if (!is_valid_key(key)) {
return promise.set_error(Status::Error(400, "Key is invalid"));
}
switch (str->get_id()) {
case td_api::languagePackStringValue::ID: {
auto value = static_cast<td_api::languagePackStringValue *>(str.get());
if (!clean_input_string(value->value_)) {
return promise.set_error(Status::Error(400, "Strings must be encoded in UTF-8"));
}
server_strings.push_back(
make_tl_object<telegram_api::langPackString>(std::move(key), std::move(value->value_)));
break;
}
case td_api::languagePackStringPluralized::ID: {
auto value = static_cast<td_api::languagePackStringPluralized *>(str.get());
if (!clean_input_string(value->zero_value_) || !clean_input_string(value->one_value_) ||
!clean_input_string(value->two_value_) || !clean_input_string(value->few_value_) ||
!clean_input_string(value->many_value_) || !clean_input_string(value->other_value_)) {
return promise.set_error(Status::Error(400, "Strings must be encoded in UTF-8"));
}
server_strings.push_back(make_tl_object<telegram_api::langPackStringPluralized>(
31, std::move(key), std::move(value->zero_value_), std::move(value->one_value_),
std::move(value->two_value_), std::move(value->few_value_), std::move(value->many_value_),
std::move(value->other_value_)));
break;
}
case td_api::languagePackStringDeleted::ID:
// there is no reason to save deleted strings in a custom language pack to database
break;
default:
UNREACHABLE();
break;
auto r_result = convert_to_telegram_api(std::move(str));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
server_strings.push_back(r_result.move_as_ok());
}
// TODO atomic replace
@ -855,11 +861,45 @@ void LanguagePackManager::set_custom_language(string language_code, string langu
auto &info = pack->language_infos_[language_code];
info.name = language_name;
info.native_name = language_native_name;
pack->pack_kv_.set(language_code, PSLICE() << language_name << '\x00' << language_native_name);
if (!pack->pack_kv_.empty()) {
pack->pack_kv_.set(language_code, PSLICE() << language_name << '\x00' << language_native_name);
}
promise.set_value(Unit());
}
void LanguagePackManager::set_custom_language_string(string language_code,
tl_object_ptr<td_api::LanguagePackString> str,
Promise<Unit> &&promise) {
if (!check_language_code_name(language_code)) {
return promise.set_error(Status::Error(400, "Language code name must contain only letters and hyphen"));
}
if (!is_custom_language_code(language_code)) {
return promise.set_error(Status::Error(400, "Custom language code must begin with 'X'"));
}
if (get_language(database_, language_pack_, language_code) == nullptr) {
return promise.set_error(Status::Error(400, "Custom language not found"));
}
string key;
if (str != nullptr) {
downcast_call(*str, [&key](auto &value) { key = value.key_; });
}
auto r_str = convert_to_telegram_api(std::move(str));
if (r_str.is_error()) {
return promise.set_error(r_str.move_as_error());
}
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
server_strings.push_back(r_str.move_as_ok());
on_get_language_pack_strings(language_pack_, language_code, 1, true, {std::move(key)}, std::move(server_strings),
Auto());
promise.set_value(Unit());
}
void LanguagePackManager::delete_language(string language_code, Promise<Unit> &&promise) {
if (!check_language_code_name(language_code)) {
return promise.set_error(Status::Error(400, "Language code name is invalid"));

View File

@ -60,6 +60,9 @@ class LanguagePackManager : public NetQueryCallback {
void set_custom_language(string language_code, string language_name, string language_native_name,
vector<tl_object_ptr<td_api::LanguagePackString>> strings, Promise<Unit> &&promise);
void set_custom_language_string(string language_code, tl_object_ptr<td_api::LanguagePackString> str,
Promise<Unit> &&promise);
void delete_language(string language_code, Promise<Unit> &&promise);
private:
@ -105,6 +108,9 @@ class LanguagePackManager : public NetQueryCallback {
static td_api::object_ptr<td_api::languagePackStrings> get_language_pack_strings_object(Language *language,
const vector<string> &keys);
static Result<tl_object_ptr<telegram_api::LangPackString>> convert_to_telegram_api(
tl_object_ptr<td_api::LanguagePackString> &&str);
void inc_generation();
static bool is_valid_key(Slice key);

View File

@ -3268,6 +3268,7 @@ bool Td::is_preauthentication_request(int32 id) {
case td_api::getLanguagePackInfo::ID:
case td_api::getLanguagePackStrings::ID:
case td_api::setCustomLanguage::ID:
case td_api::setCustomLanguageString::ID:
case td_api::deleteLanguage::ID:
case td_api::getOption::ID:
case td_api::setOption::ID:
@ -6053,6 +6054,14 @@ void Td::on_request(uint64 id, td_api::setCustomLanguage &request) {
std::move(promise));
}
void Td::on_request(uint64 id, td_api::setCustomLanguageString &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.language_code_);
CREATE_OK_REQUEST_PROMISE();
send_closure(language_pack_manager_, &LanguagePackManager::set_custom_language_string,
std::move(request.language_code_), std::move(request.string_), std::move(promise));
}
void Td::on_request(uint64 id, td_api::deleteLanguage &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.language_code_);

View File

@ -769,6 +769,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, td_api::setCustomLanguage &request);
void on_request(uint64 id, td_api::setCustomLanguageString &request);
void on_request(uint64 id, td_api::deleteLanguage &request);
void on_request(uint64 id, td_api::getOption &request);

View File

@ -1786,6 +1786,25 @@ class CliClient final : public Actor {
send_request(make_tl_object<td_api::setCustomLanguage>(
make_tl_object<td_api::languageInfo>(language_code, name, native_name), std::move(strings)));
} else if (op == "sclsv" || op == "sclsp" || op == "sclsd") {
string language_code;
string key;
string value;
std::tie(language_code, args) = split(args);
std::tie(key, value) = split(args);
tl_object_ptr<td_api::LanguagePackString> str;
if (op == "sclsv") {
str = make_tl_object<td_api::languagePackStringValue>(key, value);
} else if (op == "sclsp") {
str = make_tl_object<td_api::languagePackStringPluralized>(key, value, string("One\0One", 7), "Two", "Few",
"Many", "Other");
} else {
str = make_tl_object<td_api::languagePackStringDeleted>(key);
}
send_request(make_tl_object<td_api::setCustomLanguageString>(language_code, std::move(str)));
} else if (op == "dl") {
send_request(make_tl_object<td_api::deleteLanguage>(args));
} else if (op == "go") {