Improved tracking of views for sponsored messages.

This commit is contained in:
23rd 2021-11-02 20:54:15 +03:00
parent 8ad9770118
commit 52bacb3cde
3 changed files with 39 additions and 23 deletions

View File

@ -89,10 +89,6 @@ bool SponsoredMessages::append(not_null<History*> 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*> history) {
list.showedAll = false;
}
void SponsoredMessages::view(const std::vector<Entry>::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<Entry>::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;
}

View File

@ -41,6 +41,8 @@ public:
void clearItems(not_null<History*> history);
[[nodiscard]] MsgId channelPost(const FullMsgId &fullId) const;
void view(const FullMsgId &fullId);
private:
using OwnedItem = std::unique_ptr<HistoryItem, HistoryItem::Destroyer>;
struct Entry {
@ -65,7 +67,7 @@ private:
const MTPSponsoredMessage &message);
void clearOldRequests();
void view(const std::vector<Entry>::iterator entryIt);
const Entry *find(const FullMsgId &fullId) const;
const not_null<Main::Session*> _session;

View File

@ -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);