From 7f364a7a1875b801f06e3e96d7c6e5fdee30aa6d Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 7 Aug 2024 11:47:37 +0300 Subject: [PATCH] Hide Instant View for album link previews from Instagram and Twitter. --- td/telegram/WebPageBlock.cpp | 15 +++++++++++++++ td/telegram/WebPageBlock.h | 2 ++ td/telegram/WebPagesManager.cpp | 30 ++++++++++++++++++++++++++++-- td/telegram/WebPagesManager.h | 4 ++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/td/telegram/WebPageBlock.cpp b/td/telegram/WebPageBlock.cpp index 50cb3fb25..9d18781ea 100644 --- a/td/telegram/WebPageBlock.cpp +++ b/td/telegram/WebPageBlock.cpp @@ -2491,4 +2491,19 @@ vector> get_page_blocks_object( return get_page_blocks_object(page_blocks, &context); } +bool WebPageBlock::are_allowed_album_block_types(const vector> &page_blocks) { + for (const auto &block : page_blocks) { + switch (block->get_type()) { + case Type::Title: + case Type::AuthorDate: + case Type::Collage: + case Type::Slideshow: + continue; + default: + return false; + } + } + return true; +} + } // namespace td diff --git a/td/telegram/WebPageBlock.h b/td/telegram/WebPageBlock.h index f8574da07..061670aca 100644 --- a/td/telegram/WebPageBlock.h +++ b/td/telegram/WebPageBlock.h @@ -87,6 +87,8 @@ class WebPageBlock { virtual void append_file_ids(const Td *td, vector &file_ids) const = 0; virtual td_api::object_ptr get_page_block_object(Context *context) const = 0; + + static bool are_allowed_album_block_types(const vector> &page_blocks); }; void store(const unique_ptr &block, LogEventStorerCalcLength &storer); diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 66873155e..54ceb228b 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -245,6 +245,8 @@ class WebPagesManager::WebPage { int32 duration_ = 0; string author_; bool has_large_media_ = false; + mutable bool is_album_ = false; + mutable bool is_album_checked_ = false; Document document_; vector documents_; ThemeSettings theme_settings_; @@ -1347,6 +1349,30 @@ bool WebPagesManager::have_web_page(WebPageId web_page_id) const { return get_web_page(web_page_id) != nullptr; } +bool WebPagesManager::can_web_page_be_album(const WebPage *web_page) { + if (web_page->type_ == "telegram_album") { + return true; + } + auto site_name = to_lower(web_page->site_name_); + return site_name == "instagram" || site_name == "twitter" || site_name == "x"; +} + +bool WebPagesManager::is_web_page_album(const WebPage *web_page) { + if (!web_page->is_album_checked_) { + web_page->is_album_checked_ = true; + if (web_page->type_ == "telegram_album") { + web_page->is_album_ = true; + } else if (can_web_page_be_album(web_page) && !web_page->instant_view_.is_empty_) { + if (!web_page->instant_view_.is_loaded_) { + LOG(ERROR) << "Have no instant view for " << web_page->url_; + } else { + web_page->is_album_ = WebPageBlock::are_allowed_album_block_types(web_page->instant_view_.page_blocks_); + } + } + } + return web_page->is_album_; +} + td_api::object_ptr WebPagesManager::get_link_preview_type_object( const WebPage *web_page) const { if (begins_with(web_page->type_, "telegram_")) { @@ -1723,7 +1749,7 @@ td_api::object_ptr WebPagesManager::get_link_preview_object return nullptr; } int32 instant_view_version = [web_page] { - if (web_page->instant_view_.is_empty_ || web_page->type_ == "telegram_album") { + if (web_page->instant_view_.is_empty_ || is_web_page_album(web_page)) { return 0; } if (web_page->instant_view_.is_v2_) { @@ -2292,7 +2318,7 @@ void WebPagesManager::on_load_web_page_from_database(WebPageId web_page_id, stri update_web_page(std::move(result), web_page_id, true, true); const WebPage *web_page = get_web_page(web_page_id); - if (web_page != nullptr && web_page->type_ == "telegram_album" && !web_page->instant_view_.is_empty_ && + if (web_page != nullptr && can_web_page_be_album(web_page) && !web_page->instant_view_.is_empty_ && !web_page->instant_view_.is_loaded_) { LOG(INFO) << "Forcely load instant view of " << web_page_id; on_load_web_page_instant_view_from_database( diff --git a/td/telegram/WebPagesManager.h b/td/telegram/WebPagesManager.h index 8b4778859..67bde5253 100644 --- a/td/telegram/WebPagesManager.h +++ b/td/telegram/WebPagesManager.h @@ -193,6 +193,10 @@ class WebPagesManager final : public Actor { vector get_web_page_file_ids(const WebPage *web_page) const; + static bool can_web_page_be_album(const WebPage *web_page); + + static bool is_web_page_album(const WebPage *web_page); + Td *td_; ActorShared<> parent_; WaitFreeHashMap, WebPageIdHash> web_pages_;