2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +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/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/telegram/DialogId.h"
|
|
|
|
#include "td/telegram/files/FileId.h"
|
2019-01-30 22:37:38 +01:00
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
|
|
|
#include "td/telegram/Photo.h"
|
|
|
|
#include "td/telegram/WebPageId.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
#include "td/actor/Timeout.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2018-02-20 22:20:45 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-01-06 20:11:02 +01:00
|
|
|
struct BinlogEvent;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class Td;
|
|
|
|
|
|
|
|
class WebPagesManager : public Actor {
|
|
|
|
public:
|
|
|
|
WebPagesManager(Td *td, ActorShared<> parent);
|
|
|
|
|
|
|
|
WebPagesManager(const WebPagesManager &) = delete;
|
|
|
|
WebPagesManager &operator=(const WebPagesManager &) = delete;
|
|
|
|
WebPagesManager(WebPagesManager &&) = delete;
|
|
|
|
WebPagesManager &operator=(WebPagesManager &&) = delete;
|
|
|
|
~WebPagesManager() override;
|
|
|
|
|
|
|
|
WebPageId on_get_web_page(tl_object_ptr<telegram_api::WebPage> &&web_page_ptr, DialogId owner_dialog_id);
|
|
|
|
|
|
|
|
void on_get_web_page_by_url(const string &url, WebPageId web_page_id, bool from_database);
|
|
|
|
|
|
|
|
void wait_for_pending_web_page(DialogId dialog_id, MessageId message_id, WebPageId web_page_id);
|
|
|
|
|
|
|
|
bool have_web_page(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
bool have_web_page_force(WebPageId web_page_id);
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::webPage> get_web_page_object(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::webPageInstantView> get_web_page_instant_view_object(WebPageId web_page_id) const;
|
|
|
|
|
2018-02-20 22:20:45 +01:00
|
|
|
int64 get_web_page_preview(td_api::object_ptr<td_api::formattedText> &&text, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::webPage> get_web_page_preview_result(int64 request_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-04-22 19:26:00 +02:00
|
|
|
WebPageId get_web_page_instant_view(const string &url, bool force_full, bool force, Promise<Unit> &&promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
WebPageId get_web_page_by_url(const string &url) const;
|
|
|
|
|
|
|
|
WebPageId get_web_page_by_url(const string &url, Promise<Unit> &&promise);
|
|
|
|
|
2019-01-14 19:46:04 +01:00
|
|
|
void reload_web_page_by_url(const string &url, Promise<Unit> &&promise);
|
|
|
|
|
2018-02-20 22:20:45 +01:00
|
|
|
void on_get_web_page_preview_success(int64 request_id, const string &url,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::MessageMedia> &&message_media_ptr,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2018-02-20 22:20:45 +01:00
|
|
|
void on_get_web_page_preview_fail(int64 request_id, const string &url, Status error, Promise<Unit> &&promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
SecretInputMedia get_secret_input_media(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
void on_binlog_web_page_event(BinlogEvent &&event);
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
FileSourceId get_url_file_source_id(const string &url);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
string get_web_page_search_text(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_TYPE = 1;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_SITE_NAME = 2;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_TITLE = 4;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_DESCRIPTION = 8;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_PHOTO = 16;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW = 32;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW_SIZE = 64;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_DURATION = 128;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_AUTHOR = 256;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_DOCUMENT = 512;
|
|
|
|
static constexpr int32 WEBPAGE_FLAG_HAS_INSTANT_VIEW = 1024;
|
|
|
|
|
|
|
|
class WebPage;
|
|
|
|
|
|
|
|
class RichText;
|
|
|
|
|
2019-02-06 00:24:29 +01:00
|
|
|
class PageBlockCaption;
|
2019-02-07 00:58:27 +01:00
|
|
|
class PageBlockTableCell;
|
2019-02-06 00:24:29 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class PageBlock;
|
|
|
|
class PageBlockTitle;
|
|
|
|
class PageBlockSubtitle;
|
|
|
|
class PageBlockAuthorDate;
|
|
|
|
class PageBlockHeader;
|
|
|
|
class PageBlockSubheader;
|
2019-02-06 01:50:38 +01:00
|
|
|
class PageBlockKicker;
|
2018-12-31 20:04:05 +01:00
|
|
|
class PageBlockParagraph;
|
|
|
|
class PageBlockPreformatted;
|
|
|
|
class PageBlockFooter;
|
|
|
|
class PageBlockDivider;
|
|
|
|
class PageBlockAnchor;
|
|
|
|
class PageBlockList;
|
|
|
|
class PageBlockBlockQuote;
|
|
|
|
class PageBlockPullQuote;
|
|
|
|
class PageBlockAnimation;
|
|
|
|
class PageBlockPhoto;
|
|
|
|
class PageBlockVideo;
|
|
|
|
class PageBlockCover;
|
|
|
|
class PageBlockEmbedded;
|
|
|
|
class PageBlockEmbeddedPost;
|
|
|
|
class PageBlockCollage;
|
|
|
|
class PageBlockSlideshow;
|
|
|
|
class PageBlockChatLink;
|
|
|
|
class PageBlockAudio;
|
2019-02-07 00:58:27 +01:00
|
|
|
class PageBlockTable;
|
2019-02-07 14:38:11 +01:00
|
|
|
class PageBlockDetails;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class WebPageInstantView;
|
|
|
|
|
|
|
|
class WebPageLogEvent;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
friend void store(const unique_ptr<PageBlock> &block, T &storer);
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
friend void parse(unique_ptr<PageBlock> &block, T &parser);
|
|
|
|
|
|
|
|
void update_web_page(unique_ptr<WebPage> web_page, WebPageId web_page_id, bool from_binlog, bool from_database);
|
|
|
|
|
|
|
|
void update_web_page_instant_view(WebPageId web_page_id, WebPageInstantView &new_instant_view,
|
|
|
|
WebPageInstantView &&old_instant_view);
|
|
|
|
|
|
|
|
static bool need_use_old_instant_view(const WebPageInstantView &new_instant_view,
|
|
|
|
const WebPageInstantView &old_instant_view);
|
|
|
|
|
|
|
|
void update_messages_content(WebPageId web_page_id, bool have_web_page);
|
|
|
|
|
|
|
|
const WebPage *get_web_page(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
const WebPageInstantView *get_web_page_instant_view(WebPageId web_page_id) const;
|
|
|
|
|
|
|
|
WebPageId get_web_page_instant_view(WebPageId web_page_id, bool force_full, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::webPageInstantView> get_web_page_instant_view_object(
|
|
|
|
const WebPageInstantView *web_page_instant_view) const;
|
|
|
|
|
|
|
|
static void on_pending_web_page_timeout_callback(void *web_pages_manager_ptr, int64 web_page_id);
|
|
|
|
void on_pending_web_page_timeout(WebPageId web_page_id);
|
|
|
|
|
2018-02-20 22:20:45 +01:00
|
|
|
void on_get_web_page_preview_success(int64 request_id, const string &url, WebPageId web_page_id,
|
|
|
|
Promise<Unit> &&promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-05 20:41:25 +01:00
|
|
|
static RichText get_rich_text(tl_object_ptr<telegram_api::RichText> &&rich_text_ptr,
|
|
|
|
const std::unordered_map<int64, FileId> &documents);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-05 20:41:25 +01:00
|
|
|
static vector<RichText> get_rich_texts(vector<tl_object_ptr<telegram_api::RichText>> &&rich_text_ptrs,
|
|
|
|
const std::unordered_map<int64, FileId> &documents);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static tl_object_ptr<td_api::RichText> get_rich_text_object(const RichText &rich_text);
|
|
|
|
|
|
|
|
static vector<tl_object_ptr<td_api::RichText>> get_rich_text_objects(const vector<RichText> &rich_texts);
|
|
|
|
|
2019-02-06 00:24:29 +01:00
|
|
|
static PageBlockCaption get_page_block_caption(tl_object_ptr<telegram_api::pageCaption> &&page_caption,
|
|
|
|
const std::unordered_map<int64, FileId> &documents);
|
|
|
|
|
|
|
|
static td_api::object_ptr<td_api::pageBlockCaption> get_page_block_caption_object(const PageBlockCaption &caption);
|
|
|
|
|
2019-02-07 00:58:27 +01:00
|
|
|
static td_api::object_ptr<td_api::pageBlockTableCell> get_page_block_table_cell_object(
|
|
|
|
const PageBlockTableCell &cell);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static vector<tl_object_ptr<td_api::PageBlock>> get_page_block_objects(
|
|
|
|
const vector<unique_ptr<PageBlock>> &page_blocks);
|
|
|
|
|
|
|
|
unique_ptr<PageBlock> get_page_block(tl_object_ptr<telegram_api::PageBlock> page_block_ptr,
|
|
|
|
const std::unordered_map<int64, FileId> &animations,
|
|
|
|
const std::unordered_map<int64, FileId> &audios,
|
2019-02-05 20:41:25 +01:00
|
|
|
const std::unordered_map<int64, FileId> &documents,
|
2018-12-31 20:04:05 +01:00
|
|
|
const std::unordered_map<int64, Photo> &photos,
|
|
|
|
const std::unordered_map<int64, FileId> &videos) const;
|
|
|
|
|
|
|
|
vector<unique_ptr<PageBlock>> get_page_blocks(vector<tl_object_ptr<telegram_api::PageBlock>> page_block_ptrs,
|
|
|
|
const std::unordered_map<int64, FileId> &animations,
|
|
|
|
const std::unordered_map<int64, FileId> &audios,
|
2019-02-05 20:41:25 +01:00
|
|
|
const std::unordered_map<int64, FileId> &documents,
|
2018-12-31 20:04:05 +01:00
|
|
|
const std::unordered_map<int64, Photo> &photos,
|
|
|
|
const std::unordered_map<int64, FileId> &videos) const;
|
|
|
|
|
2019-02-04 23:08:15 +01:00
|
|
|
void on_get_web_page_instant_view(WebPage *web_page, tl_object_ptr<telegram_api::page> &&page, int32 hash,
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogId owner_dialog_id);
|
|
|
|
|
2019-01-21 21:55:26 +01:00
|
|
|
void save_web_page(const WebPage *web_page, WebPageId web_page_id, bool from_binlog);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static string get_web_page_database_key(WebPageId web_page_id);
|
|
|
|
|
|
|
|
void on_save_web_page_to_database(WebPageId web_page_id, bool success);
|
|
|
|
|
|
|
|
void load_web_page_from_database(WebPageId web_page_id, Promise<Unit> promise);
|
|
|
|
|
|
|
|
void on_load_web_page_from_database(WebPageId web_page_id, string value);
|
|
|
|
|
2019-01-21 21:55:26 +01:00
|
|
|
const WebPage *get_web_page_force(WebPageId web_page_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static string get_web_page_instant_view_database_key(WebPageId web_page_id);
|
|
|
|
|
|
|
|
void load_web_page_instant_view(WebPageId web_page_id, bool force_full, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_load_web_page_instant_view_from_database(WebPageId web_page_id, string value);
|
|
|
|
|
|
|
|
void reload_web_page_instant_view(WebPageId web_page_id);
|
|
|
|
|
|
|
|
void update_web_page_instant_view_load_requests(WebPageId web_page_id, bool force_update, Result<> result);
|
|
|
|
|
|
|
|
static string get_web_page_url_database_key(const string &url);
|
|
|
|
|
|
|
|
void load_web_page_by_url(const string &url, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_load_web_page_id_by_url_from_database(const string &url, string value, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_load_web_page_by_url_from_database(WebPageId web_page_id, const string &url, Promise<Unit> &&promise,
|
|
|
|
Result<> result);
|
|
|
|
|
|
|
|
void tear_down() override;
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
FileSourceId get_web_page_file_source_id(WebPage *web_page);
|
|
|
|
|
2019-02-06 00:46:27 +01:00
|
|
|
static void append_rich_text_file_ids(const RichText &rich_text, vector<FileId> &file_ids);
|
|
|
|
|
|
|
|
static void append_page_block_caption_file_ids(const PageBlockCaption &caption, vector<FileId> &file_ids);
|
|
|
|
|
2019-01-21 22:59:55 +01:00
|
|
|
static vector<FileId> get_web_page_file_ids(const WebPage *web_page);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
std::unordered_map<WebPageId, unique_ptr<WebPage>, WebPageIdHash> web_pages_;
|
|
|
|
|
|
|
|
std::unordered_map<WebPageId, vector<Promise<Unit>>, WebPageIdHash> load_web_page_from_database_queries_;
|
|
|
|
std::unordered_set<WebPageId, WebPageIdHash> loaded_from_database_web_pages_;
|
|
|
|
|
|
|
|
struct PendingWebPageInstantViewQueries {
|
|
|
|
vector<Promise<Unit>> partial;
|
|
|
|
vector<Promise<Unit>> full;
|
|
|
|
};
|
|
|
|
std::unordered_map<WebPageId, PendingWebPageInstantViewQueries, WebPageIdHash> load_web_page_instant_view_queries_;
|
|
|
|
|
|
|
|
std::unordered_map<WebPageId, std::unordered_set<FullMessageId, FullMessageIdHash>, WebPageIdHash> pending_web_pages_;
|
2018-02-20 22:20:45 +01:00
|
|
|
std::unordered_map<WebPageId, std::unordered_map<int64, std::pair<string, Promise<Unit>>>, WebPageIdHash>
|
|
|
|
pending_get_web_pages_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-20 22:20:45 +01:00
|
|
|
int64 get_web_page_preview_request_id_ = 1;
|
|
|
|
std::unordered_map<int64, WebPageId> got_web_page_previews_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::unordered_map<string, WebPageId> url_to_web_page_id_;
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
std::unordered_map<string, FileSourceId> url_to_file_source_id_;
|
|
|
|
|
2018-08-01 19:31:20 +02:00
|
|
|
MultiTimeout pending_web_pages_timeout_{"PendingWebPagesTimeout"};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|