From 7a57174ab16e63315a299209bfa3a08f27564b1d Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 3 Nov 2021 15:49:48 +0400 Subject: [PATCH] Register sponsored view only on full message show. --- .../data/data_sponsored_messages.cpp | 25 ++++++++++++---- .../data/data_sponsored_messages.h | 1 + Telegram/SourceFiles/history/history.cpp | 4 --- Telegram/SourceFiles/history/history.h | 2 -- .../history/history_inner_widget.cpp | 30 ++++++++++--------- .../SourceFiles/history/history_widget.cpp | 8 ++--- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.cpp b/Telegram/SourceFiles/data/data_sponsored_messages.cpp index c6a8813e8..4bd5861b2 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.cpp +++ b/Telegram/SourceFiles/data/data_sponsored_messages.cpp @@ -92,15 +92,24 @@ bool SponsoredMessages::append(not_null history) { return true; } +bool SponsoredMessages::canHaveFor(not_null history) const { + return history->isChannel(); +} + void SponsoredMessages::request(not_null history) { + if (!canHaveFor(history)) { + return; + } auto &request = _requests[history]; if (request.requestId || TooEarlyForRequest(request.lastReceived)) { return; } + const auto channel = history->peer->asChannel(); + Assert(channel != nullptr); request.requestId = _session->api().request( MTPchannels_GetSponsoredMessages( - _session->data().channel(history->channelId())->inputChannel - )).done([=](const MTPmessages_sponsoredMessages &result) { + channel->inputChannel) + ).done([=](const MTPmessages_sponsoredMessages &result) { parse(history, result); }).fail([=](const MTP::Error &error) { _requests.remove(history); @@ -169,6 +178,9 @@ void SponsoredMessages::clearItems(not_null history) { const SponsoredMessages::Entry *SponsoredMessages::find( const FullMsgId &fullId) const { + if (!fullId.channel) { + return nullptr; + } const auto history = _session->data().history( peerFromChannel(fullId.channel)); const auto it = _data.find(history); @@ -195,12 +207,13 @@ void SponsoredMessages::view(const FullMsgId &fullId) { if (request.requestId || TooEarlyForRequest(request.lastReceived)) { return; } - const auto history = entryPtr->item->history(); + const auto channel = entryPtr->item->history()->peer->asChannel(); + Assert(channel != nullptr); request.requestId = _session->api().request( MTPchannels_ViewSponsoredMessage( - _session->data().channel(history->channelId())->inputChannel, - MTP_bytes(randomId) - )).done([=] { + channel->inputChannel, + MTP_bytes(randomId)) + ).done([=] { auto &request = _viewRequests[randomId]; request.lastReceived = crl::now(); request.requestId = 0; diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.h b/Telegram/SourceFiles/data/data_sponsored_messages.h index b4e4d7795..e8e34ab6a 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.h +++ b/Telegram/SourceFiles/data/data_sponsored_messages.h @@ -36,6 +36,7 @@ public: SponsoredMessages &operator=(const SponsoredMessages &other) = delete; ~SponsoredMessages(); + [[nodiscard]] bool canHaveFor(not_null history) const; void request(not_null history); [[nodiscard]] bool append(not_null history); void clearItems(not_null history); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 039fb5e53..dc5c2239c 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -2674,10 +2674,6 @@ QString History::topPromotionMessage() const { return _topPromotedMessage; } -bool History::canHaveSponsoredMessages() const { - return isChannel(); -} - bool History::clearUnreadOnClientSide() const { if (!session().supportMode()) { return false; diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index b0aaf1a41..588d5cdd9 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -282,8 +282,6 @@ public: [[nodiscard]] bool topPromotionAboutShown() const; void markTopPromotionAboutShown(); - [[nodiscard]] bool canHaveSponsoredMessages() const; - MsgId minMsgId() const; MsgId maxMsgId() const; MsgId msgIdForRead() const; diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index a1b4e0813..83f47f439 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -679,18 +679,20 @@ void HistoryInner::paintEvent(QPaintEvent *e) { seltoy - mtop); view->draw(p, context); - if (item->hasViews()) { - session().api().views().scheduleIncrement(item); - } - if (item->isSponsored()) { - session().data().sponsoredMessages().view(item->fullId()); - } - if (item->isUnreadMention() && !item->isUnreadMedia()) { - readMentions.insert(item); - _widget->enqueueMessageHighlight(view); + const auto height = view->height(); + const auto middle = top + height / 2; + const auto bottom = top + height; + if (_visibleAreaBottom >= middle + && _visibleAreaTop <= middle) { + if (item->hasViews()) { + session().api().views().scheduleIncrement(item); + } + if (item->isUnreadMention() && !item->isUnreadMedia()) { + readMentions.insert(item); + _widget->enqueueMessageHighlight(view); + } } - const auto height = view->height(); top += height; context.translate(0, -height); p.translate(0, height); @@ -739,16 +741,16 @@ void HistoryInner::paintEvent(QPaintEvent *e) { if (!item->out() && item->unread()) { readTill = item; } + if (item->isSponsored()) { + session().data().sponsoredMessages().view( + item->fullId()); + } } if (_visibleAreaBottom >= middle && _visibleAreaTop <= middle) { if (item->hasViews()) { session().api().views().scheduleIncrement(item); } - if (item->isSponsored()) { - session().data().sponsoredMessages().view( - item->fullId()); - } if (item->isUnreadMention() && !item->isUnreadMedia()) { readMentions.insert(item); _widget->enqueueMessageHighlight(view); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index acac67acf..f346f79f3 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -2189,11 +2189,9 @@ void HistoryWidget::showHistory( showAboutTopPromotion(); { - const auto hasSponsored = _history->canHaveSponsoredMessages(); - _scroll->setTrackingContent(hasSponsored); - if (hasSponsored) { - session().data().sponsoredMessages().request(_history); - } + auto &sponsored = session().data().sponsoredMessages(); + sponsored.request(_history); + _scroll->setTrackingContent(sponsored.canHaveFor(_history)); } } else { _chooseForReport = nullptr;