Td: proxy queries to DownloadManager

This commit is contained in:
Arseny Smirnov 2022-02-25 13:11:10 +01:00
parent c631c8c356
commit 97b2d3edd2
4 changed files with 53 additions and 28 deletions

View File

@ -39,20 +39,21 @@ class DownloadManagerImpl final : public DownloadManager {
return Status::OK();
}
void toggle_all_is_paused(bool is_paused) final {
Status toggle_all_is_paused(bool is_paused) final {
if (!callback_) {
return;
return Status::Error("TODO: code and message`");
}
for (auto &it : active_files_) {
toggle_is_paused(it.key(), is_paused);
}
// TODO: update db
return Status::OK();
}
void remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) final {
Status remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) final {
if (!callback_) {
return;
return Status::Error("TODO: code and message`");
}
auto it = active_files_.find(file_id);
if (it != active_files_.end() && (!file_source_id.is_valid() || file_source_id == it->second.file_source_id)) {
@ -68,14 +69,15 @@ class DownloadManagerImpl final : public DownloadManager {
active_files_.erase(it);
}
// TODO: remove from db
return Status::OK();
}
void remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) final {
Status remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) final {
if (!callback_) {
return;
return Status::Error("TODO: code and message`");
}
if (!only_completed) {
td::table_remove_if(active_files_, [&](auto &it) {
for (auto &it : active_files_) {
FileInfo &file_info = it.second;
if (!file_info.is_paused) {
callback_->pause_file(file_info.internal_file_id);
@ -83,17 +85,18 @@ class DownloadManagerImpl final : public DownloadManager {
if (delete_from_cache) {
callback_->delete_file(file_info.internal_file_id);
}
return true;
});
}
active_files_.clear();
}
// TODO: remove from db. should respect only_active
// TODO: if delete_from_cache, should iterate all files in db
return Status::OK();
}
void 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, std::string search_by, int8 priority) final {
if (!callback_) {
return;
return Status::Error("TODO: code and message`");
}
FileInfo file_info;
file_info.internal_file_id = callback_->dup_file_id(file_id);
@ -110,15 +113,16 @@ class DownloadManagerImpl final : public DownloadManager {
callback_->start_file(file_info.internal_file_id, file_info.priority);
// TODO: add file to db
return Status::OK();
}
FoundFileDownloads search(std::string query, bool only_active, bool only_completed, std::string offset,
int32 limit) final {
void search(std::string query, bool only_active, bool only_completed, std::string offset, int32 limit,
Promise<FoundFileDownloads> promise) final {
if (!callback_) {
return {};
return promise.set_error(Status::Error("TODO: code and message`"));
}
// TODO: query to database
return FoundFileDownloads();
// TODO: do query
return promise.set_value({});
}
void update_file_download_state(FileId internal_file_id, int64 download_size, int64 size, bool is_paused) final {
@ -195,4 +199,7 @@ class DownloadManagerImpl final : public DownloadManager {
unique_ptr<DownloadManager> DownloadManager::create() {
return make_unique<DownloadManagerImpl>();
}
tl_object_ptr<td_api::foundFileDownloads> DownloadManager::FoundFileDownloads::to_td_api() const {
return make_tl_object<td_api::foundFileDownloads>();
}
} // namespace td

View File

@ -6,8 +6,10 @@
//
#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/utils/common.h"
namespace td {
@ -34,6 +36,7 @@ class DownloadManager : public td::Actor {
int32 total_count{};
std::vector<FileDownload> file_downloads;
std::string offset;
tl_object_ptr<td_api::foundFileDownloads> to_td_api() const;
};
// Trying to make DownloadManager testable, so all interactions with G() will be hidden is this probably monstrous interface
@ -58,13 +61,13 @@ class DownloadManager : public td::Actor {
virtual void set_callback(unique_ptr<Callback> callback) = 0;
virtual Status toggle_is_paused(FileId, bool is_paused) = 0;
virtual void toggle_all_is_paused(bool is_paused) = 0;
virtual void remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) = 0;
virtual void remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) = 0;
virtual Status toggle_all_is_paused(bool is_paused) = 0;
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 void add_file(FileId file_id, FileSourceId file_source_id, std::string search_by, int8 priority) = 0;
virtual FoundFileDownloads search(std::string query, bool only_active, bool only_completed, std::string offset,
int32 limit) = 0;
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,
Promise<FoundFileDownloads> promise) = 0;
//
// private interface to handle all kinds of updates

View File

@ -39809,8 +39809,13 @@ void MessagesManager::add_message_file_to_downloads(FullMessageId full_message_i
return;
}
auto search_text = get_message_search_text(message);
auto file_source_id = td_->file_reference_manager_->create_message_file_source(full_message_id);
td_->download_manager_->add_file(file_id, file_source_id, std::move(search_text), static_cast<int8>(priority));
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;
}
TRY_STATUS_PROMISE(promise, td_->download_manager_->add_file(file_id, file_source_id, std::move(search_text),
static_cast<int8>(priority)));
promise.set_value(td_->file_manager_->get_file_object(file_id));
}

View File

@ -6551,6 +6551,9 @@ 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"));
}
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));
@ -6558,28 +6561,35 @@ void Td::on_request(uint64 id, const td_api::addFileToDownloads &request) {
void Td::on_request(uint64 id, const td_api::toggleDownloadIsPaused &request) {
CREATE_OK_REQUEST_PROMISE();
promise.set_error(Status::Error(500, "Unsupported"));
promise.set_result(download_manager_->toggle_is_paused(FileId(request.file_id_, 0), request.is_paused_));
}
void Td::on_request(uint64 id, const td_api::toggleAllDownloadsArePaused &request) {
CREATE_OK_REQUEST_PROMISE();
promise.set_error(Status::Error(500, "Unsupported"));
promise.set_result(download_manager_->toggle_all_is_paused(request.are_paused_));
}
void Td::on_request(uint64 id, const td_api::removeFileFromDownloads &request) {
CREATE_OK_REQUEST_PROMISE();
promise.set_error(Status::Error(500, "Unsupported"));
promise.set_result(download_manager_->remove_file(FileId(request.file_id_, 0), {}, request.delete_from_cache_));
}
void Td::on_request(uint64 id, const td_api::removeAllFilesFromDownloads &request) {
CREATE_OK_REQUEST_PROMISE();
promise.set_error(Status::Error(500, "Unsupported"));
promise.set_result(
download_manager_->remove_all_files(request.only_active_, request.only_completed_, request.delete_from_cache_));
}
void Td::on_request(uint64 id, td_api::searchFileDownloads &request) {
CLEAN_INPUT_STRING(request.query_);
CLEAN_INPUT_STRING(request.offset_);
CREATE_REQUEST_PROMISE();
auto wrapped_promise = [promise = std::move(promise)](Result<DownloadManager::FoundFileDownloads> result) mutable {
TRY_RESULT_PROMISE(promise, found, std::move(result));
promise.set_value(found.to_td_api());
};
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"));
}