2021-09-26 18:24:47 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_sponsored_messages.h"
|
|
|
|
|
|
|
|
#include "api/api_text_entities.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_peer_id.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
2021-11-05 11:28:45 +00:00
|
|
|
constexpr auto kRequestTimeLimit = 5 * 60 * crl::time(1000);
|
2021-09-26 18:24:47 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool TooEarlyForRequest(crl::time received) {
|
|
|
|
return (received > 0) && (received + kRequestTimeLimit > crl::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SponsoredMessages::SponsoredMessages(not_null<Session*> owner)
|
|
|
|
: _session(&owner->session())
|
|
|
|
, _clearTimer([=] { clearOldRequests(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SponsoredMessages::~SponsoredMessages() {
|
|
|
|
for (const auto &request : _requests) {
|
|
|
|
_session->api().request(request.second.requestId).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::clearOldRequests() {
|
|
|
|
const auto now = crl::now();
|
|
|
|
while (true) {
|
|
|
|
const auto i = ranges::find_if(_requests, [&](const auto &value) {
|
|
|
|
const auto &request = value.second;
|
|
|
|
return !request.requestId
|
|
|
|
&& (request.lastReceived + kRequestTimeLimit <= now);
|
|
|
|
});
|
|
|
|
if (i == end(_requests)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_requests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SponsoredMessages::append(not_null<History*> history) {
|
|
|
|
const auto it = _data.find(history);
|
|
|
|
if (it == end(_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto &list = it->second;
|
2021-11-05 07:44:51 +00:00
|
|
|
if (list.showedAll || !TooEarlyForRequest(list.received)) {
|
2021-09-26 18:24:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto entryIt = ranges::find_if(list.entries, [](const Entry &e) {
|
|
|
|
return e.item == nullptr;
|
|
|
|
});
|
|
|
|
if (entryIt == end(list.entries)) {
|
|
|
|
list.showedAll = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto flags = MessageFlags(0)
|
|
|
|
| (history->isChannel() ? MessageFlag::Post : MessageFlags(0))
|
|
|
|
| MessageFlag::HasFromId
|
|
|
|
| MessageFlag::IsSponsored
|
2021-11-07 08:06:00 +00:00
|
|
|
| MessageFlag::Local;
|
2021-09-26 18:24:47 +00:00
|
|
|
auto local = history->addNewLocalMessage(
|
|
|
|
_session->data().nextLocalMessageId(),
|
|
|
|
flags,
|
|
|
|
UserId(0),
|
|
|
|
MsgId(0),
|
|
|
|
HistoryItem::NewMessageDate(0),
|
|
|
|
entryIt->sponsored.fromId,
|
|
|
|
QString(),
|
|
|
|
entryIt->sponsored.textWithEntities,
|
|
|
|
MTP_messageMediaEmpty(),
|
|
|
|
HistoryMessageMarkupData());
|
|
|
|
entryIt->item.reset(std::move(local));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:49:48 +00:00
|
|
|
bool SponsoredMessages::canHaveFor(not_null<History*> history) const {
|
|
|
|
return history->isChannel();
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:24:47 +00:00
|
|
|
void SponsoredMessages::request(not_null<History*> history) {
|
2021-11-03 11:49:48 +00:00
|
|
|
if (!canHaveFor(history)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-26 18:24:47 +00:00
|
|
|
auto &request = _requests[history];
|
|
|
|
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-05 07:44:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 11:49:48 +00:00
|
|
|
const auto channel = history->peer->asChannel();
|
|
|
|
Assert(channel != nullptr);
|
2021-09-26 18:24:47 +00:00
|
|
|
request.requestId = _session->api().request(
|
|
|
|
MTPchannels_GetSponsoredMessages(
|
2021-11-03 11:49:48 +00:00
|
|
|
channel->inputChannel)
|
|
|
|
).done([=](const MTPmessages_sponsoredMessages &result) {
|
2021-09-26 18:24:47 +00:00
|
|
|
parse(history, result);
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_requests.remove(history);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::parse(
|
|
|
|
not_null<History*> history,
|
|
|
|
const MTPmessages_sponsoredMessages &list) {
|
|
|
|
auto &request = _requests[history];
|
|
|
|
request.lastReceived = crl::now();
|
|
|
|
request.requestId = 0;
|
|
|
|
if (!_clearTimer.isActive()) {
|
|
|
|
_clearTimer.callOnce(kRequestTimeLimit * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.match([&](const MTPDmessages_sponsoredMessages &data) {
|
|
|
|
_session->data().processUsers(data.vusers());
|
|
|
|
_session->data().processChats(data.vchats());
|
|
|
|
|
|
|
|
const auto &messages = data.vmessages().v;
|
|
|
|
auto &list = _data.emplace(history, List()).first->second;
|
|
|
|
list.entries.clear();
|
2021-11-05 07:44:51 +00:00
|
|
|
list.received = crl::now();
|
2021-09-26 18:24:47 +00:00
|
|
|
for (const auto &message : messages) {
|
|
|
|
append(history, list, message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::append(
|
|
|
|
not_null<History*> history,
|
|
|
|
List &list,
|
|
|
|
const MTPSponsoredMessage &message) {
|
|
|
|
message.match([&](const MTPDsponsoredMessage &data) {
|
|
|
|
const auto randomId = data.vrandom_id().v;
|
|
|
|
auto sharedMessage = SponsoredMessage{
|
|
|
|
.randomId = randomId,
|
|
|
|
.fromId = peerFromMTP(data.vfrom_id()),
|
|
|
|
.textWithEntities = {
|
|
|
|
.text = qs(data.vmessage()),
|
|
|
|
.entities = Api::EntitiesFromMTP(
|
|
|
|
_session,
|
|
|
|
data.ventities().value_or_empty()),
|
|
|
|
},
|
|
|
|
.history = history,
|
2021-10-11 08:04:10 +00:00
|
|
|
.msgId = data.vchannel_post().value_or_empty(),
|
2021-09-26 18:24:47 +00:00
|
|
|
};
|
|
|
|
list.entries.push_back({ nullptr, std::move(sharedMessage) });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::clearItems(not_null<History*> history) {
|
|
|
|
const auto it = _data.find(history);
|
|
|
|
if (it == end(_data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &list = it->second;
|
|
|
|
for (auto &entry : list.entries) {
|
|
|
|
entry.item.reset();
|
|
|
|
}
|
|
|
|
list.showedAll = false;
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:54:15 +00:00
|
|
|
const SponsoredMessages::Entry *SponsoredMessages::find(
|
|
|
|
const FullMsgId &fullId) const {
|
2021-11-03 11:49:48 +00:00
|
|
|
if (!fullId.channel) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-11-02 17:54:15 +00:00
|
|
|
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) {
|
2021-09-26 18:24:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-02 17:54:15 +00:00
|
|
|
const auto randomId = entryPtr->sponsored.randomId;
|
|
|
|
auto &request = _viewRequests[randomId];
|
|
|
|
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
|
2021-09-26 18:24:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-03 11:49:48 +00:00
|
|
|
const auto channel = entryPtr->item->history()->peer->asChannel();
|
|
|
|
Assert(channel != nullptr);
|
2021-09-26 18:24:47 +00:00
|
|
|
request.requestId = _session->api().request(
|
|
|
|
MTPchannels_ViewSponsoredMessage(
|
2021-11-03 11:49:48 +00:00
|
|
|
channel->inputChannel,
|
|
|
|
MTP_bytes(randomId))
|
|
|
|
).done([=] {
|
2021-09-26 18:24:47 +00:00
|
|
|
auto &request = _viewRequests[randomId];
|
|
|
|
request.lastReceived = crl::now();
|
|
|
|
request.requestId = 0;
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_viewRequests.remove(randomId);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:44:33 +00:00
|
|
|
MsgId SponsoredMessages::channelPost(const FullMsgId &fullId) const {
|
2021-11-02 17:54:15 +00:00
|
|
|
const auto entryPtr = find(fullId);
|
|
|
|
if (!entryPtr) {
|
2021-10-07 16:44:33 +00:00
|
|
|
return ShowAtUnreadMsgId;
|
|
|
|
}
|
2021-11-02 17:54:15 +00:00
|
|
|
const auto msgId = entryPtr->sponsored.msgId;
|
2021-10-07 16:44:33 +00:00
|
|
|
return msgId ? msgId : ShowAtUnreadMsgId;
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:24:47 +00:00
|
|
|
} // namespace Data
|