diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 820d641f0..4c26555ed 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -2123,7 +2123,12 @@ tl_object_ptr get_input_media(const MessageContent *co } tl_object_ptr get_input_media(const MessageContent *content, Td *td, int32 ttl) { - return get_input_media(content, td, nullptr, nullptr, ttl); + auto input_media = get_input_media(content, td, nullptr, nullptr, ttl); + auto file_reference = FileManager::extract_file_reference(input_media); + if (file_reference == FullRemoteFileLocation::invalid_file_reference()) { + return nullptr; + } + return input_media; } void delete_message_content_thumbnail(MessageContent *content, Td *td) { diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 059c66b67..29767fcc8 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -15362,12 +15362,14 @@ void MessagesManager::on_message_media_uploaded(DialogId dialog_id, Message *m, bool was_thumbnail_uploaded = FileManager::extract_was_thumbnail_uploaded(input_media); LOG(INFO) << "Edit media from " << message_id << " in " << dialog_id; - auto promise = PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, message_id, file_id, thumbnail_file_id, - generation = m->edit_generation, was_uploaded, - was_thumbnail_uploaded](Result result) { - send_closure(actor_id, &MessagesManager::on_message_media_edited, dialog_id, message_id, file_id, - thumbnail_file_id, was_uploaded, was_thumbnail_uploaded, generation, std::move(result)); - }); + auto promise = PromiseCreator::lambda( + [actor_id = actor_id(this), dialog_id, message_id, file_id, thumbnail_file_id, generation = m->edit_generation, + was_uploaded, was_thumbnail_uploaded, + file_reference = FileManager::extract_file_reference(input_media)](Result result) mutable { + send_closure(actor_id, &MessagesManager::on_message_media_edited, dialog_id, message_id, file_id, + thumbnail_file_id, was_uploaded, was_thumbnail_uploaded, std::move(file_reference), generation, + std::move(result)); + }); send_closure(td_->create_net_actor(std::move(promise)), &EditMessageActor::send, 1 << 11, dialog_id, message_id, caption == nullptr ? "" : caption->text, get_input_message_entities(td_->contacts_manager_.get(), caption, "edit_message_media"), @@ -16296,7 +16298,7 @@ void MessagesManager::cancel_edit_message_media(DialogId dialog_id, Message *m) void MessagesManager::on_message_media_edited(DialogId dialog_id, MessageId message_id, FileId file_id, FileId thumbnail_file_id, bool was_uploaded, bool was_thumbnail_uploaded, - uint64 generation, Result &&result) { + string file_reference, uint64 generation, Result &&result) { CHECK(message_id.is_server()); auto m = get_message({dialog_id, message_id}); if (m == nullptr || m->edit_generation != generation) { @@ -16316,7 +16318,6 @@ void MessagesManager::on_message_media_edited(DialogId dialog_id, MessageId mess m->content->get_type() == MessageContentType::Photo; update_message_content(dialog_id, m, std::move(m->edited_content), need_send_update_message_content, true); } else { - auto error_message = result.error().message(); if (was_uploaded) { if (was_thumbnail_uploaded) { CHECK(thumbnail_file_id.is_valid()); @@ -16324,6 +16325,7 @@ void MessagesManager::on_message_media_edited(DialogId dialog_id, MessageId mess td_->file_manager_->delete_partial_remote_location(thumbnail_file_id); } CHECK(file_id.is_valid()); + auto error_message = result.error().message(); if (begins_with(error_message, "FILE_PART_") && ends_with(error_message, "_MISSING")) { do_send_message(dialog_id, m, {to_integer(error_message.substr(10))}); return; @@ -16332,6 +16334,14 @@ void MessagesManager::on_message_media_edited(DialogId dialog_id, MessageId mess if (result.error().code() != 429 && result.error().code() < 500 && !G()->close_flag()) { td_->file_manager_->delete_partial_remote_location(file_id); } + } else if (FileReferenceManager::is_file_reference_error(result.error())) { + if (file_id.is_valid()) { + td_->file_manager_->delete_file_reference(file_id, file_reference); + do_send_message(dialog_id, m, {-1}); + return; + } else { + LOG(ERROR) << "Receive file reference error, but have no file_id"; + } } cancel_upload_message_content_files(m->edited_content.get()); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index da8366dc7..0d50997e7 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1269,7 +1269,7 @@ class MessagesManager : public Actor { void cancel_edit_message_media(DialogId dialog_id, Message *m); void on_message_media_edited(DialogId dialog_id, MessageId message_id, FileId file_id, FileId thumbnail_file_id, - bool was_uploaded, bool was_thumbnail_uploaded, uint64 generation, + bool was_uploaded, bool was_thumbnail_uploaded, string file_reference, uint64 generation, Result &&result); MessageId get_persistent_message_id(const Dialog *d, MessageId message_id) const; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index d9c7760d2..7d2357570 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -2104,11 +2104,14 @@ void FileManager::run_upload(FileNodePtr node, std::vector bad_parts) { return; } + bad_parts.erase(std::remove_if(bad_parts.begin(), bad_parts.end(), [](auto part_id) { return part_id < 0; }), + bad_parts.end()); + QueryId id = queries_container_.create(Query{file_id, Query::Upload}); node->upload_id_ = id; + auto new_priority = narrow_cast(bad_parts.empty() ? -priority : priority); send_closure(file_load_manager_, &FileLoadManager::upload, id, node->local_, node->remote_, - file_view.expected_size(true), node->encryption_key_, - narrow_cast(bad_parts.empty() ? -priority : priority), std::move(bad_parts)); + file_view.expected_size(true), node->encryption_key_, new_priority, std::move(bad_parts)); LOG(INFO) << "File " << file_id << " upload request has sent to FileLoadManager"; } @@ -2471,6 +2474,30 @@ bool FileManager::extract_was_thumbnail_uploaded(const tl_object_ptr(input_media.get())->thumb_ != nullptr; } +string FileManager::extract_file_reference(const tl_object_ptr &input_media) { + if (input_media != nullptr) { + switch (input_media->get_id()) { + case telegram_api::inputMediaDocument::ID: { + const auto *id = static_cast(input_media.get())->id_.get(); + CHECK(id != nullptr); + if (id->get_id() == telegram_api::inputDocument::ID) { + return static_cast(id)->file_reference_.as_slice().str(); + } + break; + } + case telegram_api::inputMediaPhoto::ID: { + const auto *id = static_cast(input_media.get())->id_.get(); + CHECK(id != nullptr); + if (id->get_id() == telegram_api::inputPhoto::ID) { + return static_cast(id)->file_reference_.as_slice().str(); + } + break; + } + } + } + return string(); +} + FileId FileManager::next_file_id() { if (!empty_file_ids_.empty()) { auto res = empty_file_ids_.back(); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 2ee80ef4b..9c4a05aa1 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -394,6 +394,7 @@ class FileManager : public FileLoadManager::Callback { static bool extract_was_uploaded(const tl_object_ptr &input_media); static bool extract_was_thumbnail_uploaded(const tl_object_ptr &input_media); + static string extract_file_reference(const tl_object_ptr &input_media); template void store_file(FileId file_id, T &storer, int32 ttl = 5) const;