mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-07-26 23:43:06 +02:00
Improved tracking of views for sponsored messages.
This commit is contained in:
parent
8ad9770118
commit
52bacb3cde
3 changed files with 39 additions and 23 deletions
|
@ -89,10 +89,6 @@ bool SponsoredMessages::append(not_null<History*> history) {
|
||||||
HistoryMessageMarkupData());
|
HistoryMessageMarkupData());
|
||||||
entryIt->item.reset(std::move(local));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,16 +167,35 @@ void SponsoredMessages::clearItems(not_null<History*> history) {
|
||||||
list.showedAll = false;
|
list.showedAll = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SponsoredMessages::view(const std::vector<Entry>::iterator entryIt) {
|
const SponsoredMessages::Entry *SponsoredMessages::find(
|
||||||
const auto randomId = entryIt->sponsored.randomId;
|
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];
|
auto &request = _viewRequests[randomId];
|
||||||
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
|
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto history = entryIt->sponsored.history;
|
const auto history = entryPtr->item->history();
|
||||||
if (!history) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
request.requestId = _session->api().request(
|
request.requestId = _session->api().request(
|
||||||
MTPchannels_ViewSponsoredMessage(
|
MTPchannels_ViewSponsoredMessage(
|
||||||
_session->data().channel(history->channelId())->inputChannel,
|
_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 {
|
MsgId SponsoredMessages::channelPost(const FullMsgId &fullId) const {
|
||||||
const auto history = _session->data().history(
|
const auto entryPtr = find(fullId);
|
||||||
peerFromChannel(fullId.channel));
|
if (!entryPtr) {
|
||||||
const auto it = _data.find(history);
|
|
||||||
if (it == end(_data)) {
|
|
||||||
return ShowAtUnreadMsgId;
|
return ShowAtUnreadMsgId;
|
||||||
}
|
}
|
||||||
auto &list = it->second;
|
const auto msgId = entryPtr->sponsored.msgId;
|
||||||
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;
|
|
||||||
return msgId ? msgId : ShowAtUnreadMsgId;
|
return msgId ? msgId : ShowAtUnreadMsgId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,8 @@ public:
|
||||||
void clearItems(not_null<History*> history);
|
void clearItems(not_null<History*> history);
|
||||||
[[nodiscard]] MsgId channelPost(const FullMsgId &fullId) const;
|
[[nodiscard]] MsgId channelPost(const FullMsgId &fullId) const;
|
||||||
|
|
||||||
|
void view(const FullMsgId &fullId);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using OwnedItem = std::unique_ptr<HistoryItem, HistoryItem::Destroyer>;
|
using OwnedItem = std::unique_ptr<HistoryItem, HistoryItem::Destroyer>;
|
||||||
struct Entry {
|
struct Entry {
|
||||||
|
@ -65,7 +67,7 @@ private:
|
||||||
const MTPSponsoredMessage &message);
|
const MTPSponsoredMessage &message);
|
||||||
void clearOldRequests();
|
void clearOldRequests();
|
||||||
|
|
||||||
void view(const std::vector<Entry>::iterator entryIt);
|
const Entry *find(const FullMsgId &fullId) const;
|
||||||
|
|
||||||
const not_null<Main::Session*> _session;
|
const not_null<Main::Session*> _session;
|
||||||
|
|
||||||
|
|
|
@ -72,6 +72,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "data/data_histories.h"
|
#include "data/data_histories.h"
|
||||||
#include "data/data_changes.h"
|
#include "data/data_changes.h"
|
||||||
#include "data/stickers/data_stickers.h"
|
#include "data/stickers/data_stickers.h"
|
||||||
|
#include "data/data_sponsored_messages.h"
|
||||||
#include "facades.h"
|
#include "facades.h"
|
||||||
#include "app.h"
|
#include "app.h"
|
||||||
#include "styles/style_chat.h"
|
#include "styles/style_chat.h"
|
||||||
|
@ -681,6 +682,9 @@ void HistoryInner::paintEvent(QPaintEvent *e) {
|
||||||
if (item->hasViews()) {
|
if (item->hasViews()) {
|
||||||
session().api().views().scheduleIncrement(item);
|
session().api().views().scheduleIncrement(item);
|
||||||
}
|
}
|
||||||
|
if (item->isSponsored()) {
|
||||||
|
session().data().sponsoredMessages().view(item->fullId());
|
||||||
|
}
|
||||||
if (item->isUnreadMention() && !item->isUnreadMedia()) {
|
if (item->isUnreadMention() && !item->isUnreadMedia()) {
|
||||||
readMentions.insert(item);
|
readMentions.insert(item);
|
||||||
_widget->enqueueMessageHighlight(view);
|
_widget->enqueueMessageHighlight(view);
|
||||||
|
@ -741,6 +745,10 @@ void HistoryInner::paintEvent(QPaintEvent *e) {
|
||||||
if (item->hasViews()) {
|
if (item->hasViews()) {
|
||||||
session().api().views().scheduleIncrement(item);
|
session().api().views().scheduleIncrement(item);
|
||||||
}
|
}
|
||||||
|
if (item->isSponsored()) {
|
||||||
|
session().data().sponsoredMessages().view(
|
||||||
|
item->fullId());
|
||||||
|
}
|
||||||
if (item->isUnreadMention() && !item->isUnreadMedia()) {
|
if (item->isUnreadMention() && !item->isUnreadMedia()) {
|
||||||
readMentions.insert(item);
|
readMentions.insert(item);
|
||||||
_widget->enqueueMessageHighlight(view);
|
_widget->enqueueMessageHighlight(view);
|
||||||
|
|
Loading…
Add table
Reference in a new issue