Use TRY_RESULT_PROMISE in LanguagePackManager.

This commit is contained in:
levlam 2024-04-27 22:49:59 +03:00
parent f6cb50ed93
commit c0c5aefabf
2 changed files with 22 additions and 56 deletions

View File

@ -2402,7 +2402,6 @@ void DialogParticipantManager::set_channel_participant_status(
auto on_result_promise =
PromiseCreator::lambda([actor_id = actor_id(this), channel_id, participant_dialog_id, new_status,
promise = std::move(promise)](Result<DialogParticipant> r_dialog_participant) mutable {
// ResultHandlers are cleared before managers, so it is safe to capture this
if (r_dialog_participant.is_error()) {
return promise.set_error(r_dialog_participant.move_as_error());
}

View File

@ -870,13 +870,9 @@ void LanguagePackManager::get_languages(bool only_local,
auto request_promise = PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_,
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::langpack_getLanguages>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
send_closure(actor_id, &LanguagePackManager::on_get_languages, r_result.move_as_ok(), std::move(language_pack),
false, std::move(promise));
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLanguages>(std::move(r_query)));
send_closure(actor_id, &LanguagePackManager::on_get_languages, std::move(result), std::move(language_pack), false,
std::move(promise));
});
send_with_promise(G()->net_query_creator().create_unauth(telegram_api::langpack_getLanguages(language_pack_)),
std::move(request_promise));
@ -891,13 +887,9 @@ void LanguagePackManager::search_language_info(string language_code,
auto request_promise =
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code,
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::langpack_getLanguage>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
LOG(INFO) << "Receive " << to_string(r_result.ok());
send_closure(actor_id, &LanguagePackManager::on_get_language, r_result.move_as_ok(), std::move(language_pack),
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLanguage>(std::move(r_query)));
LOG(INFO) << "Receive " << to_string(result);
send_closure(actor_id, &LanguagePackManager::on_get_language, std::move(result), std::move(language_pack),
std::move(language_code), std::move(promise));
});
send_with_promise(
@ -1061,12 +1053,8 @@ void LanguagePackManager::on_get_language(tl_object_ptr<telegram_api::langPackLa
string language_pack, string language_code,
Promise<td_api::object_ptr<td_api::languagePackInfo>> promise) {
CHECK(lang_pack_language != nullptr);
auto r_info = get_language_info(lang_pack_language.get());
if (r_info.is_error()) {
return promise.set_error(r_info.move_as_error());
}
auto result = get_language_pack_info_object(lang_pack_language->lang_code_, r_info.ok());
TRY_RESULT_PROMISE(promise, language_info, get_language_info(lang_pack_language.get()));
auto result = get_language_pack_info_object(lang_pack_language->lang_code_, language_info);
on_get_language_info(language_pack, result.get());
@ -1083,15 +1071,15 @@ void LanguagePackManager::on_get_language(tl_object_ptr<telegram_api::langPackLa
bool is_changed = false;
for (auto &info : pack->server_language_pack_infos_) {
if (info.first == lang_pack_language->lang_code_ || info.first == language_code) {
if (!(info.second == r_info.ok())) {
if (!(info.second == language_info)) {
LOG(INFO) << "Language pack " << info.first << " was changed";
is_changed = true;
info.second = r_info.ok();
info.second = language_info;
}
}
}
pack->all_server_language_pack_infos_[lang_pack_language->lang_code_] =
td::make_unique<LanguageInfo>(r_info.move_as_ok());
td::make_unique<LanguageInfo>(std::move(language_info));
if (is_changed) {
save_server_language_pack_infos(pack);
@ -1146,12 +1134,7 @@ void LanguagePackManager::get_language_pack_strings(string language_code, vector
auto request_promise =
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code,
promise = std::move(result_promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::langpack_getLangPack>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
auto result = r_result.move_as_ok();
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLangPack>(std::move(r_query)));
to_lower_inplace(result->lang_code_);
LOG(INFO) << "Receive language pack " << result->lang_code_ << " from version " << result->from_version_
<< " with version " << result->version_ << " of size " << result->strings_.size();
@ -1169,13 +1152,9 @@ void LanguagePackManager::get_language_pack_strings(string language_code, vector
auto request_promise =
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code, keys,
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::langpack_getStrings>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getStrings>(std::move(r_query)));
send_closure(actor_id, &LanguagePackManager::on_get_language_pack_strings, std::move(language_pack),
std::move(language_code), -1, false, std::move(keys), r_result.move_as_ok(), std::move(promise));
std::move(language_code), -1, false, std::move(keys), std::move(result), std::move(promise));
});
send_with_promise(G()->net_query_creator().create_unauth(
telegram_api::langpack_getStrings(language_pack_, language_code, std::move(keys))),
@ -1733,10 +1712,7 @@ void LanguagePackManager::set_custom_language(td_api::object_ptr<td_api::languag
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
}
auto r_info = get_language_info(language_pack_info.get());
if (r_info.is_error()) {
return promise.set_error(r_info.move_as_error());
}
TRY_RESULT_PROMISE(promise, language_info, get_language_info(language_pack_info.get()));
auto language_code = std::move(language_pack_info->id_);
if (!is_custom_language_code(language_code)) {
return promise.set_error(Status::Error(400, "Custom language pack ID must begin with 'X'"));
@ -1744,11 +1720,8 @@ void LanguagePackManager::set_custom_language(td_api::object_ptr<td_api::languag
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
for (auto &str : strings) {
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());
TRY_RESULT_PROMISE(promise, result, convert_to_telegram_api(std::move(str)));
server_strings.push_back(std::move(result));
}
// TODO atomic replace
@ -1761,7 +1734,7 @@ void LanguagePackManager::set_custom_language(td_api::object_ptr<td_api::languag
LanguagePack *pack = pack_it->second.get();
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
auto &info = pack->custom_language_pack_infos_[language_code];
info = r_info.move_as_ok();
info = std::move(language_info);
if (!pack->pack_kv_.empty()) {
pack->pack_kv_.set(language_code, get_language_info_string(info));
}
@ -1775,10 +1748,7 @@ void LanguagePackManager::edit_custom_language_info(td_api::object_ptr<td_api::l
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
}
auto r_info = get_language_info(language_pack_info.get());
if (r_info.is_error()) {
return promise.set_error(r_info.move_as_error());
}
TRY_RESULT_PROMISE(promise, language_info, get_language_info(language_pack_info.get()));
auto language_code = std::move(language_pack_info->id_);
if (!is_custom_language_code(language_code)) {
return promise.set_error(Status::Error(400, "Custom language pack ID must begin with 'X'"));
@ -1794,7 +1764,7 @@ void LanguagePackManager::edit_custom_language_info(td_api::object_ptr<td_api::l
return promise.set_error(Status::Error(400, "Custom language pack is not found"));
}
auto &info = language_info_it->second;
info = r_info.move_as_ok();
info = std::move(language_info);
if (!pack->pack_kv_.empty()) {
pack->pack_kv_.set(language_code, get_language_info_string(info));
}
@ -1824,13 +1794,10 @@ void LanguagePackManager::set_custom_language_string(string language_code,
vector<string> keys{str->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());
}
TRY_RESULT_PROMISE(promise, lang_pack_string, convert_to_telegram_api(std::move(str)));
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
server_strings.push_back(r_str.move_as_ok());
server_strings.push_back(std::move(lang_pack_string));
on_get_language_pack_strings(language_pack_, language_code, 1, true, std::move(keys), std::move(server_strings),
Auto());