2018-12-05 12:32:31 +03:00
|
|
|
//
|
2023-01-01 00:28:08 +03:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-05 12:32:31 +03: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 04:07:21 +03:00
|
|
|
#include "td/telegram/BackgroundId.h"
|
2019-01-14 21:46:04 +03:00
|
|
|
#include "td/telegram/ChannelId.h"
|
2019-01-19 20:19:29 +03:00
|
|
|
#include "td/telegram/ChatId.h"
|
2019-01-19 05:44:31 +03:00
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2023-09-21 19:11:17 +03:00
|
|
|
#include "td/telegram/MessageFullId.h"
|
2019-07-30 17:42:36 +03:00
|
|
|
#include "td/telegram/PhotoSizeSource.h"
|
2019-01-18 19:39:19 +03:00
|
|
|
#include "td/telegram/SetWithPosition.h"
|
2023-05-20 01:23:33 +03:00
|
|
|
#include "td/telegram/StoryFullId.h"
|
2022-02-27 18:23:06 +03:00
|
|
|
#include "td/telegram/td_api.h"
|
2019-01-14 21:46:04 +03:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2021-09-19 00:47:05 +03:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2019-08-14 03:13:34 +03:00
|
|
|
#include "td/utils/common.h"
|
2019-01-19 20:19:29 +03:00
|
|
|
#include "td/utils/logging.h"
|
2022-06-27 13:30:18 +03:00
|
|
|
#include "td/utils/Promise.h"
|
2019-01-21 21:22:56 +03:00
|
|
|
#include "td/utils/Slice.h"
|
2019-01-19 20:19:29 +03:00
|
|
|
#include "td/utils/Status.h"
|
2019-01-14 21:46:04 +03:00
|
|
|
#include "td/utils/Variant.h"
|
2022-11-17 19:13:36 +03:00
|
|
|
#include "td/utils/WaitFreeHashMap.h"
|
2022-08-04 14:48:10 +03:00
|
|
|
#include "td/utils/WaitFreeVector.h"
|
2018-12-05 12:32:31 +03:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-01-31 00:37:38 +03:00
|
|
|
class Td;
|
|
|
|
|
2019-01-14 21:46:04 +03:00
|
|
|
extern int VERBOSITY_NAME(file_references);
|
|
|
|
|
2021-07-04 05:58:54 +03:00
|
|
|
class FileReferenceManager final : public Actor {
|
2018-12-05 12:32:31 +03:00
|
|
|
public:
|
2022-08-09 17:00:36 +03:00
|
|
|
explicit FileReferenceManager(ActorShared<> parent);
|
2022-07-20 13:40:14 +03:00
|
|
|
FileReferenceManager(const FileReferenceManager &) = delete;
|
|
|
|
FileReferenceManager &operator=(const FileReferenceManager &) = delete;
|
|
|
|
FileReferenceManager(FileReferenceManager &&) = delete;
|
|
|
|
FileReferenceManager &operator=(FileReferenceManager &&) = delete;
|
|
|
|
~FileReferenceManager() final;
|
|
|
|
|
2019-01-23 19:00:56 +03:00
|
|
|
static bool is_file_reference_error(const Status &error);
|
2019-02-08 17:54:23 +03:00
|
|
|
static size_t get_file_reference_error_pos(const Status &error);
|
2019-01-23 19:00:56 +03:00
|
|
|
|
2023-09-21 19:11:17 +03:00
|
|
|
FileSourceId create_message_file_source(MessageFullId message_full_id);
|
2019-01-18 22:36:23 +03:00
|
|
|
FileSourceId create_user_photo_file_source(UserId user_id, int64 photo_id);
|
2020-06-27 17:05:45 +03: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 05:51:56 +03:00
|
|
|
// FileSourceId create_wallpapers_file_source(); old wallpapers can't be repaired
|
2019-01-18 22:36:23 +03:00
|
|
|
FileSourceId create_web_page_file_source(string url);
|
|
|
|
FileSourceId create_saved_animations_file_source();
|
2019-02-02 13:30:49 +03:00
|
|
|
FileSourceId create_recent_stickers_file_source(bool is_attached);
|
2019-02-02 13:54:40 +03:00
|
|
|
FileSourceId create_favorite_stickers_file_source();
|
2019-05-09 04:07:21 +03:00
|
|
|
FileSourceId create_background_file_source(BackgroundId background_id, int64 access_hash);
|
2020-07-06 15:26:29 +03:00
|
|
|
FileSourceId create_chat_full_file_source(ChatId chat_id);
|
|
|
|
FileSourceId create_channel_full_file_source(ChannelId channel_id);
|
2021-10-08 13:41:22 +03:00
|
|
|
FileSourceId create_app_config_file_source();
|
2022-04-15 16:27:30 +03:00
|
|
|
FileSourceId create_saved_ringtones_file_source();
|
2022-08-10 21:12:05 +03:00
|
|
|
FileSourceId create_user_full_file_source(UserId user_id);
|
|
|
|
FileSourceId create_attach_menu_bot_file_source(UserId user_id);
|
2023-02-28 14:11:49 +03:00
|
|
|
FileSourceId create_web_app_file_source(UserId user_id, const string &short_name);
|
2023-05-20 01:23:33 +03:00
|
|
|
FileSourceId create_story_file_source(StoryFullId story_full_id);
|
2019-01-14 21:46:04 +03:00
|
|
|
|
2019-01-17 23:42:00 +03:00
|
|
|
using NodeId = FileId;
|
2019-01-18 23:12:09 +03:00
|
|
|
void repair_file_reference(NodeId node_id, Promise<> promise);
|
2021-10-19 18:11:16 +03:00
|
|
|
|
2022-03-01 23:09:40 +03:00
|
|
|
void get_file_search_text(FileSourceId file_source_id, string unique_file_id, Promise<string> promise);
|
2022-02-27 18:23:06 +03:00
|
|
|
|
|
|
|
td_api::object_ptr<td_api::message> get_message_object(FileSourceId file_source_id) const;
|
2022-02-26 21:52:14 +03:00
|
|
|
|
2021-10-19 18:11:16 +03:00
|
|
|
static void reload_photo(PhotoSizeSource source, Promise<Unit> promise);
|
2019-01-23 22:20:48 +03:00
|
|
|
|
2019-01-31 00:37:38 +03:00
|
|
|
bool add_file_source(NodeId node_id, FileSourceId file_source_id);
|
2019-01-23 22:20:48 +03:00
|
|
|
|
2019-04-28 20:21:44 +03:00
|
|
|
vector<FileSourceId> get_some_file_sources(NodeId node_id);
|
|
|
|
|
2023-09-21 19:11:17 +03:00
|
|
|
vector<MessageFullId> get_some_message_file_sources(NodeId node_id);
|
2019-01-29 15:07:58 +04:00
|
|
|
|
2019-01-31 00:37:38 +03:00
|
|
|
bool remove_file_source(NodeId node_id, FileSourceId file_source_id);
|
2019-01-23 22:20:48 +03:00
|
|
|
|
2019-01-17 23:42:00 +03:00
|
|
|
void merge(NodeId to_node_id, NodeId from_node_id);
|
2018-12-05 12:32:31 +03:00
|
|
|
|
2019-01-31 00:37:38 +03: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 12:32:31 +03:00
|
|
|
private:
|
2019-01-17 23:42:00 +03:00
|
|
|
struct Destination {
|
|
|
|
NodeId node_id;
|
2021-11-11 17:39:09 +03:00
|
|
|
int64 generation{0};
|
2021-12-01 20:54:56 +03: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 23:42:00 +03:00
|
|
|
};
|
|
|
|
struct Query {
|
|
|
|
std::vector<Promise<>> promises;
|
|
|
|
int32 active_queries{0};
|
|
|
|
Destination proxy;
|
2021-11-11 17:39:09 +03:00
|
|
|
int64 generation{0};
|
2019-01-17 23:42:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Node {
|
|
|
|
SetWithPosition<FileSourceId> file_source_ids;
|
|
|
|
unique_ptr<Query> query;
|
2019-10-18 04:30:49 +03:00
|
|
|
double last_successful_repair_time = -1e10;
|
2019-01-17 23:42:00 +03:00
|
|
|
};
|
|
|
|
|
2019-01-14 21:46:04 +03:00
|
|
|
struct FileSourceMessage {
|
2023-09-21 19:11:17 +03:00
|
|
|
MessageFullId message_full_id;
|
2019-01-14 21:46:04 +03:00
|
|
|
};
|
|
|
|
struct FileSourceUserPhoto {
|
|
|
|
int64 photo_id;
|
2019-01-18 21:24:03 +03:00
|
|
|
UserId user_id;
|
2019-01-14 21:46:04 +03:00
|
|
|
};
|
|
|
|
struct FileSourceChatPhoto {
|
|
|
|
ChatId chat_id;
|
|
|
|
};
|
|
|
|
struct FileSourceChannelPhoto {
|
|
|
|
ChannelId channel_id;
|
|
|
|
};
|
|
|
|
struct FileSourceWallpapers {
|
|
|
|
// empty
|
|
|
|
};
|
|
|
|
struct FileSourceWebPage {
|
|
|
|
string url;
|
|
|
|
};
|
|
|
|
struct FileSourceSavedAnimations {
|
|
|
|
// empty
|
|
|
|
};
|
2019-02-02 13:30:49 +03:00
|
|
|
struct FileSourceRecentStickers {
|
|
|
|
bool is_attached;
|
|
|
|
};
|
2019-05-09 04:07:21 +03:00
|
|
|
struct FileSourceFavoriteStickers {
|
|
|
|
// empty
|
|
|
|
};
|
|
|
|
struct FileSourceBackground {
|
|
|
|
BackgroundId background_id;
|
2019-07-25 00:00:21 +03:00
|
|
|
int64 access_hash;
|
2019-05-09 04:07:21 +03:00
|
|
|
};
|
2020-07-06 15:26:29 +03:00
|
|
|
struct FileSourceChatFull {
|
|
|
|
ChatId chat_id;
|
|
|
|
};
|
|
|
|
struct FileSourceChannelFull {
|
|
|
|
ChannelId channel_id;
|
|
|
|
};
|
2021-10-08 13:41:22 +03:00
|
|
|
struct FileSourceAppConfig {
|
|
|
|
// empty
|
|
|
|
};
|
2022-04-15 16:27:30 +03:00
|
|
|
struct FileSourceSavedRingtones {
|
|
|
|
// empty
|
|
|
|
};
|
2022-08-06 13:42:35 +03:00
|
|
|
struct FileSourceUserFull {
|
|
|
|
UserId user_id;
|
|
|
|
};
|
2022-08-10 21:12:05 +03:00
|
|
|
struct FileSourceAttachMenuBot {
|
|
|
|
UserId user_id;
|
|
|
|
};
|
2023-02-28 14:11:49 +03:00
|
|
|
struct FileSourceWebApp {
|
|
|
|
UserId user_id;
|
|
|
|
string short_name;
|
|
|
|
};
|
2023-05-20 01:23:33 +03:00
|
|
|
struct FileSourceStory {
|
|
|
|
StoryFullId story_full_id;
|
|
|
|
};
|
2019-01-14 21:46:04 +03:00
|
|
|
|
2019-01-31 00:37:38 +03:00
|
|
|
// append only
|
2022-08-06 13:42:35 +03:00
|
|
|
using FileSource =
|
|
|
|
Variant<FileSourceMessage, FileSourceUserPhoto, FileSourceChatPhoto, FileSourceChannelPhoto, FileSourceWallpapers,
|
|
|
|
FileSourceWebPage, FileSourceSavedAnimations, FileSourceRecentStickers, FileSourceFavoriteStickers,
|
|
|
|
FileSourceBackground, FileSourceChatFull, FileSourceChannelFull, FileSourceAppConfig,
|
2023-05-20 01:23:33 +03:00
|
|
|
FileSourceSavedRingtones, FileSourceUserFull, FileSourceAttachMenuBot, FileSourceWebApp, FileSourceStory>;
|
2022-08-04 14:48:10 +03:00
|
|
|
WaitFreeVector<FileSource> file_sources_;
|
2019-01-14 21:46:04 +03:00
|
|
|
|
2019-01-18 22:36:23 +03:00
|
|
|
int64 query_generation_{0};
|
2019-01-17 23:42:00 +03:00
|
|
|
|
2022-11-17 19:13:36 +03:00
|
|
|
WaitFreeHashMap<NodeId, unique_ptr<Node>, FileIdHash> nodes_;
|
2019-01-17 23:42:00 +03:00
|
|
|
|
2022-08-09 17:00:36 +03:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2022-11-17 19:13:36 +03:00
|
|
|
Node &add_node(NodeId node_id);
|
|
|
|
|
2019-01-17 23:42:00 +03: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 22:10:38 +03:00
|
|
|
|
2019-01-18 22:36:23 +03:00
|
|
|
template <class T>
|
2023-02-28 14:35:52 +03:00
|
|
|
FileSourceId add_file_source_id(T &source, Slice source_str);
|
2019-01-18 22:36:23 +03:00
|
|
|
|
2019-01-18 22:10:38 +03:00
|
|
|
FileSourceId get_current_file_source_id() const;
|
2022-08-09 17:00:36 +03:00
|
|
|
|
2022-08-12 18:39:36 +03:00
|
|
|
void tear_down() final;
|
2018-12-05 12:32:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|