From 4524ba0380e726ef044188f310792c176771758d Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 26 Feb 2022 21:52:14 +0300 Subject: [PATCH] Add FileReferenceManager::get_file_info. --- CMakeLists.txt | 1 + td/telegram/DownloadManager.cpp | 8 ++++---- td/telegram/DownloadsDb.cpp | 2 +- td/telegram/FileReferenceManager.cpp | 12 ++++++++++++ td/telegram/FileReferenceManager.h | 7 +++++++ td/telegram/MessagesManager.cpp | 19 +++++++++++++++++++ td/telegram/MessagesManager.h | 3 +++ 7 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 222165fdc..71668a6e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -508,6 +508,7 @@ set(TDLIB_SOURCE td/telegram/Document.h td/telegram/DocumentsManager.h td/telegram/DownloadManager.h + td/telegram/DownloadsDb.h td/telegram/DraftMessage.h td/telegram/EncryptedFile.h td/telegram/FileReferenceManager.h diff --git a/td/telegram/DownloadManager.cpp b/td/telegram/DownloadManager.cpp index 23bd2b855..5cfe3b2fc 100644 --- a/td/telegram/DownloadManager.cpp +++ b/td/telegram/DownloadManager.cpp @@ -6,12 +6,12 @@ // #include "td/telegram/DownloadManager.h" -#include "td/utils/FlatHashMap.h" - #include "td/telegram/DownloadsDb.h" #include "td/telegram/Global.h" #include "td/telegram/TdDb.h" +#include "td/utils/FlatHashMap.h" + namespace td { class DownloadManagerImpl final : public DownloadManager { @@ -50,7 +50,7 @@ class DownloadManagerImpl final : public DownloadManager { return Status::Error("TODO: code and message`"); } for (auto &it : active_files_) { - toggle_is_paused(it.key(), is_paused); + toggle_is_paused(it.first, is_paused); } // TODO: update db @@ -132,7 +132,7 @@ class DownloadManagerImpl final : public DownloadManager { TRY_RESULT_PROMISE(promise, offset_int64, to_integer_safe(offset)); // TODO: only active, only completed G()->td_db()->get_downloads_db_async()->get_downloads_fts(DownloadsDbFtsQuery{query, offset_int64, limit}, - [](Result) {}); + [](Result) {}); return promise.set_value({}); } diff --git a/td/telegram/DownloadsDb.cpp b/td/telegram/DownloadsDb.cpp index 62174ce82..c0c36ac1d 100644 --- a/td/telegram/DownloadsDb.cpp +++ b/td/telegram/DownloadsDb.cpp @@ -247,7 +247,7 @@ class DownloadsDbAsync final : public DownloadsDbAsyncInterface { promise.set_result(sync_db_->get_downloads_fts(std::move(query))); } - void get_active_downloads(Promise<> promise) { + void get_active_downloads(Promise promise) { add_read_query(); promise.set_result(sync_db_->get_active_downloads()); } diff --git a/td/telegram/FileReferenceManager.cpp b/td/telegram/FileReferenceManager.cpp index 4b0dbadb3..032003161 100644 --- a/td/telegram/FileReferenceManager.cpp +++ b/td/telegram/FileReferenceManager.cpp @@ -394,4 +394,16 @@ void FileReferenceManager::reload_photo(PhotoSizeSource source, Promise pr } } +void FileReferenceManager::get_file_info(FileSourceId file_source_id, string unique_file_id, + Promise promise) { + auto index = static_cast(file_source_id.get()) - 1; + CHECK(index < file_sources_.size()); + file_sources_[index].visit(overloaded( + [&](const FileSourceMessage &source) { + send_closure_later(G()->messages_manager(), &MessagesManager::get_message_file_info, source.full_message_id, + std::move(unique_file_id), std::move(promise)); + }, + [&](const auto &source) { promise.set_error(Status::Error(500, "Unsupported file source")); })); +} + } // namespace td diff --git a/td/telegram/FileReferenceManager.h b/td/telegram/FileReferenceManager.h index a103842d6..7f0a2ada9 100644 --- a/td/telegram/FileReferenceManager.h +++ b/td/telegram/FileReferenceManager.h @@ -32,6 +32,11 @@ class Td; extern int VERBOSITY_NAME(file_references); +struct FileSearchInfo { + FileId file_id; + string search_text; +}; + class FileReferenceManager final : public Actor { public: static bool is_file_reference_error(const Status &error); @@ -55,6 +60,8 @@ class FileReferenceManager final : public Actor { using NodeId = FileId; void repair_file_reference(NodeId node_id, Promise<> promise); + void get_file_info(FileSourceId file_source_id, string unique_file_id, Promise promise); + static void reload_photo(PhotoSizeSource source, Promise promise); bool add_file_source(NodeId node_id, FileSourceId file_source_id); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index b2ef966bb..3a6b40164 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -39816,4 +39816,23 @@ void MessagesManager::add_message_file_to_downloads(FullMessageId full_message_i promise.set_value(td_->file_manager_->get_file_object(file_id)); } +void MessagesManager::get_message_file_info(FullMessageId full_message_id, string unique_file_id, + Promise promise) { + auto m = get_message_force(full_message_id, "add_message_file_to_downloads"); + if (m == nullptr) { + return promise.set_error(Status::Error(200, "Message not found")); + } + for (auto file_id : get_message_file_ids(m)) { + auto file_view = td_->file_manager_->get_file_view(file_id); + CHECK(!file_view.empty()); + if (file_view.get_unique_file_id() == unique_file_id) { + FileSearchInfo info; + info.file_id = file_id; + info.search_text = get_message_search_text(m); + return promise.set_value(std::move(info)); + } + } + return promise.set_error(Status::Error(200, "File not found")); +} + } // namespace td diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 02046978a..418e53563 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -90,6 +90,7 @@ struct Dependencies; class DialogActionBar; class DialogFilter; class DraftMessage; +struct FileSearchInfo; struct InputMessageContent; class MessageContent; struct MessageReactions; @@ -983,6 +984,8 @@ class MessagesManager final : public Actor { void add_message_file_to_downloads(FullMessageId full_message_id, FileId file_id, int32 priority, Promise> promise); + void get_message_file_info(FullMessageId full_message_id, string unique_file_id, Promise promise); + private: class PendingPtsUpdate { public: