From 6471d43c71920168ed34c6929ad8af0fe679d64b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 13 Dec 2023 00:00:39 +0300 Subject: [PATCH] Added mini preview for some types of webpages. --- .../SourceFiles/data/data_media_types.cpp | 58 ++++++++++++++++--- Telegram/SourceFiles/data/data_web_page.cpp | 2 +- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Telegram/SourceFiles/data/data_media_types.cpp b/Telegram/SourceFiles/data/data_media_types.cpp index 3965657504..8ee9bcdd7c 100644 --- a/Telegram/SourceFiles/data/data_media_types.cpp +++ b/Telegram/SourceFiles/data/data_media_types.cpp @@ -1506,15 +1506,57 @@ bool MediaWebPage::replyPreviewLoaded() const { } ItemPreview MediaWebPage::toPreview(ToPreviewOptions options) const { - auto text = options.ignoreMessageText - ? TextWithEntities() - : options.translated - ? parent()->translatedText() - : parent()->originalText(); - if (text.empty()) { - text = Ui::Text::Colorized(_page->url); + const auto caption = [&] { + const auto text = options.ignoreMessageText + ? TextWithEntities() + : options.translated + ? parent()->translatedText() + : parent()->originalText(); + return text.empty() ? Ui::Text::Colorized(_page->url) : text; + }(); + const auto pageTypeWithPreview = _page->type == WebPageType::Photo + || _page->type == WebPageType::Video + || _page->type == WebPageType::Document; + if (pageTypeWithPreview || !_page->collage.items.empty()) { + if (auto found = FindCachedPreview(options.existing, _page, false)) { + return { .text = caption, .images = { std::move(found) } }; + } + auto context = std::any(); + auto images = std::vector(); + auto prepared = ItemPreviewImage(); + const auto radius = ImageRoundRadius::Small; + if (const auto photo = MediaWebPage::photo()) { + const auto media = photo->createMediaView(); + prepared = PreparePhotoPreview(parent(), media, radius, false); + if (prepared || !prepared.cacheKey) { + images.push_back(std::move(prepared)); + if (!prepared.cacheKey) { + context = media; + } + } + } else { + const auto document = MediaWebPage::document(); + if (document + && document->hasThumbnail() + && (document->isGifv() || document->isVideoFile())) { + const auto media = document->createMediaView(); + prepared = PrepareFilePreview(parent(), media, radius, false); + if (prepared || !prepared.cacheKey) { + images.push_back(std::move(prepared)); + if (!prepared.cacheKey) { + context = media; + } + } + } + } + return { + .text = caption, + .images = std::move(images), + .loadingContext = std::move(context), + }; + } else { + return { .text = caption }; } - return { .text = text }; } TextWithEntities MediaWebPage::notificationText() const { diff --git a/Telegram/SourceFiles/data/data_web_page.cpp b/Telegram/SourceFiles/data/data_web_page.cpp index 7aaeaf5d79..6174c138bd 100644 --- a/Telegram/SourceFiles/data/data_web_page.cpp +++ b/Telegram/SourceFiles/data/data_web_page.cpp @@ -141,7 +141,7 @@ WebPageType ParseWebPageType( const QString &type, const QString &embedUrl, bool hasIV) { - if (type == u"video"_q || !embedUrl.isEmpty()) { + if (type == u"video"_q || type == u"gif"_q || !embedUrl.isEmpty()) { return WebPageType::Video; } else if (type == u"photo"_q) { return WebPageType::Photo;