From 78a0a37afb9449a3651a387256149d47e84c2136 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 3 Jan 2019 01:39:21 +0300 Subject: [PATCH] Remove contacts by phone number in changeImportedContacts. GitOrigin-RevId: 1ac5998d3d01b289761aa62e131589163e1b5194 --- td/telegram/ContactsManager.cpp | 65 +++++++++++++++++++++++++++++---- td/telegram/ContactsManager.h | 3 ++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 5df209ba..b5a34f63 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -592,6 +592,41 @@ class DeleteContactsQuery : public Td::ResultHandler { } }; +class DeleteContactsByPhoneNumberQuery : public Td::ResultHandler { + Promise promise_; + vector user_ids_; + + public: + explicit DeleteContactsByPhoneNumberQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(vector &&user_phone_numbers, vector &&user_ids) { + user_ids_ = std::move(user_ids); + send_query(G()->net_query_creator().create( + create_storer(telegram_api::contacts_deleteByPhones(std::move(user_phone_numbers))))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + bool result = result_ptr.ok(); + if (!result) { + return on_error(id, Status::Error(500, "Some contacts can't be deleted")); + } + + td->contacts_manager_->on_deleted_contacts(user_ids_); + promise_.set_value(Unit()); + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + td->contacts_manager_->reload_contacts(true); + } +}; + class ResetContactsQuery : public Td::ResultHandler { Promise promise_; @@ -3525,11 +3560,22 @@ void ContactsManager::remove_contacts(vector user_ids, Promise &&p return promise.set_value(Unit()); } - // TODO DeleteContactQuery td_->create_handler(std::move(promise)) ->send(std::move(to_delete_user_ids), std::move(input_users)); } +void ContactsManager::remove_contacts_by_phone_number(vector user_phone_numbers, vector user_ids, + Promise &&promise) { + LOG(INFO) << "Delete contacts by phone number: " << format::as_array(user_phone_numbers); + if (!are_contacts_loaded_) { + load_contacts(std::move(promise)); + return; + } + + td_->create_handler(std::move(promise)) + ->send(std::move(user_phone_numbers), std::move(user_ids)); +} + int32 ContactsManager::get_imported_contact_count(Promise &&promise) { LOG(INFO) << "Get imported contact count"; @@ -3690,13 +3736,18 @@ std::pair, vector> ContactsManager::change_imported_contac } } - vector to_delete; + vector to_delete; + vector to_delete_user_ids; for (auto &old_contact : all_imported_contacts_) { auto user_id = old_contact.get_user_id(); auto it = different_new_contacts.find(old_contact); if (it == different_new_contacts.end()) { - if (user_id.is_valid() && different_new_phone_numbers.count(old_contact.get_phone_number()) == 0) { - to_delete.push_back(user_id); + auto phone_number = old_contact.get_phone_number(); + if (different_new_phone_numbers.count(phone_number) == 0) { + to_delete.push_back(std::move(phone_number)); + if (user_id.is_valid()) { + to_delete_user_ids.push_back(user_id); + } } } else { unique_new_contacts[it->second].set_user_id(user_id); @@ -3723,8 +3774,8 @@ std::pair, vector> ContactsManager::change_imported_contac are_imported_contacts_changing_ = true; random_id = 1; - remove_contacts( - std::move(to_delete), + remove_contacts_by_phone_number( + std::move(to_delete), std::move(to_delete_user_ids), PromiseCreator::lambda([new_contacts = std::move(unique_new_contacts), new_contacts_unique_id = std::move(new_contacts_unique_id), to_add = std::move(to_add), promise = std::move(promise)](Result<> result) mutable { @@ -4781,7 +4832,7 @@ void ContactsManager::on_imported_contacts(int64 random_id, vector impor } void ContactsManager::on_deleted_contacts(const vector &deleted_contact_user_ids) { - LOG(INFO) << "Contacts deletion has finished"; + LOG(INFO) << "Contacts deletion has finished for " << deleted_contact_user_ids; for (auto user_id : deleted_contact_user_ids) { LOG(INFO) << "Drop contact with " << user_id; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 6a2dfff5..45119ab5 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -265,6 +265,9 @@ class ContactsManager : public Actor { void remove_contacts(vector user_ids, Promise &&promise); + void remove_contacts_by_phone_number(vector user_phone_numbers, vector user_ids, + Promise &&promise); + int32 get_imported_contact_count(Promise &&promise); std::pair, vector> change_imported_contacts(vector> &&contacts,