From 299dc26be6483d1a9b9636fb75316d25ef01ecbb Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 20 Dec 2022 15:01:39 +0300 Subject: [PATCH] Add phoneNumberInfo.is_anonymous. --- td/generate/scheme/td_api.tl | 6 ++-- td/telegram/ContactsManager.cpp | 49 ++-------------------------- td/telegram/ContactsManager.h | 8 ----- td/telegram/CountryInfoManager.cpp | 51 +++++++++++++++++++++++++++--- td/telegram/CountryInfoManager.h | 7 ++++ td/telegram/OptionManager.cpp | 3 +- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index fce8d5f1a..c9886086d 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -619,7 +619,6 @@ usernames active_usernames:vector disabled_usernames:vector edit //@is_verified True, if the user is verified //@is_premium True, if the user is a Telegram Premium user //@is_support True, if the user is Telegram support account -//@has_anonymous_phone_number True, if the user's phone number was bought on Fragment and isn't tied to a SIM card //@restriction_reason If non-empty, it contains a human-readable description of the reason why access to this user must be restricted //@is_scam True, if many users reported this user as a scam //@is_fake True, if many users reported this user as a fake account @@ -627,7 +626,7 @@ usernames active_usernames:vector disabled_usernames:vector edit //@type Type of the user //@language_code IETF language tag of the user's language; only available to bots //@added_to_attachment_menu True, if the user added the current bot to attachment menu; only available to bots -user id:int53 first_name:string last_name:string usernames:usernames phone_number:string status:UserStatus profile_photo:profilePhoto emoji_status:emojiStatus is_contact:Bool is_mutual_contact:Bool is_verified:Bool is_premium:Bool is_support:Bool has_anonymous_phone_number:Bool restriction_reason:string is_scam:Bool is_fake:Bool have_access:Bool type:UserType language_code:string added_to_attachment_menu:Bool = User; +user id:int53 first_name:string last_name:string usernames:usernames phone_number:string status:UserStatus profile_photo:profilePhoto emoji_status:emojiStatus is_contact:Bool is_mutual_contact:Bool is_verified:Bool is_premium:Bool is_support:Bool restriction_reason:string is_scam:Bool is_fake:Bool have_access:Bool type:UserType language_code:string added_to_attachment_menu:Bool = User; //@description Contains information about a bot @@ -1779,7 +1778,8 @@ countries countries:vector = Countries; //@country Information about the country to which the phone number belongs; may be null //@country_calling_code The part of the phone number denoting country calling code or its part //@formatted_phone_number The phone number without country calling code formatted accordingly to local rules. Expected digits are returned as '-', but even more digits might be entered by the user -phoneNumberInfo country:countryInfo country_calling_code:string formatted_phone_number:string = PhoneNumberInfo; +//@is_anonymous True, if the phone number was bought on Fragment and isn't tied to a SIM card +phoneNumberInfo country:countryInfo country_calling_code:string formatted_phone_number:string is_anonymous:Bool = PhoneNumberInfo; //@description Describes an action associated with a bank card number @text Action text @url The URL to be opened diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index dd8ddc6c9..730aecf30 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -3815,8 +3815,6 @@ ContactsManager::ContactsManager(Td *td, ActorShared<> parent) : td_(td), parent G()->td_db()->get_sqlite_pmc()->erase_by_prefix("us_bot_info", Auto()); } - on_update_fragment_prefixes(); - was_online_local_ = to_integer(G()->td_db()->get_binlog_pmc()->get("my_was_online_local")); was_online_remote_ = to_integer(G()->td_db()->get_binlog_pmc()->get("my_was_online_remote")); if (was_online_local_ >= G()->unix_time_cached() && !td_->is_online()) { @@ -11244,7 +11242,6 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo if (!u->phone_number.empty() && !td_->auth_manager_->is_bot()) { resolved_phone_numbers_[u->phone_number] = user_id; } - u->is_fragment_phone_number = is_fragment_phone_number(u->phone_number); u->is_phone_number_changed = false; } if (u->is_status_changed && user_id != get_my_id()) { @@ -17186,45 +17183,6 @@ void ContactsManager::on_update_channel_administrator_count(ChannelId channel_id } } -bool ContactsManager::is_fragment_phone_number(string phone_number) const { - if (phone_number.empty()) { - return false; - } - clean_phone_number(phone_number); - for (auto &prefix : fragment_prefixes_) { - if (begins_with(phone_number, prefix)) { - return true; - } - } - return false; -} - -void ContactsManager::on_update_fragment_prefixes() { - if (G()->close_flag()) { - return; - } - if (td_->auth_manager_->is_bot()) { - return; - } - - auto fragment_prefixes_str = td_->option_manager_->get_option_string("fragment_prefixes", "888"); - if (fragment_prefixes_str == fragment_prefixes_str_) { - return; - } - fragment_prefixes_str_ = std::move(fragment_prefixes_str); - fragment_prefixes_ = full_split(fragment_prefixes_str_, ','); - - users_.foreach([&](const UserId &user_id, unique_ptr &user) { - User *u = user.get(); - bool should_be_fragment_phone_number = is_fragment_phone_number(u->phone_number); - if (u->is_fragment_phone_number != should_be_fragment_phone_number) { - u->is_fragment_phone_number = should_be_fragment_phone_number; - u->is_changed = true; - update_user(u, user_id); - } - }); -} - void ContactsManager::on_update_dialog_administrators(DialogId dialog_id, vector &&administrators, bool have_access, bool from_database) { LOG(INFO) << "Update administrators in " << dialog_id << " to " << format::as_array(administrators); @@ -17894,8 +17852,7 @@ td_api::object_ptr ContactsManager::get_user_status_object(U td_api::object_ptr ContactsManager::get_update_unknown_user_object(UserId user_id) { return td_api::make_object(td_api::make_object( user_id.get(), "", "", nullptr, "", td_api::make_object(), nullptr, nullptr, false, - false, false, false, false, false, "", false, false, false, td_api::make_object(), "", - false)); + false, false, false, false, "", false, false, false, td_api::make_object(), "", false)); } int64 ContactsManager::get_user_id_object(UserId user_id, const char *source) const { @@ -17931,8 +17888,8 @@ tl_object_ptr ContactsManager::get_user_object(UserId user_id, con user_id.get(), u->first_name, u->last_name, u->usernames.get_usernames_object(), u->phone_number, get_user_status_object(user_id, u), get_profile_photo_object(td_->file_manager_.get(), u->photo), std::move(emoji_status), u->is_contact, u->is_mutual_contact, u->is_verified, u->is_premium, u->is_support, - u->is_fragment_phone_number, get_restriction_reason_description(u->restriction_reasons), u->is_scam, u->is_fake, - u->is_received, std::move(type), u->language_code, u->attach_menu_enabled); + get_restriction_reason_description(u->restriction_reasons), u->is_scam, u->is_fake, u->is_received, + std::move(type), u->language_code, u->attach_menu_enabled); } vector ContactsManager::get_user_ids_object(const vector &user_ids, const char *source) const { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 26c0d8bd8..0f1f50309 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -235,8 +235,6 @@ class ContactsManager final : public Actor { void on_update_bot_menu_button(UserId bot_user_id, tl_object_ptr &&bot_menu_button); - void on_update_fragment_prefixes(); - void on_update_dialog_administrators(DialogId dialog_id, vector &&administrators, bool have_access, bool from_database); @@ -743,7 +741,6 @@ class ContactsManager final : public Actor { bool need_apply_min_photo = false; bool can_be_added_to_attach_menu = false; bool attach_menu_enabled = false; - bool is_fragment_phone_number = false; bool is_photo_inited = false; @@ -1525,8 +1522,6 @@ class ContactsManager final : public Actor { bool is_user_contact(const User *u, UserId user_id, bool is_mutual) const; - bool is_fragment_phone_number(string phone_number) const; - int32 get_user_was_online(const User *u, UserId user_id) const; int64 get_contacts_hash(); @@ -1971,9 +1966,6 @@ class ContactsManager final : public Actor { vector imported_contact_user_ids_; // result of change_imported_contacts vector unimported_contact_invites_; // result of change_imported_contacts - string fragment_prefixes_str_; - vector fragment_prefixes_; - MultiTimeout user_online_timeout_{"UserOnlineTimeout"}; MultiTimeout user_emoji_status_timeout_{"UserEmojiStatusTimeout"}; MultiTimeout channel_unban_timeout_{"ChannelUnbanTimeout"}; diff --git a/td/telegram/CountryInfoManager.cpp b/td/telegram/CountryInfoManager.cpp index 73591a0b4..06f98683a 100644 --- a/td/telegram/CountryInfoManager.cpp +++ b/td/telegram/CountryInfoManager.cpp @@ -6,9 +6,11 @@ // #include "td/telegram/CountryInfoManager.h" +#include "td/telegram/AuthManager.h" #include "td/telegram/Global.h" #include "td/telegram/LanguagePackManager.h" #include "td/telegram/misc.h" +#include "td/telegram/OptionManager.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" @@ -115,6 +117,7 @@ struct CountryInfoManager::CountryList { }; CountryInfoManager::CountryInfoManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { + on_update_fragment_prefixes(); } CountryInfoManager::~CountryInfoManager() = default; @@ -135,6 +138,43 @@ void CountryInfoManager::tear_down() { } } +bool CountryInfoManager::is_fragment_phone_number(string phone_number) { + if (phone_number.empty()) { + return false; + } + if (fragment_prefixes_.empty()) { + fragment_prefixes_str_ = "888"; + fragment_prefixes_.push_back(fragment_prefixes_str_); + } + clean_phone_number(phone_number); + for (auto &prefix : fragment_prefixes_) { + if (begins_with(phone_number, prefix)) { + return true; + } + } + return false; +} + +void CountryInfoManager::on_update_fragment_prefixes() { + if (G()->close_flag()) { + return; + } + if (td_->auth_manager_->is_bot()) { + return; + } + if (!td_->option_manager_->have_option("fragment_prefixes")) { + return; + } + + auto fragment_prefixes_str = td_->option_manager_->get_option_string("fragment_prefixes", "888"); + std::lock_guard country_lock(country_mutex_); + if (fragment_prefixes_str == fragment_prefixes_str_) { + return; + } + fragment_prefixes_str_ = std::move(fragment_prefixes_str); + fragment_prefixes_ = full_split(fragment_prefixes_str_, ','); +} + string CountryInfoManager::get_main_language_code() { return to_lower(td_->language_pack_manager_.get_actor_unsafe()->get_main_language_code()); } @@ -182,7 +222,7 @@ void CountryInfoManager::get_phone_number_info(string phone_number_prefix, Promise> &&promise) { clean_phone_number(phone_number_prefix); if (phone_number_prefix.empty()) { - return promise.set_value(td_api::make_object(nullptr, string(), string())); + return promise.set_value(td_api::make_object(nullptr, string(), string(), false)); } do_get_phone_number_info(std::move(phone_number_prefix), get_main_language_code(), false, std::move(promise)); } @@ -225,7 +265,7 @@ td_api::object_ptr CountryInfoManager::get_phone_number string phone_number_prefix) { clean_phone_number(phone_number_prefix); if (phone_number_prefix.empty()) { - return td_api::make_object(nullptr, string(), string()); + return td_api::make_object(nullptr, string(), string(), false); } std::lock_guard country_lock(country_mutex_); @@ -244,6 +284,7 @@ td_api::object_ptr CountryInfoManager::get_phone_number const CallingCodeInfo *best_calling_code = nullptr; size_t best_length = 0; bool is_prefix = false; // is phone number a prefix of a valid country_code + prefix + bool is_anonymous = is_fragment_phone_number(phone_number.str()); for (auto &country : list->countries) { for (auto &calling_code : country.calling_codes) { if (begins_with(phone_number, calling_code.calling_code)) { @@ -267,7 +308,7 @@ td_api::object_ptr CountryInfoManager::get_phone_number } if (best_country == nullptr) { return td_api::make_object(nullptr, is_prefix ? phone_number.str() : string(), - is_prefix ? string() : phone_number.str()); + is_prefix ? string() : phone_number.str(), is_anonymous); } Slice formatted_part = phone_number.substr(best_calling_code->calling_code.size()); @@ -323,7 +364,7 @@ td_api::object_ptr CountryInfoManager::get_phone_number return td_api::make_object( best_country->get_country_info_object(), best_calling_code->calling_code, - formatted_phone_number.empty() ? formatted_part.str() : formatted_phone_number); + formatted_phone_number.empty() ? formatted_part.str() : formatted_phone_number, is_anonymous); } void CountryInfoManager::get_current_country_code(Promise &&promise) { @@ -539,5 +580,7 @@ const CountryInfoManager::CountryList *CountryInfoManager::get_country_list(Coun int32 CountryInfoManager::manager_count_ = 0; std::mutex CountryInfoManager::country_mutex_; FlatHashMap> CountryInfoManager::countries_; +string CountryInfoManager::fragment_prefixes_str_; +vector CountryInfoManager::fragment_prefixes_; } // namespace td diff --git a/td/telegram/CountryInfoManager.h b/td/telegram/CountryInfoManager.h index 013c5024e..431e4db15 100644 --- a/td/telegram/CountryInfoManager.h +++ b/td/telegram/CountryInfoManager.h @@ -37,6 +37,8 @@ class CountryInfoManager final : public Actor { static td_api::object_ptr get_phone_number_info_sync(const string &language_code, string phone_number_prefix); + void on_update_fragment_prefixes(); + CountryInfoManager(const CountryInfoManager &) = delete; CountryInfoManager &operator=(const CountryInfoManager &) = delete; CountryInfoManager(CountryInfoManager &&) = delete; @@ -53,6 +55,8 @@ class CountryInfoManager final : public Actor { string get_main_language_code(); + static bool is_fragment_phone_number(string phone_number); + void do_get_countries(string language_code, bool is_recursive, Promise> &&promise); @@ -78,6 +82,9 @@ class CountryInfoManager final : public Actor { static FlatHashMap> countries_; + static string fragment_prefixes_str_; + static vector fragment_prefixes_; + FlatHashMap>> pending_load_country_queries_; Td *td_; diff --git a/td/telegram/OptionManager.cpp b/td/telegram/OptionManager.cpp index 62cc362ea..18622543a 100644 --- a/td/telegram/OptionManager.cpp +++ b/td/telegram/OptionManager.cpp @@ -11,6 +11,7 @@ #include "td/telegram/AuthManager.h" #include "td/telegram/ConfigManager.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/CountryInfoManager.h" #include "td/telegram/DialogId.h" #include "td/telegram/GitCommitHash.h" #include "td/telegram/Global.h" @@ -364,7 +365,7 @@ void OptionManager::on_option_updated(Slice name) { td_->stickers_manager_->on_update_favorite_stickers_limit(); } if (name == "fragment_prefixes") { - send_closure(td_->contacts_manager_actor_, &ContactsManager::on_update_fragment_prefixes); + send_closure(td_->country_info_manager_actor_, &CountryInfoManager::on_update_fragment_prefixes); } break; case 'i':