Removed display of stale sponsored messages.

This commit is contained in:
23rd 2021-11-05 10:44:51 +03:00
parent 5565da5308
commit 55eb381bd2
3 changed files with 18 additions and 5 deletions

View File

@ -19,7 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Data {
namespace {
constexpr auto kRequestTimeLimit = 5 * 60 * crl::time(1000);
constexpr auto kRequestTimeLimit = 10 * 60 * crl::time(1000);
[[nodiscard]] bool TooEarlyForRequest(crl::time received) {
return (received > 0) && (received + kRequestTimeLimit > crl::now());
@ -59,7 +59,7 @@ bool SponsoredMessages::append(not_null<History*> history) {
return false;
}
auto &list = it->second;
if (list.showedAll) {
if (list.showedAll || !TooEarlyForRequest(list.received)) {
return false;
}
@ -104,6 +104,19 @@ void SponsoredMessages::request(not_null<History*> history) {
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
return;
}
{
const auto it = _data.find(history);
if (it != end(_data)) {
auto &list = it->second;
// Don't rebuild currently displayed messages.
const auto proj = [](const Entry &e) {
return e.item != nullptr;
};
if (ranges::any_of(list.entries, proj)) {
return;
}
}
}
const auto channel = history->peer->asChannel();
Assert(channel != nullptr);
request.requestId = _session->api().request(
@ -131,11 +144,9 @@ void SponsoredMessages::parse(
_session->data().processChats(data.vchats());
const auto &messages = data.vmessages().v;
if (messages.isEmpty()) {
return;
}
auto &list = _data.emplace(history, List()).first->second;
list.entries.clear();
list.received = crl::now();
for (const auto &message : messages) {
append(history, list, message);
}

View File

@ -53,6 +53,7 @@ private:
struct List {
std::vector<Entry> entries;
bool showedAll = false;
crl::time received = 0;
};
struct Request {
mtpRequestId requestId = 0;

View File

@ -3010,6 +3010,7 @@ void HistoryWidget::loadMessagesDown() {
auto loadMigrated = _migrated && !(_migrated->isEmpty() || _migrated->loadedAtBottom() || (!_history->isEmpty() && !_history->loadedAtTop()));
auto from = loadMigrated ? _migrated : _history;
if (from->loadedAtBottom()) {
session().data().sponsoredMessages().request(_history);
return;
}