Minor improvements.

This commit is contained in:
levlam 2022-02-25 16:18:23 +03:00
parent 97b2d3edd2
commit 21a71e6441
7 changed files with 57 additions and 49 deletions

View File

@ -506,6 +506,7 @@ set(TDLIB_SOURCE
td/telegram/DialogSource.h td/telegram/DialogSource.h
td/telegram/Document.h td/telegram/Document.h
td/telegram/DocumentsManager.h td/telegram/DocumentsManager.h
td/telegram/DownloadManager.h
td/telegram/DraftMessage.h td/telegram/DraftMessage.h
td/telegram/EncryptedFile.h td/telegram/EncryptedFile.h
td/telegram/FileReferenceManager.h td/telegram/FileReferenceManager.h

View File

@ -4,10 +4,12 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying // 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) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#include "DownloadManager.h" #include "td/telegram/DownloadManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashMap.h"
namespace td { namespace td {
class DownloadManagerImpl final : public DownloadManager { class DownloadManagerImpl final : public DownloadManager {
public: public:
void set_callback(unique_ptr<Callback> callback) final { void set_callback(unique_ptr<Callback> callback) final {
@ -94,7 +96,7 @@ class DownloadManagerImpl final : public DownloadManager {
return Status::OK(); 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_) { if (!callback_) {
return Status::Error("TODO: code and message`"); return Status::Error("TODO: code and message`");
} }
@ -116,7 +118,7 @@ class DownloadManagerImpl final : public DownloadManager {
return Status::OK(); 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<FoundFileDownloads> promise) final { Promise<FoundFileDownloads> promise) final {
if (!callback_) { if (!callback_) {
return promise.set_error(Status::Error("TODO: code and message`")); return promise.set_error(Status::Error("TODO: code and message`"));
@ -138,11 +140,11 @@ class DownloadManagerImpl final : public DownloadManager {
CHECK(it != active_files_.end()); CHECK(it != active_files_.end());
auto &file_info = it->second; auto &file_info = it->second;
counters_.downloaded_size -= file_info.downloaded_size; 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.size = size;
file_info.downloaded_size = download_size; file_info.downloaded_size = download_size;
counters_.downloaded_size += file_info.downloaded_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; file_info.is_paused = is_paused;
if (download_size == size) { if (download_size == size) {
@ -184,14 +186,14 @@ class DownloadManagerImpl final : public DownloadManager {
Counters counters_; Counters counters_;
void loop() override { void loop() final {
if (!callback_) { if (!callback_) {
return; return;
} }
// TODO: ??? // TODO: ???
// TODO: load active files from db // TODO: load active files from db
} }
void tear_down() override { void tear_down() final {
callback_.reset(); callback_.reset();
} }
}; };
@ -199,7 +201,9 @@ class DownloadManagerImpl final : public DownloadManager {
unique_ptr<DownloadManager> DownloadManager::create() { unique_ptr<DownloadManager> DownloadManager::create() {
return make_unique<DownloadManagerImpl>(); return make_unique<DownloadManagerImpl>();
} }
tl_object_ptr<td_api::foundFileDownloads> DownloadManager::FoundFileDownloads::to_td_api() const { tl_object_ptr<td_api::foundFileDownloads> DownloadManager::FoundFileDownloads::to_td_api() const {
return make_tl_object<td_api::foundFileDownloads>(); return make_tl_object<td_api::foundFileDownloads>();
} }
} // namespace td } // namespace td

View File

@ -5,15 +5,20 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#pragma once #pragma once
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/telegram/files/FileId.h" #include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h" #include "td/telegram/files/FileSourceId.h"
#include "td/telegram/td_api.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/common.h"
#include "td/utils/Status.h"
namespace td { namespace td {
class DownloadManager : public td::Actor {
class DownloadManager : public Actor {
public: public:
// creates, but do not stats the actor // creates, but do not stats the actor
static td::unique_ptr<DownloadManager> create(); static td::unique_ptr<DownloadManager> create();
@ -34,8 +39,8 @@ class DownloadManager : public td::Actor {
struct FoundFileDownloads { struct FoundFileDownloads {
int32 total_count{}; int32 total_count{};
std::vector<FileDownload> file_downloads; vector<FileDownload> file_downloads;
std::string offset; string offset;
tl_object_ptr<td_api::foundFileDownloads> to_td_api() const; tl_object_ptr<td_api::foundFileDownloads> to_td_api() const;
}; };
@ -49,8 +54,8 @@ class DownloadManager : public td::Actor {
virtual void delete_file(FileId file_id) = 0; virtual void delete_file(FileId file_id) = 0;
virtual FileId dup_file_id(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 string get_unique_file_id(FileId file_id) = 0;
virtual std::string get_file_source_serialized(FileSourceId file_source_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_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; 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 // 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 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, std::string offset, int32 limit, virtual void search(std::string query, bool only_active, bool only_completed, string offset, int32 limit,
Promise<FoundFileDownloads> promise) = 0; Promise<FoundFileDownloads> 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_download_state(FileId file_id, int64 download_size, int64 size, bool is_paused) = 0;
virtual void update_file_deleted(FileId file_id) = 0; virtual void update_file_deleted(FileId file_id) = 0;
}; };
}; // namespace td
} // namespace td

View File

@ -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) { FileSourceId MessagesManager::get_message_file_source_id(FullMessageId full_message_id, bool force) {
if (td_->auth_manager_->is_bot()) { if (!force) {
return FileSourceId(); if (td_->auth_manager_->is_bot()) {
} return FileSourceId();
}
auto dialog_id = full_message_id.get_dialog_id(); auto dialog_id = full_message_id.get_dialog_id();
auto message_id = full_message_id.get_message_id(); auto message_id = full_message_id.get_message_id();
if (!dialog_id.is_valid() || !(message_id.is_valid() || message_id.is_valid_scheduled()) || 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()) { dialog_id.get_type() == DialogType::SecretChat || !message_id.is_any_server()) {
return FileSourceId(); return FileSourceId();
}
} }
auto &file_source_id = full_message_id_to_file_source_id_[full_message_id]; auto &file_source_id = full_message_id_to_file_source_id_[full_message_id];
@ -39799,21 +39801,16 @@ void MessagesManager::get_current_state(vector<td_api::object_ptr<td_api::Update
void MessagesManager::add_message_file_to_downloads(FullMessageId full_message_id, FileId file_id, int32 priority, void MessagesManager::add_message_file_to_downloads(FullMessageId full_message_id, FileId file_id, int32 priority,
Promise<td_api::object_ptr<td_api::file>> promise) { Promise<td_api::object_ptr<td_api::file>> promise) {
auto message = get_message_force(full_message_id, "add message file to downloads"); auto m = get_message_force(full_message_id, "add_message_file_to_downloads");
if (!message) { if (m == nullptr) {
promise.set_error(Status::Error(400, "Can't find message")); return promise.set_error(Status::Error(400, "Message not found"));
return;
} }
if (!contains(get_message_file_ids(message), file_id)) { if (!contains(get_message_file_ids(m), file_id)) {
promise.set_error(Status::Error(400, "Can't find file in message")); return promise.set_error(Status::Error(400, "Message has no specified file"));
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;
} }
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), TRY_STATUS_PROMISE(promise, td_->download_manager_->add_file(file_id, file_source_id, std::move(search_text),
static_cast<int8>(priority))); static_cast<int8>(priority)));
promise.set_value(td_->file_manager_->get_file_object(file_id)); promise.set_value(td_->file_manager_->get_file_object(file_id));

View File

@ -922,7 +922,7 @@ class MessagesManager final : public Actor {
vector<tl_object_ptr<telegram_api::User>> users, vector<tl_object_ptr<telegram_api::User>> users,
vector<tl_object_ptr<telegram_api::Chat>> chats); vector<tl_object_ptr<telegram_api::Chat>> 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 { struct MessagePushNotificationInfo {
NotificationGroupId group_id; NotificationGroupId group_id;

View File

@ -3975,7 +3975,7 @@ void Td::init_managers() {
// TODO: move this callback somewhere else // TODO: move this callback somewhere else
class DownloadManagerCallback final : public DownloadManager::Callback { class DownloadManagerCallback final : public DownloadManager::Callback {
public: public:
DownloadManagerCallback(ActorShared<> parent) : parent_(std::move(parent)) { explicit DownloadManagerCallback(ActorShared<> parent) : parent_(std::move(parent)) {
// TODO // TODO
} }
void update_counters(DownloadManager::Counters counters) final { void update_counters(DownloadManager::Counters counters) final {
@ -3996,11 +3996,11 @@ void Td::init_managers() {
} }
string get_unique_file_id(FileId file_id) final { string get_unique_file_id(FileId file_id) final {
// TODO // TODO
return std::string(); return string();
} }
string get_file_source_serialized(FileSourceId file_source_id) final { string get_file_source_serialized(FileSourceId file_source_id) final {
// TODO // TODO
return std::string(); return string();
} }
private: 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) { void Td::on_request(uint64 id, const td_api::downloadFile &request) {
auto priority = request.priority_; auto priority = request.priority_;
if (!(1 <= priority && priority <= 32)) { 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_; auto offset = request.offset_;
if (offset < 0) { 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) { void Td::on_request(uint64 id, td_api::uploadFile &request) {
auto priority = request.priority_; auto priority = request.priority_;
if (!(1 <= priority && priority <= 32)) { 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_); 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) { void Td::on_request(uint64 id, const td_api::addFileToDownloads &request) {
CREATE_REQUEST_PROMISE();
if (!(1 <= request.priority_ && request.priority_ <= 32)) { 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( messages_manager_->add_message_file_to_downloads(
FullMessageId(DialogId(request.chat_id_), MessageId(request.message_id_)), FileId(request.file_id_, 0), FullMessageId(DialogId(request.chat_id_), MessageId(request.message_id_)), FileId(request.file_id_, 0),
request.priority_, std::move(promise)); 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_, 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)); 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) { void Td::on_request(uint64 id, td_api::getMessageFileType &request) {

View File

@ -12,6 +12,7 @@
#include "td/utils/FlatHashMapLinear.h" #include "td/utils/FlatHashMapLinear.h"
#include <cstddef> #include <cstddef>
#include <cstdlib>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
#include <iterator> #include <iterator>