Fix clearImportedContacts and getImportedContactCount.

GitOrigin-RevId: 295c524eab124ee19284841b2b6aa3083c850446
This commit is contained in:
levlam 2018-05-25 00:37:43 +03:00
parent ef01eba051
commit 266722112e
6 changed files with 50 additions and 11 deletions

View File

@ -2769,7 +2769,7 @@ getImportedContactCount = Count;
//-Query result depends on the result of the previous query, so only one query is possible at the same time @contacts The new list of contacts //-Query result depends on the result of the previous query, so only one query is possible at the same time @contacts The new list of contacts
changeImportedContacts contacts:vector<contact> = ImportedContacts; changeImportedContacts contacts:vector<contact> = ImportedContacts;
//@description Clears all imported contacts //@description Clears all imported contacts, contacts list remains unchanged
clearImportedContacts = Ok; clearImportedContacts = Ok;

View File

@ -3501,7 +3501,7 @@ int32 ContactsManager::get_imported_contact_count(Promise<Unit> &&promise) {
reload_contacts(false); reload_contacts(false);
promise.set_value(Unit()); promise.set_value(Unit());
return saved_contact_count_ + static_cast<int32>(contacts_hints_.size()); return saved_contact_count_;
} }
void ContactsManager::load_imported_contacts(Promise<Unit> &&promise) { void ContactsManager::load_imported_contacts(Promise<Unit> &&promise) {
@ -3533,6 +3533,10 @@ void ContactsManager::load_imported_contacts(Promise<Unit> &&promise) {
void ContactsManager::on_load_imported_contacts_from_database(string value) { void ContactsManager::on_load_imported_contacts_from_database(string value) {
CHECK(!are_imported_contacts_loaded_); CHECK(!are_imported_contacts_loaded_);
if (need_clear_imported_contacts_) {
need_clear_imported_contacts_ = false;
value.clear();
}
if (value.empty()) { if (value.empty()) {
CHECK(all_imported_contacts_.empty()); CHECK(all_imported_contacts_.empty());
} else { } else {
@ -3565,6 +3569,10 @@ void ContactsManager::on_load_imported_contacts_finished() {
get_user_id_object(contact.get_user_id(), "on_load_imported_contacts_finished"); // to ensure updateUser get_user_id_object(contact.get_user_id(), "on_load_imported_contacts_finished"); // to ensure updateUser
} }
if (need_clear_imported_contacts_) {
need_clear_imported_contacts_ = false;
all_imported_contacts_.clear();
}
are_imported_contacts_loaded_ = true; are_imported_contacts_loaded_ = true;
auto promises = std::move(load_imported_contacts_queries_); auto promises = std::move(load_imported_contacts_queries_);
load_imported_contacts_queries_.clear(); load_imported_contacts_queries_.clear();
@ -3588,6 +3596,15 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contac
<< " contacts with random_id = " << random_id; << " contacts with random_id = " << random_id;
if (random_id != 0) { if (random_id != 0) {
// request has already been sent before // request has already been sent before
if (need_clear_imported_contacts_) {
need_clear_imported_contacts_ = false;
all_imported_contacts_.clear();
if (G()->parameters().use_chat_info_db) {
G()->td_db()->get_sqlite_pmc()->erase("user_imported_contacts", Auto());
}
reload_contacts(true);
}
CHECK(are_imported_contacts_changing_); CHECK(are_imported_contacts_changing_);
are_imported_contacts_changing_ = false; are_imported_contacts_changing_ = false;
@ -3697,12 +3714,7 @@ void ContactsManager::on_clear_imported_contacts(vector<Contact> &&contacts, vec
void ContactsManager::clear_imported_contacts(Promise<Unit> &&promise) { void ContactsManager::clear_imported_contacts(Promise<Unit> &&promise) {
LOG(INFO) << "Delete imported contacts"; LOG(INFO) << "Delete imported contacts";
if (!are_contacts_loaded_ || saved_contact_count_ == -1) { if (saved_contact_count_ == 0) {
load_contacts(std::move(promise));
return;
}
if (contacts_hints_.size() == 0 && saved_contact_count_ == 0) {
promise.set_value(Unit()); promise.set_value(Unit());
return; return;
} }
@ -3711,6 +3723,7 @@ void ContactsManager::clear_imported_contacts(Promise<Unit> &&promise) {
} }
void ContactsManager::on_update_contacts_reset() { void ContactsManager::on_update_contacts_reset() {
/*
UserId my_id = get_my_id("on_update_contacts_reset"); UserId my_id = get_my_id("on_update_contacts_reset");
for (auto &p : users_) { for (auto &p : users_) {
UserId user_id = p.first; UserId user_id = p.first;
@ -3729,8 +3742,30 @@ void ContactsManager::on_update_contacts_reset() {
} }
} }
} }
*/
saved_contact_count_ = 0; saved_contact_count_ = 0;
if (G()->parameters().use_chat_info_db) {
G()->td_db()->get_binlog_pmc()->set("saved_contact_count", "0");
G()->td_db()->get_sqlite_pmc()->erase("user_imported_contacts", Auto());
}
if (!are_imported_contacts_loaded_) {
CHECK(all_imported_contacts_.empty());
if (load_imported_contacts_queries_.empty()) {
LOG(INFO) << "Imported contacts was never loaded, just clear them";
} else {
LOG(INFO) << "Imported contacts are being loaded, clear them also when they will be loaded";
need_clear_imported_contacts_ = true;
}
} else {
if (!are_imported_contacts_changing_) {
LOG(INFO) << "Imported contacts was loaded, but aren't changing now, just clear them";
all_imported_contacts_.clear();
} else {
LOG(INFO) << "Imported contacts are changing now, clear them also after they will be loaded";
need_clear_imported_contacts_ = true;
}
}
reload_contacts(true); reload_contacts(true);
} }

View File

@ -1078,6 +1078,7 @@ class ContactsManager : public Actor {
MultiPromiseActor load_imported_contact_users_multipromise_; MultiPromiseActor load_imported_contact_users_multipromise_;
vector<Contact> all_imported_contacts_; vector<Contact> all_imported_contacts_;
bool are_imported_contacts_changing_ = false; bool are_imported_contacts_changing_ = false;
bool need_clear_imported_contacts_ = false;
vector<Contact> next_all_imported_contacts_; vector<Contact> next_all_imported_contacts_;
vector<size_t> imported_contacts_unique_id_; vector<size_t> imported_contacts_unique_id_;

View File

@ -2426,14 +2426,13 @@ class ChangeImportedContactsRequest : public RequestActor<> {
} }
}; };
class ClearImportedContactsRequest : public RequestActor<> { class ClearImportedContactsRequest : public RequestOnceActor {
void do_run(Promise<Unit> &&promise) override { void do_run(Promise<Unit> &&promise) override {
td->contacts_manager_->clear_imported_contacts(std::move(promise)); td->contacts_manager_->clear_imported_contacts(std::move(promise));
} }
public: public:
ClearImportedContactsRequest(ActorShared<Td> td, uint64 request_id) : RequestActor(std::move(td), request_id) { ClearImportedContactsRequest(ActorShared<Td> td, uint64 request_id) : RequestOnceActor(std::move(td), request_id) {
set_tries(3); // load_contacts + clear
} }
}; };

View File

@ -17,6 +17,7 @@
#include <unordered_map> #include <unordered_map>
namespace td { namespace td {
class SqliteKeyValue { class SqliteKeyValue {
public: public:
static Status drop(SqliteDb &connection, Slice kv_name) TD_WARN_UNUSED_RESULT { static Status drop(SqliteDb &connection, Slice kv_name) TD_WARN_UNUSED_RESULT {
@ -218,4 +219,5 @@ class SqliteKeyValue {
return string{}; return string{};
} }
}; };
} // namespace td } // namespace td

View File

@ -12,6 +12,7 @@
#include <unordered_map> #include <unordered_map>
namespace td { namespace td {
class SqliteKeyValueAsync : public SqliteKeyValueAsyncInterface { class SqliteKeyValueAsync : public SqliteKeyValueAsyncInterface {
public: public:
explicit SqliteKeyValueAsync(std::shared_ptr<SqliteKeyValueSafe> kv_safe, int32 scheduler_id = -1) { explicit SqliteKeyValueAsync(std::shared_ptr<SqliteKeyValueSafe> kv_safe, int32 scheduler_id = -1) {
@ -133,6 +134,7 @@ class SqliteKeyValueAsync : public SqliteKeyValueAsyncInterface {
}; };
ActorOwn<Impl> impl_; ActorOwn<Impl> impl_;
}; };
std::unique_ptr<SqliteKeyValueAsyncInterface> create_sqlite_key_value_async(std::shared_ptr<SqliteKeyValueSafe> kv, std::unique_ptr<SqliteKeyValueAsyncInterface> create_sqlite_key_value_async(std::shared_ptr<SqliteKeyValueSafe> kv,
int32 scheduler_id) { int32 scheduler_id) {
return std::make_unique<SqliteKeyValueAsync>(std::move(kv), scheduler_id); return std::make_unique<SqliteKeyValueAsync>(std::move(kv), scheduler_id);