2020-08-23 23:43:31 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2020-08-23 23:43:31 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/CountryInfoManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2020-08-24 22:38:45 +02:00
|
|
|
#include "td/telegram/LanguagePackManager.h"
|
2020-08-23 23:43:31 +02:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-09-25 14:14:33 +02:00
|
|
|
#include "td/utils/base64.h"
|
2020-08-23 23:43:31 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2020-09-25 14:14:33 +02:00
|
|
|
#include "td/utils/Gzip.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
#include "td/utils/logging.h"
|
2020-08-24 22:38:45 +02:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Random.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
#include "td/utils/Time.h"
|
2020-09-25 14:14:33 +02:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2020-08-23 23:43:31 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetNearestDcQuery final : public Td::ResultHandler {
|
2020-08-23 23:43:31 +02:00
|
|
|
Promise<string> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetNearestDcQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
|
|
|
send_query(G()->net_query_creator().create_unauth(telegram_api::help_getNearestDc()));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(uint64 id, BufferSlice packet) final {
|
2020-08-23 23:43:31 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::help_getNearestDc>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.move_as_ok();
|
|
|
|
promise_.set_value(std::move(result->country_));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(uint64 id, Status status) final {
|
2020-08-23 23:43:31 +02:00
|
|
|
if (!G()->is_expected_error(status) && status.message() != "BOT_METHOD_INVALID") {
|
|
|
|
LOG(ERROR) << "GetNearestDc returned " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetCountriesListQuery final : public Td::ResultHandler {
|
2020-08-24 22:38:45 +02:00
|
|
|
Promise<tl_object_ptr<telegram_api::help_CountriesList>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetCountriesListQuery(Promise<tl_object_ptr<telegram_api::help_CountriesList>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &language_code, int32 hash) {
|
|
|
|
send_query(G()->net_query_creator().create_unauth(telegram_api::help_getCountriesList(language_code, hash)));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(uint64 id, BufferSlice packet) final {
|
2021-08-29 17:03:26 +02:00
|
|
|
// LOG(ERROR) << base64url_encode(gzencode(packet, 0.9));
|
2020-08-24 22:38:45 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::help_getCountriesList>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(uint64 id, Status status) final {
|
2020-08-24 22:38:45 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
|
|
|
LOG(ERROR) << "GetCountriesList returned " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CountryInfoManager::CallingCodeInfo {
|
2020-08-25 12:42:14 +02:00
|
|
|
string calling_code;
|
2020-08-24 22:38:45 +02:00
|
|
|
vector<string> prefixes;
|
|
|
|
vector<string> patterns;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CountryInfoManager::CountryInfo {
|
|
|
|
string country_code;
|
|
|
|
string default_name;
|
|
|
|
string name;
|
|
|
|
vector<CallingCodeInfo> calling_codes;
|
|
|
|
bool is_hidden = false;
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::countryInfo> get_country_info_object() const {
|
|
|
|
return td_api::make_object<td_api::countryInfo>(
|
|
|
|
country_code, name.empty() ? default_name : name, default_name, is_hidden,
|
2020-08-25 12:42:14 +02:00
|
|
|
transform(calling_codes, [](const CallingCodeInfo &info) { return info.calling_code; }));
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CountryInfoManager::CountryList {
|
2021-08-30 18:20:40 +02:00
|
|
|
vector<CountryInfo> countries;
|
2020-08-24 22:38:45 +02:00
|
|
|
int32 hash = 0;
|
|
|
|
double next_reload_time = 0.0;
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::countries> get_countries_object() const {
|
|
|
|
return td_api::make_object<td_api::countries>(
|
2021-08-30 18:20:40 +02:00
|
|
|
transform(countries, [](const CountryInfo &info) { return info.get_country_info_object(); }));
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-24 13:39:03 +02:00
|
|
|
CountryInfoManager::CountryInfoManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2020-09-14 17:02:19 +02:00
|
|
|
CountryInfoManager::~CountryInfoManager() = default;
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
void CountryInfoManager::start_up() {
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
manager_count_++;
|
|
|
|
}
|
|
|
|
|
2020-08-24 13:39:03 +02:00
|
|
|
void CountryInfoManager::tear_down() {
|
|
|
|
parent_.reset();
|
2021-08-30 18:20:40 +02:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
manager_count_--;
|
2021-10-06 12:10:28 +02:00
|
|
|
if (manager_count_ == 0 && !countries_.empty()) {
|
2021-08-30 18:20:40 +02:00
|
|
|
LOG(INFO) << "Clear country info";
|
|
|
|
countries_.clear();
|
|
|
|
}
|
2020-08-23 23:43:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 22:38:45 +02:00
|
|
|
string CountryInfoManager::get_main_language_code() {
|
|
|
|
return to_lower(td_->language_pack_manager_->get_actor_unsafe()->get_main_language_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInfoManager::get_countries(Promise<td_api::object_ptr<td_api::countries>> &&promise) {
|
|
|
|
do_get_countries(get_main_language_code(), false, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInfoManager::do_get_countries(string language_code, bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::countries>> &&promise) {
|
2020-08-25 12:42:14 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
auto main_language_code = get_main_language_code();
|
|
|
|
if (language_code != main_language_code) {
|
|
|
|
language_code = std::move(main_language_code);
|
|
|
|
is_recursive = false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 18:20:40 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
auto list = get_country_list(this, language_code);
|
|
|
|
if (list != nullptr) {
|
|
|
|
return promise.set_value(list->get_countries_object());
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_error(Status::Error(500, "Requested data is inaccessible"));
|
|
|
|
}
|
|
|
|
load_country_list(language_code, 0,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_code,
|
|
|
|
promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
send_closure(actor_id, &CountryInfoManager::do_get_countries, std::move(language_code), true,
|
|
|
|
std::move(promise));
|
|
|
|
}));
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:42:14 +02:00
|
|
|
void CountryInfoManager::get_phone_number_info(string phone_number_prefix,
|
|
|
|
Promise<td_api::object_ptr<td_api::phoneNumberInfo>> &&promise) {
|
|
|
|
td::remove_if(phone_number_prefix, [](char c) { return c < '0' || c > '9'; });
|
|
|
|
if (phone_number_prefix.empty()) {
|
|
|
|
return promise.set_value(td_api::make_object<td_api::phoneNumberInfo>(nullptr, string(), string()));
|
|
|
|
}
|
|
|
|
do_get_phone_number_info(std::move(phone_number_prefix), get_main_language_code(), false, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInfoManager::do_get_phone_number_info(string phone_number_prefix, string language_code, bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::phoneNumberInfo>> &&promise) {
|
|
|
|
if (is_recursive) {
|
|
|
|
auto main_language_code = get_main_language_code();
|
|
|
|
if (language_code != main_language_code) {
|
|
|
|
language_code = std::move(main_language_code);
|
|
|
|
is_recursive = false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 18:20:40 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
auto list = get_country_list(this, language_code);
|
|
|
|
if (list != nullptr) {
|
|
|
|
return promise.set_value(get_phone_number_info_object(list, phone_number_prefix));
|
2020-08-25 12:42:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_error(Status::Error(500, "Requested data is inaccessible"));
|
|
|
|
}
|
|
|
|
load_country_list(language_code, 0,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), phone_number_prefix, language_code,
|
|
|
|
promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
send_closure(actor_id, &CountryInfoManager::do_get_phone_number_info,
|
|
|
|
std::move(phone_number_prefix), std::move(language_code), true, std::move(promise));
|
|
|
|
}));
|
2021-08-30 16:51:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 21:26:02 +02:00
|
|
|
td_api::object_ptr<td_api::phoneNumberInfo> 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<td_api::phoneNumberInfo>(nullptr, string(), string());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> 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);
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:51:09 +02:00
|
|
|
td_api::object_ptr<td_api::phoneNumberInfo> CountryInfoManager::get_phone_number_info_object(const CountryList *list,
|
|
|
|
Slice phone_number) {
|
|
|
|
CHECK(list != nullptr);
|
2020-08-25 12:42:14 +02:00
|
|
|
const CountryInfo *best_country = nullptr;
|
|
|
|
const CallingCodeInfo *best_calling_code = nullptr;
|
|
|
|
size_t best_length = 0;
|
2021-08-30 16:11:45 +02:00
|
|
|
bool is_prefix = false; // is phone number a prefix of a valid country_code + prefix
|
2021-08-30 18:20:40 +02:00
|
|
|
for (auto &country : list->countries) {
|
2020-08-25 12:42:14 +02:00
|
|
|
for (auto &calling_code : country.calling_codes) {
|
|
|
|
if (begins_with(phone_number, calling_code.calling_code)) {
|
|
|
|
auto calling_code_size = calling_code.calling_code.size();
|
|
|
|
for (auto &prefix : calling_code.prefixes) {
|
|
|
|
if (begins_with(prefix, phone_number.substr(calling_code_size))) {
|
|
|
|
is_prefix = true;
|
|
|
|
}
|
|
|
|
if (calling_code_size + prefix.size() > best_length &&
|
|
|
|
begins_with(phone_number.substr(calling_code_size), prefix)) {
|
|
|
|
best_country = &country;
|
|
|
|
best_calling_code = &calling_code;
|
|
|
|
best_length = calling_code_size + prefix.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (begins_with(calling_code.calling_code, phone_number)) {
|
|
|
|
is_prefix = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_country == nullptr) {
|
2021-08-30 16:51:09 +02:00
|
|
|
return td_api::make_object<td_api::phoneNumberInfo>(nullptr, is_prefix ? phone_number.str() : string(),
|
|
|
|
is_prefix ? string() : phone_number.str());
|
2020-08-25 12:42:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 16:51:09 +02:00
|
|
|
Slice formatted_part = phone_number.substr(best_calling_code->calling_code.size());
|
|
|
|
string formatted_phone_number;
|
2020-08-25 12:42:14 +02:00
|
|
|
size_t max_matched_digits = 0;
|
|
|
|
for (auto &pattern : best_calling_code->patterns) {
|
|
|
|
string result;
|
|
|
|
size_t current_pattern_pos = 0;
|
|
|
|
bool is_failed_match = false;
|
|
|
|
size_t matched_digits = 0;
|
|
|
|
for (auto &c : formatted_part) {
|
|
|
|
while (current_pattern_pos < pattern.size() && pattern[current_pattern_pos] != 'X' &&
|
|
|
|
!is_digit(pattern[current_pattern_pos])) {
|
|
|
|
result += pattern[current_pattern_pos++];
|
|
|
|
}
|
|
|
|
if (current_pattern_pos == pattern.size()) {
|
2021-08-30 16:11:45 +02:00
|
|
|
// result += ' ';
|
2021-08-13 20:06:54 +02:00
|
|
|
}
|
|
|
|
if (current_pattern_pos >= pattern.size() || pattern[current_pattern_pos] == 'X') {
|
2020-08-25 12:42:14 +02:00
|
|
|
result += c;
|
|
|
|
current_pattern_pos++;
|
|
|
|
} else {
|
|
|
|
CHECK(is_digit(pattern[current_pattern_pos]));
|
|
|
|
if (c == pattern[current_pattern_pos]) {
|
|
|
|
matched_digits++;
|
|
|
|
result += c;
|
|
|
|
current_pattern_pos++;
|
|
|
|
} else {
|
|
|
|
is_failed_match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 16:11:45 +02:00
|
|
|
for (size_t i = current_pattern_pos; i < pattern.size(); i++) {
|
|
|
|
if (is_digit(pattern[i])) {
|
|
|
|
is_failed_match = true;
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 12:42:14 +02:00
|
|
|
if (!is_failed_match && matched_digits >= max_matched_digits) {
|
|
|
|
max_matched_digits = matched_digits;
|
2021-08-30 16:11:45 +02:00
|
|
|
while (current_pattern_pos < pattern.size()) {
|
|
|
|
if (pattern[current_pattern_pos] == 'X') {
|
|
|
|
result.push_back('-');
|
|
|
|
} else {
|
|
|
|
CHECK(!is_digit(pattern[current_pattern_pos]));
|
|
|
|
result.push_back(' ');
|
|
|
|
}
|
|
|
|
current_pattern_pos++;
|
|
|
|
}
|
2020-08-25 12:42:14 +02:00
|
|
|
formatted_phone_number = std::move(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:51:09 +02:00
|
|
|
return td_api::make_object<td_api::phoneNumberInfo>(
|
2020-08-25 12:42:14 +02:00
|
|
|
best_country->get_country_info_object(), best_calling_code->calling_code,
|
2021-08-30 16:51:09 +02:00
|
|
|
formatted_phone_number.empty() ? formatted_part.str() : formatted_phone_number);
|
2020-08-25 12:42:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 23:43:31 +02:00
|
|
|
void CountryInfoManager::get_current_country_code(Promise<string> &&promise) {
|
|
|
|
td_->create_handler<GetNearestDcQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:38:45 +02:00
|
|
|
void CountryInfoManager::load_country_list(string language_code, int32 hash, Promise<Unit> &&promise) {
|
|
|
|
auto &queries = pending_load_country_queries_[language_code];
|
|
|
|
if (!promise && !queries.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() == 1) {
|
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), language_code](Result<tl_object_ptr<telegram_api::help_CountriesList>> &&result) {
|
|
|
|
send_closure(actor_id, &CountryInfoManager::on_get_country_list, language_code, std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetCountriesListQuery>(std::move(query_promise))->send(language_code, hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInfoManager::on_get_country_list(const string &language_code,
|
|
|
|
Result<tl_object_ptr<telegram_api::help_CountriesList>> r_country_list) {
|
|
|
|
auto query_it = pending_load_country_queries_.find(language_code);
|
|
|
|
CHECK(query_it != pending_load_country_queries_.end());
|
|
|
|
auto promises = std::move(query_it->second);
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
pending_load_country_queries_.erase(query_it);
|
|
|
|
|
|
|
|
if (r_country_list.is_error()) {
|
2021-08-30 18:20:40 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
auto it = countries_.find(language_code);
|
|
|
|
if (it != countries_.end()) {
|
|
|
|
// don't try to reload countries more often than once in 1-2 minutes
|
|
|
|
it->second->next_reload_time = max(Time::now() + Random::fast(60, 120), it->second->next_reload_time);
|
|
|
|
|
|
|
|
// if we have data for the language, then we don't need to fail promises
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_error(r_country_list.error().clone());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> country_lock(country_mutex_);
|
|
|
|
on_get_country_list_impl(language_code, r_country_list.move_as_ok());
|
|
|
|
}
|
2020-09-25 14:14:33 +02:00
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInfoManager::on_get_country_list_impl(const string &language_code,
|
|
|
|
tl_object_ptr<telegram_api::help_CountriesList> country_list) {
|
2020-08-24 22:38:45 +02:00
|
|
|
CHECK(country_list != nullptr);
|
2020-09-25 14:14:33 +02:00
|
|
|
auto &countries = countries_[language_code];
|
2020-08-24 22:38:45 +02:00
|
|
|
switch (country_list->get_id()) {
|
|
|
|
case telegram_api::help_countriesListNotModified::ID:
|
|
|
|
if (countries == nullptr) {
|
|
|
|
LOG(ERROR) << "Receive countriesListNotModified for unknown list with language code " << language_code;
|
|
|
|
countries_.erase(language_code);
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "List of countries with language code " << language_code << " is not modified";
|
|
|
|
countries->next_reload_time = Time::now() + Random::fast(86400, 2 * 86400);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case telegram_api::help_countriesList::ID: {
|
|
|
|
auto list = move_tl_object_as<telegram_api::help_countriesList>(country_list);
|
|
|
|
if (countries == nullptr) {
|
|
|
|
countries = make_unique<CountryList>();
|
|
|
|
}
|
|
|
|
for (auto &c : list->countries_) {
|
|
|
|
CountryInfo info;
|
|
|
|
info.country_code = std::move(c->iso2_);
|
|
|
|
info.default_name = std::move(c->default_name_);
|
|
|
|
info.name = std::move(c->name_);
|
|
|
|
info.is_hidden = std::move(c->hidden_);
|
|
|
|
for (auto &code : c->country_codes_) {
|
|
|
|
auto r_calling_code = to_integer_safe<int32>(code->country_code_);
|
|
|
|
if (r_calling_code.is_error() || r_calling_code.ok() <= 0) {
|
|
|
|
LOG(ERROR) << "Receive invalid calling code " << code->country_code_ << " for country "
|
|
|
|
<< info.country_code;
|
|
|
|
} else {
|
|
|
|
CallingCodeInfo calling_code_info;
|
2020-08-25 12:42:14 +02:00
|
|
|
calling_code_info.calling_code = std::move(code->country_code_);
|
2020-08-24 22:38:45 +02:00
|
|
|
calling_code_info.prefixes = std::move(code->prefixes_);
|
2020-08-25 12:42:14 +02:00
|
|
|
if (calling_code_info.prefixes.empty()) {
|
|
|
|
calling_code_info.prefixes.resize(1);
|
|
|
|
}
|
2020-08-24 22:38:45 +02:00
|
|
|
calling_code_info.patterns = std::move(code->patterns_);
|
|
|
|
info.calling_codes.push_back(std::move(calling_code_info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.calling_codes.empty()) {
|
|
|
|
LOG(ERROR) << "Receive empty list of calling codes for " << info.country_code;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
countries->countries.push_back(std::move(info));
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
countries->hash = list->hash_;
|
|
|
|
countries->next_reload_time = Time::now() + Random::fast(86400, 2 * 86400);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
const CountryInfoManager::CountryList *CountryInfoManager::get_country_list(CountryInfoManager *manager,
|
|
|
|
const string &language_code) {
|
2020-08-24 22:38:45 +02:00
|
|
|
auto it = countries_.find(language_code);
|
|
|
|
if (it == countries_.end()) {
|
2020-09-25 14:14:33 +02:00
|
|
|
if (language_code == "en") {
|
|
|
|
static const BufferSlice en = gzdecode(
|
|
|
|
base64url_decode(
|
2021-08-29 17:03:26 +02:00
|
|
|
"eJyNW81uI0lyzhb13-qeaXs8uwdjQMDAehfwLMj6ryP_RZFFsVmk_m5JMofMUbFKU6ySVnqBve0jGAb2sNiDb_YTtH034LfYi1_"
|
|
|
|
"BUaTEKlYFsxrobqmpjMzIyIgvvohM_Wv4P3_8h__6z3_8GyHkn_70xy_whexV6uSo4k4936fRz97BZ__"
|
|
|
|
"334XqHnwtyLr29tnxzU1x9WdLtkG-G7k8YNNixafjYmPBfRqwJfwsM5epl98-e7-aaPX3Jp6rSd5XfprNqcuXAXXT8numTBLymwli-"
|
|
|
|
"Rb5VHEDPgtp8TfFKvXH4ZRm9dgvS5qR0O_"
|
|
|
|
"4TZGtfbXJccWdhdxxKLKXaA4ld44u2NUZw3YQu6qq2BYWyPoLhsrqyttnp7FsYt1Lcgi6e6B5RlZSFLENB-"
|
|
|
|
"Sk4s8YmNFN2W71c1VJrGOTj5UF8_mEukWbLrzsevtlzci30xD2Gi4DP7vXPSU-"
|
|
|
|
"89O15M32XkegbyRLHY6c9Z5WTvjMSj5l52tyUPHDMSJbkExdrPcdOa28MH9M-c_URfzdVIRnXK2Q76reEo4Y3PWc-"
|
|
|
|
"S9s5j2uzJ6dSzZ04ZlXq-Q48ng69bDYA39VtLxzqNbJaZW6M4dO2XKe1cEwSmIdGuSoypwZDxeZc5ClzXxnN29AksKSapOcVUP_"
|
|
|
|
"HkxQbNKlh9lBkrRtm67_xHO0wA6hM6M-T9uRrOPOjMeeg7507lOewRrAKvnts5Ob2FixbBtkQz90pzyro5o6q-"
|
|
|
|
"K2z1YvyEEVIttF92eKbWxFNvYXgGz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rqgv5vC1YY18Pwz9-6gLXKOQ1ZeC_"
|
|
|
|
"qiW_3ZlWCf7tTmd4nGRqu1SdciwRfaH3mzHu2hD6JfDc3I8nFO-vpZGMCrnfd3wgpwO6c_"
|
|
|
|
"8lZVlz8EU97SGHXI09O4ZsIZsDjBjzjCE2mXIF57_Yxf4DUPyRWKsRc6is4l-S2IHVwS9kj669vGt8wWcGAJLWKJvicRYObwkB0MPcB_"
|
|
|
|
"lxHE_ZDggh5GeDKmdzK-40x4OyachQAqf0im44dAb08gJsv5n5P9ey_Aq0uWROiHG3WIuOwTuMKT8CTtrw9CEMTeEmBtS94XuyEFSzu-"
|
|
|
|
"-jCrkaHQfPctnGVk5_h2Es41sqq84apHD0WwX7xFzlpFNCiO7kuFb5a_gW6Nb0NsPI7qL4P42d0n3-"
|
|
|
|
"0Z35HT0Mma748vI3fdVjfyw5hFX3J0w-PqbYjBnxfW7nagcw_A3__"
|
|
|
|
"dzrhrk5Apy6QuUQAgG76lfcX971SLfV6OO2nIO2vkzLrpnkr5Cpzb5NLLTU6F3VqXcuQADrjgLIGenY2LPSPy-"
|
|
|
|
"09UIxlE3pAGCYXocO9dN8s01dRweJY1mGIRu9t5PM-K3ZdfR27zVr09hMbmqraPP9m5a5IeWA9HvFC1vzB0GqTkArswD-"
|
|
|
|
"O4Z0HKRlTcS69x0yGHHW3qPGKcyduby9dpX5Pu2GzDfXRUkoEKPBU8eJMYsVzUMKfF93He5bZCDW7ZgGD83NX0XpqzWv4V6_vbHTD2_-"
|
|
|
|
"pkixXeKyro22qx5F9UxKz61fpmP4NGepBMhJtxZ5PBu17usuJeLy16T4zu-GNPxE86FNfTNyJ__99_-9v8o_old")
|
2020-09-25 14:14:33 +02:00
|
|
|
.ok());
|
|
|
|
TlBufferParser parser(&en);
|
|
|
|
auto result = telegram_api::help_getCountriesList::fetch_result(parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
CHECK(parser.get_error() == nullptr);
|
|
|
|
on_get_country_list_impl(language_code, std::move(result));
|
|
|
|
|
|
|
|
it = countries_.find(language_code);
|
|
|
|
CHECK(it != countries_.end())
|
|
|
|
auto *country = it->second.get();
|
2021-08-30 18:20:40 +02:00
|
|
|
if (manager != nullptr) {
|
|
|
|
manager->load_country_list(language_code, country->hash, Auto());
|
|
|
|
}
|
2020-09-25 14:14:33 +02:00
|
|
|
return country;
|
|
|
|
}
|
2020-08-24 22:38:45 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *country = it->second.get();
|
|
|
|
CHECK(country != nullptr);
|
2021-08-30 18:20:40 +02:00
|
|
|
if (manager != nullptr && country->next_reload_time < Time::now()) {
|
|
|
|
manager->load_country_list(language_code, country->hash, Auto());
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return country;
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:20:40 +02:00
|
|
|
int32 CountryInfoManager::manager_count_ = 0;
|
|
|
|
std::mutex CountryInfoManager::country_mutex_;
|
|
|
|
std::unordered_map<string, unique_ptr<CountryInfoManager::CountryList>> CountryInfoManager::countries_;
|
|
|
|
|
2020-08-23 23:43:31 +02:00
|
|
|
} // namespace td
|