diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 0adb385ff..3e4bb9a59 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5350,8 +5350,8 @@ void ContactsManager::add_contact(Contact contact, bool share_phone_number, Prom ->send(user_id, std::move(input_user), contact, share_phone_number); } -std::pair, vector> ContactsManager::import_contacts( - const vector> &contacts, int64 &random_id, Promise &&promise) { +std::pair, vector> ContactsManager::import_contacts(const vector &contacts, + int64 &random_id, Promise &&promise) { if (!are_contacts_loaded_) { load_contacts(std::move(promise)); return {}; @@ -5368,25 +5368,13 @@ std::pair, vector> ContactsManager::import_contacts( promise.set_value(Unit()); return result; } - for (auto &contact : contacts) { - if (contact == nullptr) { - promise.set_error(Status::Error(400, "Imported contacts must be non-empty")); - return {}; - } - } do { random_id = Random::secure_int64(); } while (random_id == 0 || imported_contacts_.find(random_id) != imported_contacts_.end()); imported_contacts_[random_id]; // reserve place for result - td_->create_handler(std::move(promise)) - ->send(transform(contacts, - [](const tl_object_ptr &contact) { - return Contact(contact->phone_number_, contact->first_name_, contact->last_name_, string(), - UserId()); - }), - random_id); + td_->create_handler(std::move(promise))->send(contacts, random_id); return {}; } @@ -5523,8 +5511,9 @@ void ContactsManager::on_load_imported_contacts_finished() { } } -std::pair, vector> ContactsManager::change_imported_contacts( - vector> &&contacts, int64 &random_id, Promise &&promise) { +std::pair, vector> ContactsManager::change_imported_contacts(vector &contacts, + int64 &random_id, + Promise &&promise) { if (!are_contacts_loaded_) { load_contacts(std::move(promise)); return {}; @@ -5565,26 +5554,14 @@ std::pair, vector> ContactsManager::change_imported_contac return {}; } - for (auto &contact : contacts) { - if (contact == nullptr) { - promise.set_error(Status::Error(400, "Contacts must be non-empty")); - return {}; - } - } - - auto new_contacts = transform(std::move(contacts), [](tl_object_ptr &&contact) { - return Contact(std::move(contact->phone_number_), std::move(contact->first_name_), std::move(contact->last_name_), - string(), UserId()); - }); - - vector new_contacts_unique_id(new_contacts.size()); + vector new_contacts_unique_id(contacts.size()); vector unique_new_contacts; - unique_new_contacts.reserve(new_contacts.size()); + unique_new_contacts.reserve(contacts.size()); std::unordered_map different_new_contacts; std::unordered_set different_new_phone_numbers; size_t unique_size = 0; - for (size_t i = 0; i < new_contacts.size(); i++) { - auto it_success = different_new_contacts.emplace(std::move(new_contacts[i]), unique_size); + for (size_t i = 0; i < contacts.size(); i++) { + auto it_success = different_new_contacts.emplace(std::move(contacts[i]), unique_size); new_contacts_unique_id[i] = it_success.first->second; if (it_success.second) { unique_new_contacts.push_back(it_success.first->first); @@ -5618,14 +5595,14 @@ std::pair, vector> ContactsManager::change_imported_contac } if (to_add.first.empty() && to_delete.empty()) { - for (size_t i = 0; i < new_contacts.size(); i++) { + for (size_t i = 0; i < contacts.size(); i++) { auto unique_id = new_contacts_unique_id[i]; - new_contacts[i].set_user_id(unique_new_contacts[unique_id].get_user_id()); + contacts[i].set_user_id(unique_new_contacts[unique_id].get_user_id()); } promise.set_value(Unit()); - return {transform(new_contacts, [&](const Contact &contact) { return contact.get_user_id(); }), - vector(new_contacts.size())}; + return {transform(contacts, [&](const Contact &contact) { return contact.get_user_id(); }), + vector(contacts.size())}; } are_imported_contacts_changing_ = true; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 49e615675..9c4095c76 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -290,8 +290,8 @@ class ContactsManager final : public Actor { void add_contact(Contact contact, bool share_phone_number, Promise &&promise); - std::pair, vector> import_contacts(const vector> &contacts, - int64 &random_id, Promise &&promise); + std::pair, vector> import_contacts(const vector &contacts, int64 &random_id, + Promise &&promise); std::pair> search_contacts(const string &query, int32 limit, Promise &&promise); @@ -302,8 +302,8 @@ class ContactsManager final : public Actor { int32 get_imported_contact_count(Promise &&promise); - std::pair, vector> change_imported_contacts(vector> &&contacts, - int64 &random_id, Promise &&promise); + std::pair, vector> change_imported_contacts(vector &contacts, int64 &random_id, + Promise &&promise); void clear_imported_contacts(Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 526b909d4..388a0ea57 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -1925,7 +1925,7 @@ class GetBlockedMessageSendersRequest final : public RequestActor<> { }; class ImportContactsRequest final : public RequestActor<> { - vector> contacts_; + vector contacts_; int64 random_id_; std::pair, vector> imported_contacts_; @@ -1946,7 +1946,7 @@ class ImportContactsRequest final : public RequestActor<> { } public: - ImportContactsRequest(ActorShared td, uint64 request_id, vector> &&contacts) + ImportContactsRequest(ActorShared td, uint64 request_id, vector &&contacts) : RequestActor(std::move(td), request_id), contacts_(std::move(contacts)), random_id_(0) { set_tries(3); // load_contacts + import_contacts } @@ -2003,15 +2003,14 @@ class GetImportedContactCountRequest final : public RequestActor<> { }; class ChangeImportedContactsRequest final : public RequestActor<> { - vector> contacts_; + vector contacts_; size_t contacts_size_; int64 random_id_; std::pair, vector> imported_contacts_; void do_run(Promise &&promise) final { - imported_contacts_ = - td->contacts_manager_->change_imported_contacts(std::move(contacts_), random_id_, std::move(promise)); + imported_contacts_ = td->contacts_manager_->change_imported_contacts(contacts_, random_id_, std::move(promise)); } void do_send_result() final { @@ -2026,8 +2025,7 @@ class ChangeImportedContactsRequest final : public RequestActor<> { } public: - ChangeImportedContactsRequest(ActorShared td, uint64 request_id, - vector> &&contacts) + ChangeImportedContactsRequest(ActorShared td, uint64 request_id, vector &&contacts) : RequestActor(std::move(td), request_id) , contacts_(std::move(contacts)) , contacts_size_(contacts_.size()) @@ -6655,15 +6653,16 @@ void Td::on_request(uint64 id, td_api::addContact &request) { void Td::on_request(uint64 id, td_api::importContacts &request) { CHECK_IS_USER(); + vector contacts; + contacts.reserve(request.contacts_.size()); for (auto &contact : request.contacts_) { - if (contact == nullptr) { - return send_error_raw(id, 400, "Contact must be non-empty"); + auto r_contact = get_contact(std::move(contact)); + if (r_contact.is_error()) { + return send_closure(actor_id(this), &Td::send_error, id, r_contact.move_as_error()); } - CLEAN_INPUT_STRING(contact->phone_number_); - CLEAN_INPUT_STRING(contact->first_name_); - CLEAN_INPUT_STRING(contact->last_name_); + contacts.push_back(r_contact.move_as_ok()); } - CREATE_REQUEST(ImportContactsRequest, std::move(request.contacts_)); + CREATE_REQUEST(ImportContactsRequest, std::move(contacts)); } void Td::on_request(uint64 id, const td_api::getContacts &request) { @@ -6689,15 +6688,16 @@ void Td::on_request(uint64 id, const td_api::getImportedContactCount &request) { void Td::on_request(uint64 id, td_api::changeImportedContacts &request) { CHECK_IS_USER(); + vector contacts; + contacts.reserve(request.contacts_.size()); for (auto &contact : request.contacts_) { - if (contact == nullptr) { - return send_error_raw(id, 400, "Contact must be non-empty"); + auto r_contact = get_contact(std::move(contact)); + if (r_contact.is_error()) { + return send_closure(actor_id(this), &Td::send_error, id, r_contact.move_as_error()); } - CLEAN_INPUT_STRING(contact->phone_number_); - CLEAN_INPUT_STRING(contact->first_name_); - CLEAN_INPUT_STRING(contact->last_name_); + contacts.push_back(r_contact.move_as_ok()); } - CREATE_REQUEST(ChangeImportedContactsRequest, std::move(request.contacts_)); + CREATE_REQUEST(ChangeImportedContactsRequest, std::move(contacts)); } void Td::on_request(uint64 id, const td_api::clearImportedContacts &request) {