From 624e068f2fe74172909e778421152ab57287cd35 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 14 Aug 2024 13:41:22 +0300 Subject: [PATCH] Added initial ability to display media in sponsored messages. --- .../data/components/sponsored_messages.cpp | 34 +++++++++++ .../data/components/sponsored_messages.h | 4 ++ .../view/media/history_view_web_page.cpp | 59 ++++++++++++++----- .../view/media/history_view_web_page.h | 2 + 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/Telegram/SourceFiles/data/components/sponsored_messages.cpp b/Telegram/SourceFiles/data/components/sponsored_messages.cpp index c20ec967e..24be2842d 100644 --- a/Telegram/SourceFiles/data/components/sponsored_messages.cpp +++ b/Telegram/SourceFiles/data/components/sponsored_messages.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "core/click_handler_types.h" #include "data/data_channel.h" +#include "data/data_document.h" #include "data/data_photo.h" #include "data/data_session.h" #include "data/data_user.h" @@ -269,6 +270,35 @@ void SponsoredMessages::append( const MTPSponsoredMessage &message) { const auto &data = message.data(); const auto randomId = data.vrandom_id().v; + auto mediaPhotoId = PhotoId(0); + auto mediaDocumentId = DocumentId(0); + { + if (data.vmedia()) { + data.vmedia()->match([&](const MTPDmessageMediaPhoto &media) { + if (const auto tlPhoto = media.vphoto()) { + tlPhoto->match([&](const MTPDphoto &data) { + const auto p = history->owner().processPhoto(data); + mediaPhotoId = p->id; + }, [](const MTPDphotoEmpty &) { + }); + } + }, [&](const MTPDmessageMediaDocument &media) { + if (const auto tlDocument = media.vdocument()) { + tlDocument->match([&](const MTPDdocument &data) { + const auto d = history->owner().processDocument(data); + if (d->isVideoFile() + || d->isSilentVideo() + || d->isAnimation() + || d->isGifv()) { + mediaDocumentId = d->id; + } + }, [](const MTPDdocumentEmpty &) { + }); + } + }, [](const auto &) { + }); + } + }; const auto from = SponsoredFrom{ .title = qs(data.vtitle()), .link = qs(data.vurl()), @@ -276,6 +306,8 @@ void SponsoredMessages::append( .photoId = data.vphoto() ? history->session().data().processPhoto(*data.vphoto())->id : PhotoId(0), + .mediaPhotoId = mediaPhotoId, + .mediaDocumentId = mediaDocumentId, .backgroundEmojiId = data.vcolor().has_value() ? data.vcolor()->data().vbackground_emoji_id().value_or_empty() : uint64(0), @@ -396,6 +428,8 @@ SponsoredMessages::Details SponsoredMessages::lookupDetails( .colorIndex = data.from.colorIndex, .isLinkInternal = data.from.isLinkInternal, .canReport = data.from.canReport, + .mediaPhotoId = data.from.mediaPhotoId, + .mediaDocumentId = data.from.mediaDocumentId, }; } diff --git a/Telegram/SourceFiles/data/components/sponsored_messages.h b/Telegram/SourceFiles/data/components/sponsored_messages.h index ca7edbdfe..498809033 100644 --- a/Telegram/SourceFiles/data/components/sponsored_messages.h +++ b/Telegram/SourceFiles/data/components/sponsored_messages.h @@ -44,6 +44,8 @@ struct SponsoredFrom { QString link; QString buttonText; PhotoId photoId = PhotoId(0); + PhotoId mediaPhotoId = PhotoId(0); + DocumentId mediaDocumentId = DocumentId(0); uint64 backgroundEmojiId = 0; uint8 colorIndex : 6 = 0; bool isLinkInternal = false; @@ -73,6 +75,8 @@ public: QString link; QString buttonText; PhotoId photoId = PhotoId(0); + PhotoId mediaPhotoId = PhotoId(0); + DocumentId mediaDocumentId = DocumentId(0); uint64 backgroundEmojiId = 0; uint8 colorIndex : 6 = 0; bool isLinkInternal = false; diff --git a/Telegram/SourceFiles/history/view/media/history_view_web_page.cpp b/Telegram/SourceFiles/history/view/media/history_view_web_page.cpp index b05a01ab8..aef1f527b 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_web_page.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_web_page.cpp @@ -305,6 +305,23 @@ void WebPage::setupAdditionalData() { raw->backgroundEmojiId = details.backgroundEmojiId; raw->colorIndex = details.colorIndex; raw->canReport = details.canReport ? 1 : 0; + raw->hasMedia = (details.mediaPhotoId || details.mediaDocumentId) + ? 1 + : 0; + if (!_attach) { + const auto maybePhoto = details.mediaPhotoId + ? _data->session().data().photo(details.mediaPhotoId) + : nullptr; + const auto maybeDocument = details.mediaDocumentId + ? _data->session().data().document(details.mediaDocumentId) + : nullptr; + _attach = CreateAttach( + _parent, + maybeDocument, + maybePhoto, + _collage, + _data->url); + } } else if (_data->stickerSet) { _additionalData = std::make_unique(StickerSetData()); const auto raw = stickerSetData(); @@ -460,6 +477,9 @@ QSize WebPage::countOptimalSize() { } else { _asArticle = _data->computeDefaultSmallMedia(); } + if (sponsored && sponsored->hasMedia) { + _asArticle = 0; + } // init attach if (!_attach && !_asArticle) { @@ -567,9 +587,10 @@ QSize WebPage::countOptimalSize() { minHeight += st::factcheckFooterSkip + factcheck->footer.minHeight(); } if (_attach) { - const auto attachAtTop = _siteName.isEmpty() - && _title.isEmpty() - && _description.isEmpty(); + const auto attachAtTop = (_siteName.isEmpty() + && _title.isEmpty() + && _description.isEmpty()) + || (sponsored && sponsored->hasMedia); if (!attachAtTop) { minHeight += st::mediaInBubbleSkip; } @@ -622,7 +643,9 @@ QSize WebPage::countCurrentSize(int newWidth) { const auto stickerSet = stickerSetData(); const auto factcheck = factcheckData(); - const auto specialRightPix = (sponsoredData() || stickerSet); + const auto sponsored = sponsoredData(); + const auto specialRightPix = ((sponsored && !sponsored->hasMedia) + || stickerSet); const auto lineHeight = UnitedLineHeight(); const auto factcheckMetrics = factcheck ? computeFactcheckMetrics(_description.countHeight(innerWidth)) @@ -720,9 +743,10 @@ QSize WebPage::countCurrentSize(int newWidth) { } if (_attach) { - const auto attachAtTop = !_siteNameLines - && !_titleLines - && !_descriptionLines; + const auto attachAtTop = (!_siteNameLines + && !_titleLines + && !_descriptionLines) + || (sponsored && sponsored->hasMedia); if (!attachAtTop) { newHeight += st::mediaInBubbleSkip; } @@ -818,6 +842,11 @@ void WebPage::draw(Painter &p, const PaintContext &context) const { const auto sponsored = sponsoredData(); const auto factcheck = factcheckData(); + const auto hasSponsoredMedia = sponsored && sponsored->hasMedia; + if (hasSponsoredMedia && _attach) { + tshift += _attach->height() + st::mediaInBubbleSkip; + } + const auto selected = context.selected(); const auto view = parent(); const auto from = view->data()->contentColorsFrom(); @@ -1078,9 +1107,8 @@ void WebPage::draw(Painter &p, const PaintContext &context) const { tshift += factcheck->footerHeight; } if (_attach) { - const auto attachAtTop = !_siteNameLines - && !_titleLines - && !_descriptionLines; + const auto attachAtTop = hasSponsoredMedia + || (!_siteNameLines && !_titleLines && !_descriptionLines); if (!attachAtTop) { tshift += st::mediaInBubbleSkip; } @@ -1088,7 +1116,9 @@ void WebPage::draw(Painter &p, const PaintContext &context) const { const auto attachLeft = rtl() ? (width() - (inner.left() - bubble.left()) - _attach->width()) : (inner.left() - bubble.left()); - const auto attachTop = tshift - bubble.top(); + const auto attachTop = hasSponsoredMedia + ? inner.top() + : (tshift - bubble.top()); p.translate(attachLeft, attachTop); @@ -1305,9 +1335,10 @@ TextState WebPage::textState(QPoint point, StateRequest request) const { if (inThumb) { result.link = _openl; } else if (_attach) { - const auto attachAtTop = !_siteNameLines - && !_titleLines - && !_descriptionLines; + const auto attachAtTop = (!_siteNameLines + && !_titleLines + && !_descriptionLines) + || (sponsored && sponsored->hasMedia); if (!attachAtTop) { tshift += st::mediaInBubbleSkip; } diff --git a/Telegram/SourceFiles/history/view/media/history_view_web_page.h b/Telegram/SourceFiles/history/view/media/history_view_web_page.h index 31bc076b0..51b47cb21 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_web_page.h +++ b/Telegram/SourceFiles/history/view/media/history_view_web_page.h @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/userpic_view.h" namespace Data { +class DocumentMedia; class Media; class PhotoMedia; } // namespace Data @@ -127,6 +128,7 @@ private: uint8 colorIndex : 6 = 0; uint8 isLinkInternal : 1 = 0; uint8 canReport : 1 = 0; + uint8 hasMedia : 1 = 0; HintData hint; };