diff --git a/CMakeLists.txt b/CMakeLists.txt index 54b9ed829..acd70dc01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -506,6 +506,7 @@ set(TDLIB_SOURCE td/telegram/DialogSource.h td/telegram/Document.h td/telegram/DocumentsManager.h + td/telegram/DownloadManager.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 bdde2112a..52a009477 100644 --- a/td/telegram/DownloadManager.cpp +++ b/td/telegram/DownloadManager.cpp @@ -4,10 +4,12 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include "DownloadManager.h" -#include "td/utils/algorithm.h" +#include "td/telegram/DownloadManager.h" + #include "td/utils/FlatHashMap.h" + namespace td { + class DownloadManagerImpl final : public DownloadManager { public: void set_callback(unique_ptr callback) final { @@ -94,7 +96,7 @@ class DownloadManagerImpl final : public DownloadManager { return Status::OK(); } - Status add_file(FileId file_id, FileSourceId file_source_id, std::string search_by, int8 priority) final { + Status add_file(FileId file_id, FileSourceId file_source_id, string search_by, int8 priority) final { if (!callback_) { return Status::Error("TODO: code and message`"); } @@ -116,7 +118,7 @@ class DownloadManagerImpl final : public DownloadManager { return Status::OK(); } - void search(std::string query, bool only_active, bool only_completed, std::string offset, int32 limit, + void search(string query, bool only_active, bool only_completed, string offset, int32 limit, Promise promise) final { if (!callback_) { return promise.set_error(Status::Error("TODO: code and message`")); @@ -138,11 +140,11 @@ class DownloadManagerImpl final : public DownloadManager { CHECK(it != active_files_.end()); auto &file_info = it->second; counters_.downloaded_size -= file_info.downloaded_size; - counters_.total_count -= file_info.size; + counters_.total_size -= file_info.size; file_info.size = size; file_info.downloaded_size = download_size; counters_.downloaded_size += file_info.downloaded_size; - counters_.total_count += file_info.size; + counters_.total_size += file_info.size; file_info.is_paused = is_paused; if (download_size == size) { @@ -184,14 +186,14 @@ class DownloadManagerImpl final : public DownloadManager { Counters counters_; - void loop() override { + void loop() final { if (!callback_) { return; } // TODO: ??? // TODO: load active files from db } - void tear_down() override { + void tear_down() final { callback_.reset(); } }; @@ -199,7 +201,9 @@ class DownloadManagerImpl final : public DownloadManager { unique_ptr DownloadManager::create() { return make_unique(); } + tl_object_ptr DownloadManager::FoundFileDownloads::to_td_api() const { return make_tl_object(); } + } // namespace td diff --git a/td/telegram/DownloadManager.h b/td/telegram/DownloadManager.h index 4658003d1..db0518e43 100644 --- a/td/telegram/DownloadManager.h +++ b/td/telegram/DownloadManager.h @@ -5,15 +5,20 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once -#include "td/actor/actor.h" -#include "td/actor/PromiseFuture.h" + #include "td/telegram/files/FileId.h" #include "td/telegram/files/FileSourceId.h" #include "td/telegram/td_api.h" + +#include "td/actor/actor.h" +#include "td/actor/PromiseFuture.h" + #include "td/utils/common.h" +#include "td/utils/Status.h" namespace td { -class DownloadManager : public td::Actor { + +class DownloadManager : public Actor { public: // creates, but do not stats the actor static td::unique_ptr create(); @@ -34,8 +39,8 @@ class DownloadManager : public td::Actor { struct FoundFileDownloads { int32 total_count{}; - std::vector file_downloads; - std::string offset; + vector file_downloads; + string offset; tl_object_ptr to_td_api() const; }; @@ -49,8 +54,8 @@ class DownloadManager : public td::Actor { virtual void delete_file(FileId file_id) = 0; virtual FileId dup_file_id(FileId file_id) = 0; - virtual std::string get_unique_file_id(FileId file_id) = 0; - virtual std::string get_file_source_serialized(FileSourceId file_source_id) = 0; + virtual string get_unique_file_id(FileId file_id) = 0; + virtual string get_file_source_serialized(FileSourceId file_source_id) = 0; }; // @@ -65,8 +70,8 @@ class DownloadManager : public td::Actor { virtual Status remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) = 0; virtual Status remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) = 0; // Files are always added in is_paused = false state - virtual Status add_file(FileId file_id, FileSourceId file_source_id, std::string search_by, int8 priority) = 0; - virtual void search(std::string query, bool only_active, bool only_completed, std::string offset, int32 limit, + virtual Status add_file(FileId file_id, FileSourceId file_source_id, string search_by, int8 priority) = 0; + virtual void search(std::string query, bool only_active, bool only_completed, string offset, int32 limit, Promise promise) = 0; // @@ -75,4 +80,5 @@ class DownloadManager : public td::Actor { virtual void update_file_download_state(FileId file_id, int64 download_size, int64 size, bool is_paused) = 0; virtual void update_file_deleted(FileId file_id) = 0; }; -}; // namespace td + +} // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index ac47107cf..b2ef966bb 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -22822,16 +22822,18 @@ void MessagesManager::delete_bot_command_message_id(DialogId dialog_id, MessageI } } -FileSourceId MessagesManager::get_message_file_source_id(FullMessageId full_message_id) { - if (td_->auth_manager_->is_bot()) { - return FileSourceId(); - } +FileSourceId MessagesManager::get_message_file_source_id(FullMessageId full_message_id, bool force) { + if (!force) { + if (td_->auth_manager_->is_bot()) { + return FileSourceId(); + } - auto dialog_id = full_message_id.get_dialog_id(); - auto message_id = full_message_id.get_message_id(); - if (!dialog_id.is_valid() || !(message_id.is_valid() || message_id.is_valid_scheduled()) || - dialog_id.get_type() == DialogType::SecretChat || !message_id.is_any_server()) { - return FileSourceId(); + auto dialog_id = full_message_id.get_dialog_id(); + auto message_id = full_message_id.get_message_id(); + if (!dialog_id.is_valid() || !(message_id.is_valid() || message_id.is_valid_scheduled()) || + dialog_id.get_type() == DialogType::SecretChat || !message_id.is_any_server()) { + return FileSourceId(); + } } auto &file_source_id = full_message_id_to_file_source_id_[full_message_id]; @@ -39799,21 +39801,16 @@ void MessagesManager::get_current_state(vector> promise) { - auto message = get_message_force(full_message_id, "add message file to downloads"); - if (!message) { - promise.set_error(Status::Error(400, "Can't find message")); - return; + auto m = get_message_force(full_message_id, "add_message_file_to_downloads"); + if (m == nullptr) { + return promise.set_error(Status::Error(400, "Message not found")); } - if (!contains(get_message_file_ids(message), file_id)) { - promise.set_error(Status::Error(400, "Can't find file in message")); - return; - } - auto search_text = get_message_search_text(message); - auto file_source_id = get_message_file_source_id(full_message_id); - if (!file_source_id.is_valid()) { - promise.set_error(Status::Error(400, "Can't get file source")); - return; + if (!contains(get_message_file_ids(m), file_id)) { + return promise.set_error(Status::Error(400, "Message has no specified file")); } + auto search_text = get_message_search_text(m); + auto file_source_id = get_message_file_source_id(full_message_id, true); + CHECK(file_source_id.is_valid()); TRY_STATUS_PROMISE(promise, td_->download_manager_->add_file(file_id, file_source_id, std::move(search_text), static_cast(priority))); promise.set_value(td_->file_manager_->get_file_object(file_id)); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 45132c098..02046978a 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -922,7 +922,7 @@ class MessagesManager final : public Actor { vector> users, vector> chats); - FileSourceId get_message_file_source_id(FullMessageId full_message_id); + FileSourceId get_message_file_source_id(FullMessageId full_message_id, bool force = false); struct MessagePushNotificationInfo { NotificationGroupId group_id; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3ef391682..fe9398590 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3975,7 +3975,7 @@ void Td::init_managers() { // TODO: move this callback somewhere else class DownloadManagerCallback final : public DownloadManager::Callback { public: - DownloadManagerCallback(ActorShared<> parent) : parent_(std::move(parent)) { + explicit DownloadManagerCallback(ActorShared<> parent) : parent_(std::move(parent)) { // TODO } void update_counters(DownloadManager::Counters counters) final { @@ -3996,11 +3996,11 @@ void Td::init_managers() { } string get_unique_file_id(FileId file_id) final { // TODO - return std::string(); + return string(); } string get_file_source_serialized(FileSourceId file_source_id) final { // TODO - return std::string(); + return string(); } private: @@ -6391,7 +6391,7 @@ void Td::on_request(uint64 id, const td_api::clearAllDraftMessages &request) { void Td::on_request(uint64 id, const td_api::downloadFile &request) { auto priority = request.priority_; if (!(1 <= priority && priority <= 32)) { - return send_error_raw(id, 400, "Download priority must be in [1;32] range"); + return send_error_raw(id, 400, "Download priority must be between 1 and 32"); } auto offset = request.offset_; if (offset < 0) { @@ -6489,7 +6489,7 @@ void Td::on_request(uint64 id, const td_api::getSuggestedFileName &request) { void Td::on_request(uint64 id, td_api::uploadFile &request) { auto priority = request.priority_; if (!(1 <= priority && priority <= 32)) { - return send_error_raw(id, 400, "Upload priority must be in [1;32] range"); + return send_error_raw(id, 400, "Upload priority must be between 1 and 32"); } auto file_type = request.file_type_ == nullptr ? FileType::Temp : get_file_type(*request.file_type_); @@ -6550,10 +6550,10 @@ void Td::on_request(uint64 id, const td_api::deleteFile &request) { } void Td::on_request(uint64 id, const td_api::addFileToDownloads &request) { - CREATE_REQUEST_PROMISE(); if (!(1 <= request.priority_ && request.priority_ <= 32)) { - promise.set_error(Status::Error(400, "Upload priority must be in [1;32] range")); + return send_error_raw(id, 400, "Download priority must be between 1 and 32"); } + CREATE_REQUEST_PROMISE(); messages_manager_->add_message_file_to_downloads( FullMessageId(DialogId(request.chat_id_), MessageId(request.message_id_)), FileId(request.file_id_, 0), request.priority_, std::move(promise)); @@ -6590,7 +6590,6 @@ void Td::on_request(uint64 id, td_api::searchFileDownloads &request) { }; send_closure(download_manager_actor_, &DownloadManager::search, std::move(request.query_), request.only_active_, request.only_completed_, std::move(request.offset_), request.limit_, std::move(wrapped_promise)); - promise.set_error(Status::Error(500, "Unsupported")); } void Td::on_request(uint64 id, td_api::getMessageFileType &request) { diff --git a/tdutils/td/utils/FlatHashMapChunks.h b/tdutils/td/utils/FlatHashMapChunks.h index 4566a6769..7ee155ef7 100644 --- a/tdutils/td/utils/FlatHashMapChunks.h +++ b/tdutils/td/utils/FlatHashMapChunks.h @@ -12,6 +12,7 @@ #include "td/utils/FlatHashMapLinear.h" #include +#include #include #include #include