From 086ec3d2e5745ddde645cc15069cd24136652202 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 30 Aug 2021 22:26:02 +0300 Subject: [PATCH] Add td_api::getPhoneNumberInfoSync. --- td/generate/scheme/td_api.tl | 4 ++++ td/telegram/CountryInfoManager.cpp | 16 ++++++++++++++++ td/telegram/CountryInfoManager.h | 3 +++ td/telegram/Td.cpp | 10 ++++++++++ td/telegram/Td.h | 3 +++ td/telegram/cli.cpp | 2 ++ 6 files changed, 38 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index e5d469b46..915149aec 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5446,6 +5446,10 @@ getCountryCode = Text; //@description Returns information about a phone number by its prefix. Can be called before authorization @phone_number_prefix The phone number prefix getPhoneNumberInfo phone_number_prefix:string = PhoneNumberInfo; +//@description Returns information about a phone number by its prefix synchronously. getCountries must be called at least once after changing localization to the specified language if properly localized country information is expected. Can be called synchronously +//@language_code A two-letter ISO 639-1 country code for country information localization @phone_number_prefix The phone number prefix +getPhoneNumberInfoSync language_code:string phone_number_prefix:string = PhoneNumberInfo; + //@description Returns the link for downloading official Telegram application to be used when the current user invites friends to Telegram getApplicationDownloadLink = HttpUrl; diff --git a/td/telegram/CountryInfoManager.cpp b/td/telegram/CountryInfoManager.cpp index 9437512aa..ac561434a 100644 --- a/td/telegram/CountryInfoManager.cpp +++ b/td/telegram/CountryInfoManager.cpp @@ -216,6 +216,22 @@ void CountryInfoManager::do_get_phone_number_info(string phone_number_prefix, st })); } +td_api::object_ptr CountryInfoManager::get_phone_number_info_sync(string language_code, + string phone_number_prefix) { + td::remove_if(phone_number_prefix, [](char c) { return c < '0' || c > '9'; }); + if (phone_number_prefix.empty()) { + return td_api::make_object(nullptr, string(), string()); + } + + std::lock_guard country_lock(country_mutex_); + auto list = get_country_list(nullptr, language_code); + if (list == nullptr) { + list = get_country_list(nullptr, "en"); + } + + return get_phone_number_info_object(list, phone_number_prefix); +} + td_api::object_ptr CountryInfoManager::get_phone_number_info_object(const CountryList *list, Slice phone_number) { CHECK(list != nullptr); diff --git a/td/telegram/CountryInfoManager.h b/td/telegram/CountryInfoManager.h index de4506c2e..98e747c1c 100644 --- a/td/telegram/CountryInfoManager.h +++ b/td/telegram/CountryInfoManager.h @@ -33,6 +33,9 @@ class CountryInfoManager final : public Actor { void get_phone_number_info(string phone_number_prefix, Promise> &&promise); + static td_api::object_ptr get_phone_number_info_sync(string language_code, + string phone_number_prefix); + CountryInfoManager(const CountryInfoManager &) = delete; CountryInfoManager &operator=(const CountryInfoManager &) = delete; CountryInfoManager(CountryInfoManager &&) = delete; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0d7208428..404877589 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3152,6 +3152,7 @@ bool Td::is_synchronous_request(int32 id) { case td_api::getFileExtension::ID: case td_api::cleanFileName::ID: case td_api::getLanguagePackString::ID: + case td_api::getPhoneNumberInfoSync::ID: case td_api::getChatFilterDefaultIconName::ID: case td_api::getJsonValue::ID: case td_api::getJsonString::ID: @@ -8269,6 +8270,10 @@ void Td::on_request(uint64 id, const td_api::getLanguagePackString &request) { UNREACHABLE(); } +void Td::on_request(uint64 id, const td_api::getPhoneNumberInfoSync &request) { + UNREACHABLE(); +} + void Td::on_request(uint64 id, const td_api::getPushReceiverId &request) { UNREACHABLE(); } @@ -8420,6 +8425,11 @@ td_api::object_ptr Td::do_static_request(const td_api::getLangua request.language_pack_database_path_, request.localization_target_, request.language_pack_id_, request.key_); } +td_api::object_ptr Td::do_static_request(const td_api::getPhoneNumberInfoSync &request) { + // don't check language_code/phone number UTF-8 correctness + return CountryInfoManager::get_phone_number_info_sync(request.language_code_, request.phone_number_prefix_); +} + td_api::object_ptr Td::do_static_request(const td_api::getPushReceiverId &request) { // don't check push payload UTF-8 correctness auto r_push_receiver_id = NotificationManager::get_push_receiver_id(request.payload_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index c79f007ba..dff566c67 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1224,6 +1224,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::getLanguagePackString &request); + void on_request(uint64 id, const td_api::getPhoneNumberInfoSync &request); + void on_request(uint64 id, const td_api::getPushReceiverId &request); void on_request(uint64 id, const td_api::getChatFilterDefaultIconName &request); @@ -1275,6 +1277,7 @@ class Td final : public NetQueryCallback { static td_api::object_ptr do_static_request(const td_api::getFileExtension &request); static td_api::object_ptr do_static_request(const td_api::cleanFileName &request); static td_api::object_ptr do_static_request(const td_api::getLanguagePackString &request); + static td_api::object_ptr do_static_request(const td_api::getPhoneNumberInfoSync &request); static td_api::object_ptr do_static_request(const td_api::getPushReceiverId &request); static td_api::object_ptr do_static_request(const td_api::getChatFilterDefaultIconName &request); static td_api::object_ptr do_static_request(td_api::getJsonValue &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 4362f251f..e890af10a 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2310,6 +2310,8 @@ class CliClient final : public Actor { send_request(td_api::make_object()); } else if (op == "gpni") { send_request(td_api::make_object(args)); + } else if (op == "gpnis") { + execute(td_api::make_object(rand_bool() ? "en" : "", args)); } else if (op == "gadl") { send_request(td_api::make_object()); } else if (op == "atos") {