Better documents cleanup

This commit is contained in:
Andrea Cavalli 2020-10-21 22:17:07 +02:00
parent 6babc009d7
commit 5cff13f2ae

View File

@ -54,10 +54,11 @@ tl_object_ptr<td_api::document> DocumentsManager::get_document_object(FileId fil
} }
LOG(INFO) << "Return document " << file_id << " object"; LOG(INFO) << "Return document " << file_id << " object";
auto &document = documents_[file_id]; auto document_it = documents_.find(file_id);
if (document == nullptr) { if (document_it == documents_.end() || document_it->second == nullptr) {
return nullptr; return nullptr;
} }
auto &document = document_it->second;
LOG_CHECK(document != nullptr) << tag("file_id", file_id); LOG_CHECK(document != nullptr) << tag("file_id", file_id);
document->is_changed = false; document->is_changed = false;
return make_tl_object<td_api::document>( return make_tl_object<td_api::document>(
@ -629,11 +630,11 @@ FileId DocumentsManager::get_document_thumbnail_file_id(FileId file_id) const {
} }
void DocumentsManager::delete_document_thumbnail(FileId file_id) { void DocumentsManager::delete_document_thumbnail(FileId file_id) {
auto &document = documents_[file_id]; auto document_it = documents_.find(file_id);
if (document == nullptr) { if (document_it == documents_.end() || document_it->second == nullptr) {
return; return;
} }
document->thumbnail = PhotoSize(); document_it->second->thumbnail = PhotoSize();
} }
FileId DocumentsManager::dup_document(FileId new_id, FileId old_id) { FileId DocumentsManager::dup_document(FileId new_id, FileId old_id) {
@ -662,7 +663,9 @@ bool DocumentsManager::merge_documents(FileId new_id, FileId old_id, bool can_de
auto new_it = documents_.find(new_id); auto new_it = documents_.find(new_id);
if (new_it == documents_.end() || new_it->second == nullptr) { if (new_it == documents_.end() || new_it->second == nullptr) {
auto &old = documents_[old_id]; auto old_it = documents_.find(old_id);
if (old_it != documents_.end() && old_it->second != nullptr) {
auto &old = old_it->second;
old->is_changed = true; old->is_changed = true;
if (!can_delete_old) { if (!can_delete_old) {
dup_document(new_id, old_id); dup_document(new_id, old_id);
@ -670,6 +673,7 @@ bool DocumentsManager::merge_documents(FileId new_id, FileId old_id, bool can_de
old->file_id = new_id; old->file_id = new_id;
documents_.emplace(new_id, std::move(old)); documents_.emplace(new_id, std::move(old));
} }
}
} else { } else {
GeneralDocument *new_ = new_it->second.get(); GeneralDocument *new_ = new_it->second.get();
CHECK(new_ != nullptr); CHECK(new_ != nullptr);