Add td_api::searchUserByPhoneNumber.

This commit is contained in:
levlam 2022-02-18 18:13:10 +03:00
parent 254d74c4a6
commit a92d1b2ae4
6 changed files with 120 additions and 1 deletions

View File

@ -5401,6 +5401,9 @@ changeImportedContacts contacts:vector<contact> = ImportedContacts;
clearImportedContacts = Ok;
//@description Searches a user by their phone number @phone_number Phone number to search for
searchUserByPhoneNumber phone_number:string = User;
//@description Shares the phone number of the current user with a mutual contact. Supposed to be called when the user clicks on chatActionBarSharePhoneNumber @user_id Identifier of the user with whom to share the phone number. The user must be a mutual contact
sharePhoneNumber user_id:int53 = Ok;

View File

@ -188,6 +188,50 @@ class AddContactQuery final : public Td::ResultHandler {
}
};
class ResolvePhoneQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
string phone_number_;
public:
explicit ResolvePhoneQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const string &phone_number) {
phone_number_ = phone_number;
send_query(G()->net_query_creator().create(telegram_api::contacts_resolvePhone(phone_number)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::contacts_resolvePhone>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(DEBUG) << "Receive result for ResolvePhoneQuery: " << to_string(ptr);
td_->contacts_manager_->on_get_users(std::move(ptr->users_), "ResolvePhoneQuery");
td_->contacts_manager_->on_get_chats(std::move(ptr->chats_), "ResolvePhoneQuery");
DialogId dialog_id(ptr->peer_);
if (dialog_id.get_type() != DialogType::User) {
LOG(ERROR) << "Receive " << dialog_id << " by " << phone_number_;
return on_error(Status::Error(500, "Receive invalid response"));
}
td_->contacts_manager_->on_resolved_phone_number(phone_number_, dialog_id.get_user_id());
promise_.set_value(Unit());
}
void on_error(Status status) final {
if (status.message() == Slice("PHONE_NOT_OCCUPIED")) {
td_->contacts_manager_->on_resolved_phone_number(phone_number_, UserId());
return promise_.set_value(Unit());
}
promise_.set_error(std::move(status));
}
};
class AcceptContactQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
UserId user_id_;
@ -5660,6 +5704,42 @@ std::pair<int32, vector<UserId>> ContactsManager::search_contacts(const string &
return {narrow_cast<int32>(result.first), std::move(user_ids)};
}
UserId ContactsManager::search_user_by_phone_number(string phone_number, Promise<Unit> &&promise) {
clean_phone_number(phone_number);
if (phone_number.empty()) {
promise.set_error(Status::Error(200, "Phone number is invalid"));
return UserId();
}
auto it = resolved_phone_numbers_.find(phone_number);
if (it != resolved_phone_numbers_.end()) {
promise.set_value(Unit());
return it->second;
}
td_->create_handler<ResolvePhoneQuery>(std::move(promise))->send(phone_number);
return UserId();
}
void ContactsManager::on_resolved_phone_number(const string &phone_number, UserId user_id) {
if (!user_id.is_valid()) {
resolved_phone_numbers_[phone_number] = user_id; // negative cache
return;
}
auto it = resolved_phone_numbers_.find(phone_number);
if (it != resolved_phone_numbers_.end()) {
if (it->second != user_id) {
LOG(ERROR) << "Resolve phone number \"" << phone_number << "\" to " << user_id << ", but have it in "
<< it->second;
}
return;
}
LOG(ERROR) << "Resolve phone number \"" << phone_number << "\" to " << user_id << ", but doesn't have it";
resolved_phone_numbers_[phone_number] = user_id;
}
void ContactsManager::share_phone_number(UserId user_id, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
@ -11085,7 +11165,8 @@ void ContactsManager::on_update_user_phone_number(User *u, UserId user_id, strin
clean_phone_number(phone_number);
if (u->phone_number != phone_number) {
if (!u->phone_number.empty()) {
resolved_phone_numbers_.erase(u->phone_number);
auto erased_count = resolved_phone_numbers_.erase(u->phone_number) > 0;
CHECK(erased_count == 1);
}
u->phone_number = std::move(phone_number);

View File

@ -305,6 +305,10 @@ class ContactsManager final : public Actor {
void on_update_contacts_reset();
UserId search_user_by_phone_number(string phone_number, Promise<Unit> &&promise);
void on_resolved_phone_number(const string &phone_number, UserId user_id);
void share_phone_number(UserId user_id, Promise<Unit> &&promise);
void search_dialogs_nearby(const Location &location, Promise<td_api::object_ptr<td_api::chatsNearby>> &&promise);

View File

@ -784,6 +784,25 @@ class GetChatFilterRequest final : public RequestActor<> {
}
};
class SearchUserByPhoneNumberRequest final : public RequestActor<> {
string phone_number_;
UserId user_id_;
void do_run(Promise<Unit> &&promise) final {
user_id_ = td_->contacts_manager_->search_user_by_phone_number(phone_number_, std::move(promise));
}
void do_send_result() final {
send_result(td_->contacts_manager_->get_user_object(user_id_));
}
public:
SearchUserByPhoneNumberRequest(ActorShared<Td> td, uint64 request_id, string &&phone_number)
: RequestActor(std::move(td), request_id), phone_number_(std::move(phone_number)) {
}
};
class LoadChatsRequest final : public RequestActor<> {
DialogListId dialog_list_id_;
DialogDate offset_;
@ -6663,6 +6682,12 @@ void Td::on_request(uint64 id, const td_api::clearImportedContacts &request) {
contacts_manager_->clear_imported_contacts(std::move(promise));
}
void Td::on_request(uint64 id, td_api::searchUserByPhoneNumber &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.phone_number_);
CREATE_REQUEST(SearchUserByPhoneNumberRequest, std::move(request.phone_number_));
}
void Td::on_request(uint64 id, const td_api::sharePhoneNumber &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();

View File

@ -966,6 +966,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::clearImportedContacts &request);
void on_request(uint64 id, td_api::searchUserByPhoneNumber &request);
void on_request(uint64 id, const td_api::sharePhoneNumber &request);
void on_request(uint64 id, const td_api::getRecentInlineBots &request);

View File

@ -2049,6 +2049,10 @@ class CliClient final : public Actor {
get_args(args, user_id, first_name, last_name);
send_request(td_api::make_object<td_api::addContact>(
td_api::make_object<td_api::contact>(string(), first_name, last_name, string(), user_id), false));
} else if (op == "subpn") {
string phone_number;
get_args(args, phone_number);
send_request(td_api::make_object<td_api::searchUserByPhoneNumber>(phone_number));
} else if (op == "spn") {
UserId user_id;
get_args(args, user_id);