From 52bacb3cde799a6b18f5c41d983d41658bed0827 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 2 Nov 2021 20:54:15 +0300 Subject: [PATCH] Improved tracking of views for sponsored messages. --- .../data/data_sponsored_messages.cpp | 50 +++++++++++-------- .../data/data_sponsored_messages.h | 4 +- .../history/history_inner_widget.cpp | 8 +++ 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.cpp b/Telegram/SourceFiles/data/data_sponsored_messages.cpp index ea4d4819d1..c6a8813e83 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.cpp +++ b/Telegram/SourceFiles/data/data_sponsored_messages.cpp @@ -89,10 +89,6 @@ bool SponsoredMessages::append(not_null history) { HistoryMessageMarkupData()); entryIt->item.reset(std::move(local)); - // Since sponsored posts are only created on demand for display, - // we can send a request to view immediately. - view(entryIt); - return true; } @@ -171,16 +167,35 @@ void SponsoredMessages::clearItems(not_null history) { list.showedAll = false; } -void SponsoredMessages::view(const std::vector::iterator entryIt) { - const auto randomId = entryIt->sponsored.randomId; +const SponsoredMessages::Entry *SponsoredMessages::find( + const FullMsgId &fullId) const { + const auto history = _session->data().history( + peerFromChannel(fullId.channel)); + const auto it = _data.find(history); + if (it == end(_data)) { + return nullptr; + } + auto &list = it->second; + const auto entryIt = ranges::find_if(list.entries, [&](const Entry &e) { + return e.item->fullId() == fullId; + }); + if (entryIt == end(list.entries)) { + return nullptr; + } + return &*entryIt; +} + +void SponsoredMessages::view(const FullMsgId &fullId) { + const auto entryPtr = find(fullId); + if (!entryPtr) { + return; + } + const auto randomId = entryPtr->sponsored.randomId; auto &request = _viewRequests[randomId]; if (request.requestId || TooEarlyForRequest(request.lastReceived)) { return; } - const auto history = entryIt->sponsored.history; - if (!history) { - return; - } + const auto history = entryPtr->item->history(); request.requestId = _session->api().request( MTPchannels_ViewSponsoredMessage( _session->data().channel(history->channelId())->inputChannel, @@ -195,20 +210,11 @@ void SponsoredMessages::view(const std::vector::iterator entryIt) { } MsgId SponsoredMessages::channelPost(const FullMsgId &fullId) const { - const auto history = _session->data().history( - peerFromChannel(fullId.channel)); - const auto it = _data.find(history); - if (it == end(_data)) { + const auto entryPtr = find(fullId); + if (!entryPtr) { return ShowAtUnreadMsgId; } - auto &list = it->second; - const auto entryIt = ranges::find_if(list.entries, [&](const Entry &e) { - return e.item->fullId() == fullId; - }); - if (entryIt == end(list.entries)) { - return ShowAtUnreadMsgId; - } - const auto msgId = entryIt->sponsored.msgId; + const auto msgId = entryPtr->sponsored.msgId; return msgId ? msgId : ShowAtUnreadMsgId; } diff --git a/Telegram/SourceFiles/data/data_sponsored_messages.h b/Telegram/SourceFiles/data/data_sponsored_messages.h index 475c3c20aa..b4e4d77955 100644 --- a/Telegram/SourceFiles/data/data_sponsored_messages.h +++ b/Telegram/SourceFiles/data/data_sponsored_messages.h @@ -41,6 +41,8 @@ public: void clearItems(not_null history); [[nodiscard]] MsgId channelPost(const FullMsgId &fullId) const; + void view(const FullMsgId &fullId); + private: using OwnedItem = std::unique_ptr; struct Entry { @@ -65,7 +67,7 @@ private: const MTPSponsoredMessage &message); void clearOldRequests(); - void view(const std::vector::iterator entryIt); + const Entry *find(const FullMsgId &fullId) const; const not_null _session; diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 831fee3255..a1b4e0813e 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -72,6 +72,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_histories.h" #include "data/data_changes.h" #include "data/stickers/data_stickers.h" +#include "data/data_sponsored_messages.h" #include "facades.h" #include "app.h" #include "styles/style_chat.h" @@ -681,6 +682,9 @@ void HistoryInner::paintEvent(QPaintEvent *e) { 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); @@ -741,6 +745,10 @@ void HistoryInner::paintEvent(QPaintEvent *e) { 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);