2018-12-05 10:32:31 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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
|
|
|
|
|
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"
|
2023-09-21 18:11:17 +02:00
|
|
|
#include "td/telegram/MessageFullId.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"
|
2023-05-20 00:23:33 +02:00
|
|
|
#include "td/telegram/StoryFullId.h"
|
2022-02-27 16:23:06 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
2019-01-14 19:46:04 +01:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.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"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.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"
|
2022-11-17 17:13:36 +01:00
|
|
|
#include "td/utils/WaitFreeHashMap.h"
|
2022-08-04 13:48:10 +02:00
|
|
|
#include "td/utils/WaitFreeVector.h"
|
2018-12-05 10:32:31 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileReferenceManager final : public Actor {
|
2018-12-05 10:32:31 +01:00
|
|
|
public:
|
2022-08-09 16:00:36 +02:00
|
|
|
explicit FileReferenceManager(ActorShared<> parent);
|
2022-07-20 12:40:14 +02:00
|
|
|
FileReferenceManager(const FileReferenceManager &) = delete;
|
|
|
|
FileReferenceManager &operator=(const FileReferenceManager &) = delete;
|
|
|
|
FileReferenceManager(FileReferenceManager &&) = delete;
|
|
|
|
FileReferenceManager &operator=(FileReferenceManager &&) = delete;
|
|
|
|
~FileReferenceManager() final;
|
|
|
|
|
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
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
FileSourceId create_message_file_source(MessageFullId message_full_id);
|
2019-01-18 20:36:23 +01:00
|
|
|
FileSourceId create_user_photo_file_source(UserId user_id, int64 photo_id);
|
2020-06-27 16:05:45 +02:00
|
|
|
// file reference aren't used for chat/channel photo download and the photos can't be reused
|
|
|
|
// 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);
|
2020-07-06 14:26:29 +02:00
|
|
|
FileSourceId create_chat_full_file_source(ChatId chat_id);
|
|
|
|
FileSourceId create_channel_full_file_source(ChannelId channel_id);
|
2021-10-08 12:41:22 +02:00
|
|
|
FileSourceId create_app_config_file_source();
|
2022-04-15 15:27:30 +02:00
|
|
|
FileSourceId create_saved_ringtones_file_source();
|
2022-08-10 20:12:05 +02:00
|
|
|
FileSourceId create_user_full_file_source(UserId user_id);
|
|
|
|
FileSourceId create_attach_menu_bot_file_source(UserId user_id);
|
2023-02-28 12:11:49 +01:00
|
|
|
FileSourceId create_web_app_file_source(UserId user_id, const string &short_name);
|
2023-05-20 00:23:33 +02:00
|
|
|
FileSourceId create_story_file_source(StoryFullId story_full_id);
|
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);
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2022-03-01 21:09:40 +01:00
|
|
|
void get_file_search_text(FileSourceId file_source_id, string unique_file_id, Promise<string> promise);
|
2022-02-27 16:23:06 +01:00
|
|
|
|
|
|
|
td_api::object_ptr<td_api::message> get_message_object(FileSourceId file_source_id) const;
|
2022-02-26 19:52:14 +01:00
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
static 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);
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
vector<MessageFullId> 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 {
|
|
|
|
NodeId node_id;
|
2021-11-11 15:39:09 +01:00
|
|
|
int64 generation{0};
|
2021-12-01 18:54:56 +01:00
|
|
|
|
|
|
|
Destination() = default;
|
|
|
|
Destination(NodeId node_id, int64 generation) : node_id(node_id), generation(generation) {
|
|
|
|
}
|
|
|
|
bool is_empty() const {
|
|
|
|
return node_id.empty();
|
|
|
|
}
|
2019-01-17 21:42:00 +01:00
|
|
|
};
|
|
|
|
struct Query {
|
|
|
|
std::vector<Promise<>> promises;
|
|
|
|
int32 active_queries{0};
|
|
|
|
Destination proxy;
|
2021-11-11 15:39:09 +01:00
|
|
|
int64 generation{0};
|
2019-01-17 21:42:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2023-09-21 18:11:17 +02:00
|
|
|
MessageFullId message_full_id;
|
2019-01-14 19:46:04 +01:00
|
|
|
};
|
|
|
|
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
|
|
|
};
|
2020-07-06 14:26:29 +02:00
|
|
|
struct FileSourceChatFull {
|
|
|
|
ChatId chat_id;
|
|
|
|
};
|
|
|
|
struct FileSourceChannelFull {
|
|
|
|
ChannelId channel_id;
|
|
|
|
};
|
2021-10-08 12:41:22 +02:00
|
|
|
struct FileSourceAppConfig {
|
|
|
|
// empty
|
|
|
|
};
|
2022-04-15 15:27:30 +02:00
|
|
|
struct FileSourceSavedRingtones {
|
|
|
|
// empty
|
|
|
|
};
|
2022-08-06 12:42:35 +02:00
|
|
|
struct FileSourceUserFull {
|
|
|
|
UserId user_id;
|
|
|
|
};
|
2022-08-10 20:12:05 +02:00
|
|
|
struct FileSourceAttachMenuBot {
|
|
|
|
UserId user_id;
|
|
|
|
};
|
2023-02-28 12:11:49 +01:00
|
|
|
struct FileSourceWebApp {
|
|
|
|
UserId user_id;
|
|
|
|
string short_name;
|
|
|
|
};
|
2023-05-20 00:23:33 +02:00
|
|
|
struct FileSourceStory {
|
|
|
|
StoryFullId story_full_id;
|
|
|
|
};
|
2019-01-14 19:46:04 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
// append only
|
2022-08-06 12:42:35 +02:00
|
|
|
using FileSource =
|
|
|
|
Variant<FileSourceMessage, FileSourceUserPhoto, FileSourceChatPhoto, FileSourceChannelPhoto, FileSourceWallpapers,
|
|
|
|
FileSourceWebPage, FileSourceSavedAnimations, FileSourceRecentStickers, FileSourceFavoriteStickers,
|
|
|
|
FileSourceBackground, FileSourceChatFull, FileSourceChannelFull, FileSourceAppConfig,
|
2023-05-20 00:23:33 +02:00
|
|
|
FileSourceSavedRingtones, FileSourceUserFull, FileSourceAttachMenuBot, FileSourceWebApp, FileSourceStory>;
|
2022-08-04 13:48:10 +02:00
|
|
|
WaitFreeVector<FileSource> file_sources_;
|
2019-01-14 19:46:04 +01:00
|
|
|
|
2019-01-18 20:36:23 +01:00
|
|
|
int64 query_generation_{0};
|
2019-01-17 21:42:00 +01:00
|
|
|
|
2022-11-17 17:13:36 +01:00
|
|
|
WaitFreeHashMap<NodeId, unique_ptr<Node>, FileIdHash> nodes_;
|
2019-01-17 21:42:00 +01:00
|
|
|
|
2022-08-09 16:00:36 +02:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2022-11-17 17:13:36 +01:00
|
|
|
Node &add_node(NodeId node_id);
|
|
|
|
|
2019-01-17 21:42:00 +01:00
|
|
|
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>
|
2023-02-28 12:35:52 +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;
|
2022-08-09 16:00:36 +02:00
|
|
|
|
2022-08-12 17:39:36 +02:00
|
|
|
void tear_down() final;
|
2018-12-05 10:32:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|