2018-12-05 10:32:31 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-05 10:32:31 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
#include "td/telegram/BackgroundId.h"
|
2019-01-14 19:46:04 +01:00
|
|
|
#include "td/telegram/ChannelId.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/telegram/ChatId.h"
|
2019-01-19 03:44:31 +01:00
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2019-11-26 17:53:10 +01:00
|
|
|
#include "td/telegram/FullMessageId.h"
|
2019-07-30 16:42:36 +02:00
|
|
|
#include "td/telegram/PhotoSizeSource.h"
|
2019-01-18 17:39:19 +01:00
|
|
|
#include "td/telegram/SetWithPosition.h"
|
2019-01-14 19:46:04 +01:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/common.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-01-21 19:22:56 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/Status.h"
|
2019-01-14 19:46:04 +01:00
|
|
|
#include "td/utils/Variant.h"
|
2018-12-05 10:32:31 +01:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
class Td;
|
|
|
|
|
2019-01-14 19:46:04 +01:00
|
|
|
extern int VERBOSITY_NAME(file_references);
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
class FileReferenceManager : public Actor {
|
|
|
|
public:
|
2019-01-23 17:00:56 +01:00
|
|
|
static bool is_file_reference_error(const Status &error);
|
2019-02-08 15:54:23 +01:00
|
|
|
static size_t get_file_reference_error_pos(const Status &error);
|
2019-01-23 17:00:56 +01:00
|
|
|
|
2020-05-23 21:27:24 +02:00
|
|
|
void memory_cleanup(FileId file_id);
|
|
|
|
|
2019-01-14 19:46:04 +01:00
|
|
|
FileSourceId create_message_file_source(FullMessageId full_message_id);
|
2019-01-18 20:36:23 +01:00
|
|
|
FileSourceId create_user_photo_file_source(UserId user_id, int64 photo_id);
|
|
|
|
FileSourceId create_chat_photo_file_source(ChatId chat_id);
|
|
|
|
FileSourceId create_channel_photo_file_source(ChannelId channel_id);
|
2019-05-07 04:51:56 +02:00
|
|
|
// FileSourceId create_wallpapers_file_source(); old wallpapers can't be repaired
|
2019-01-18 20:36:23 +01:00
|
|
|
FileSourceId create_web_page_file_source(string url);
|
|
|
|
FileSourceId create_saved_animations_file_source();
|
2019-02-02 11:30:49 +01:00
|
|
|
FileSourceId create_recent_stickers_file_source(bool is_attached);
|
2019-02-02 11:54:40 +01:00
|
|
|
FileSourceId create_favorite_stickers_file_source();
|
2019-05-09 03:07:21 +02:00
|
|
|
FileSourceId create_background_file_source(BackgroundId background_id, int64 access_hash);
|
2019-01-14 19:46:04 +01:00
|
|
|
|
2019-01-17 21:42:00 +01:00
|
|
|
using NodeId = FileId;
|
2019-01-18 21:12:09 +01:00
|
|
|
void repair_file_reference(NodeId node_id, Promise<> promise);
|
2019-07-30 16:42:36 +02:00
|
|
|
void reload_photo(PhotoSizeSource source, Promise<Unit> promise);
|
2019-01-23 20:20:48 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
bool add_file_source(NodeId node_id, FileSourceId file_source_id);
|
2019-01-23 20:20:48 +01:00
|
|
|
|
2019-04-28 19:21:44 +02:00
|
|
|
vector<FileSourceId> get_some_file_sources(NodeId node_id);
|
|
|
|
|
|
|
|
vector<FullMessageId> get_some_message_file_sources(NodeId node_id);
|
2019-01-29 12:07:58 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
bool remove_file_source(NodeId node_id, FileSourceId file_source_id);
|
2019-01-23 20:20:48 +01:00
|
|
|
|
2019-01-17 21:42:00 +01:00
|
|
|
void merge(NodeId to_node_id, NodeId from_node_id);
|
2018-12-05 10:32:31 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store_file_source(FileSourceId file_source_id, StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
FileSourceId parse_file_source(Td *td, ParserT &parser);
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
private:
|
2019-01-17 21:42:00 +01:00
|
|
|
struct Destination {
|
|
|
|
bool empty() const {
|
|
|
|
return node_id.empty();
|
|
|
|
}
|
|
|
|
NodeId node_id;
|
|
|
|
int64 generation;
|
|
|
|
};
|
|
|
|
struct Query {
|
|
|
|
std::vector<Promise<>> promises;
|
|
|
|
int32 active_queries{0};
|
|
|
|
Destination proxy;
|
|
|
|
int64 generation;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Node {
|
|
|
|
SetWithPosition<FileSourceId> file_source_ids;
|
|
|
|
unique_ptr<Query> query;
|
2019-10-18 03:30:49 +02:00
|
|
|
double last_successful_repair_time = -1e10;
|
2019-01-17 21:42:00 +01:00
|
|
|
};
|
|
|
|
|
2019-01-14 19:46:04 +01:00
|
|
|
struct FileSourceMessage {
|
|
|
|
FullMessageId full_message_id;
|
|
|
|
};
|
|
|
|
struct FileSourceUserPhoto {
|
|
|
|
int64 photo_id;
|
2019-01-18 19:24:03 +01:00
|
|
|
UserId user_id;
|
2019-01-14 19:46:04 +01:00
|
|
|
};
|
|
|
|
struct FileSourceChatPhoto {
|
|
|
|
ChatId chat_id;
|
|
|
|
};
|
|
|
|
struct FileSourceChannelPhoto {
|
|
|
|
ChannelId channel_id;
|
|
|
|
};
|
|
|
|
struct FileSourceWallpapers {
|
|
|
|
// empty
|
|
|
|
};
|
|
|
|
struct FileSourceWebPage {
|
|
|
|
string url;
|
|
|
|
};
|
|
|
|
struct FileSourceSavedAnimations {
|
|
|
|
// empty
|
|
|
|
};
|
2019-02-02 11:30:49 +01:00
|
|
|
struct FileSourceRecentStickers {
|
|
|
|
bool is_attached;
|
|
|
|
};
|
2019-05-09 03:07:21 +02:00
|
|
|
struct FileSourceFavoriteStickers {
|
|
|
|
// empty
|
|
|
|
};
|
|
|
|
struct FileSourceBackground {
|
|
|
|
BackgroundId background_id;
|
2019-07-24 23:00:21 +02:00
|
|
|
int64 access_hash;
|
2019-05-09 03:07:21 +02:00
|
|
|
};
|
2019-01-14 19:46:04 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
// append only
|
2019-05-09 03:07:21 +02:00
|
|
|
using FileSource = Variant<FileSourceMessage, FileSourceUserPhoto, FileSourceChatPhoto, FileSourceChannelPhoto,
|
|
|
|
FileSourceWallpapers, FileSourceWebPage, FileSourceSavedAnimations,
|
|
|
|
FileSourceRecentStickers, FileSourceFavoriteStickers, FileSourceBackground>;
|
2019-01-14 19:46:04 +01:00
|
|
|
vector<FileSource> file_sources_;
|
|
|
|
|
2019-01-18 20:36:23 +01:00
|
|
|
int64 query_generation_{0};
|
2019-01-17 21:42:00 +01:00
|
|
|
|
|
|
|
std::unordered_map<NodeId, Node, FileIdHash> nodes_;
|
|
|
|
|
|
|
|
void run_node(NodeId node);
|
|
|
|
void send_query(Destination dest, FileSourceId file_source_id);
|
|
|
|
Destination on_query_result(Destination dest, FileSourceId file_source_id, Status status, int32 sub = 0);
|
2019-01-18 20:10:38 +01:00
|
|
|
|
2019-01-18 20:36:23 +01:00
|
|
|
template <class T>
|
2019-01-21 19:22:56 +01:00
|
|
|
FileSourceId add_file_source_id(T source, Slice source_str);
|
2019-01-18 20:36:23 +01:00
|
|
|
|
2019-01-18 20:10:38 +01:00
|
|
|
FileSourceId get_current_file_source_id() const;
|
2018-12-05 10:32:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|