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
|
|
|
|
*/
|
2024-04-13 14:56:45 +00:00
|
|
|
#include "data/components/sponsored_messages.h"
|
2021-09-26 18:24:47 +00:00
|
|
|
|
|
|
|
#include "api/api_text_entities.h"
|
|
|
|
#include "apiwrap.h"
|
2024-04-11 23:42:18 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2021-09-26 18:24:47 +00:00
|
|
|
#include "data/data_channel.h"
|
2024-04-11 23:42:18 +00:00
|
|
|
#include "data/data_photo.h"
|
2021-09-26 18:24:47 +00:00
|
|
|
#include "data/data_session.h"
|
2022-11-05 12:41:27 +00:00
|
|
|
#include "data/data_user.h"
|
2021-09-26 18:24:47 +00:00
|
|
|
#include "history/history.h"
|
2022-11-05 12:41:27 +00:00
|
|
|
#include "history/view/history_view_element.h"
|
2023-03-01 13:28:06 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2021-09-26 18:24:47 +00:00
|
|
|
#include "main/main_session.h"
|
2023-03-01 13:28:06 +00:00
|
|
|
#include "ui/text/text_utilities.h" // Ui::Text::RichLangValue.
|
2021-09-26 18:24:47 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-04-13 14:56:45 +00:00
|
|
|
SponsoredMessages::SponsoredMessages(not_null<Main::Session*> session)
|
|
|
|
: _session(session)
|
2021-09-26 18:24:47 +00:00
|
|
|
, _clearTimer([=] { clearOldRequests(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SponsoredMessages::~SponsoredMessages() {
|
2024-04-24 12:35:05 +00:00
|
|
|
Expects(_data.empty());
|
|
|
|
Expects(_requests.empty());
|
|
|
|
Expects(_viewRequests.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::clear() {
|
|
|
|
_lifetime.destroy();
|
|
|
|
for (const auto &request : base::take(_requests)) {
|
|
|
|
_session->api().request(request.second.requestId).cancel();
|
|
|
|
}
|
|
|
|
for (const auto &request : base::take(_viewRequests)) {
|
2021-09-26 18:24:47 +00:00
|
|
|
_session->api().request(request.second.requestId).cancel();
|
|
|
|
}
|
2024-04-24 12:35:05 +00:00
|
|
|
base::take(_data);
|
2021-09-26 18:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-11-05 12:41:27 +00:00
|
|
|
if (list.showedAll
|
|
|
|
|| !TooEarlyForRequest(list.received)
|
|
|
|
|| list.postsBetween) {
|
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;
|
|
|
|
}
|
2023-11-23 11:45:38 +00:00
|
|
|
// SponsoredMessages::Details can be requested within
|
|
|
|
// the constructor of HistoryItem, so itemFullId is used as a key.
|
|
|
|
entryIt->itemFullId = FullMsgId(
|
|
|
|
history->peer->id,
|
|
|
|
_session->data().nextLocalMessageId());
|
2024-02-26 18:24:00 +00:00
|
|
|
entryIt->item.reset(history->addSponsoredMessage(
|
2023-11-23 11:45:38 +00:00
|
|
|
entryIt->itemFullId.msg,
|
2021-12-31 12:47:23 +00:00
|
|
|
entryIt->sponsored.from,
|
|
|
|
entryIt->sponsored.textWithEntities));
|
2021-09-26 18:24:47 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-05 12:41:27 +00:00
|
|
|
void SponsoredMessages::inject(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId injectAfterMsgId,
|
|
|
|
int betweenHeight,
|
|
|
|
int fallbackWidth) {
|
|
|
|
if (!canHaveFor(history)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto it = _data.find(history);
|
|
|
|
if (it == end(_data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &list = it->second;
|
|
|
|
if (!list.postsBetween || (list.entries.size() == list.injectedCount)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const auto entryIt = ranges::find_if(list.entries, [](const auto &e) {
|
|
|
|
return e.item == nullptr;
|
|
|
|
});
|
|
|
|
if (entryIt == end(list.entries)) {
|
|
|
|
list.showedAll = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto lastView = (entryIt != begin(list.entries))
|
|
|
|
? (entryIt - 1)->item->mainView()
|
|
|
|
: (injectAfterMsgId == ShowAtUnreadMsgId)
|
|
|
|
? history->firstUnreadMessage()
|
|
|
|
: [&] {
|
|
|
|
const auto message = history->peer->owner().message(
|
|
|
|
history->peer->id,
|
|
|
|
injectAfterMsgId);
|
|
|
|
return message ? message->mainView() : nullptr;
|
|
|
|
}();
|
|
|
|
if (!lastView || !lastView->block()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto summaryBetween = 0;
|
|
|
|
auto summaryHeight = 0;
|
|
|
|
|
|
|
|
using BlockPtr = std::unique_ptr<HistoryBlock>;
|
|
|
|
using ViewPtr = std::unique_ptr<HistoryView::Element>;
|
|
|
|
auto blockIt = ranges::find(
|
|
|
|
history->blocks,
|
|
|
|
lastView->block(),
|
|
|
|
&BlockPtr::get);
|
|
|
|
if (blockIt == end(history->blocks)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto messages = [&]() -> const std::vector<ViewPtr> & {
|
|
|
|
return (*blockIt)->messages;
|
|
|
|
};
|
|
|
|
auto lastViewIt = ranges::find(messages(), lastView, &ViewPtr::get);
|
|
|
|
while ((summaryBetween < list.postsBetween)
|
|
|
|
|| (summaryHeight < betweenHeight)) {
|
|
|
|
lastViewIt++;
|
|
|
|
if (lastViewIt == end(messages())) {
|
|
|
|
blockIt++;
|
|
|
|
if (blockIt != end(history->blocks)) {
|
|
|
|
lastViewIt = begin(messages());
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
summaryBetween++;
|
|
|
|
const auto viewHeight = (*lastViewIt)->height();
|
|
|
|
summaryHeight += viewHeight
|
|
|
|
? viewHeight
|
|
|
|
: (*lastViewIt)->resizeGetHeight(fallbackWidth);
|
|
|
|
}
|
2023-11-23 11:45:38 +00:00
|
|
|
// SponsoredMessages::Details can be requested within
|
|
|
|
// the constructor of HistoryItem, so itemFullId is used as a key.
|
|
|
|
entryIt->itemFullId = FullMsgId(
|
|
|
|
history->peer->id,
|
|
|
|
_session->data().nextLocalMessageId());
|
2022-11-05 12:41:27 +00:00
|
|
|
const auto makedMessage = history->makeMessage(
|
2023-11-23 11:45:38 +00:00
|
|
|
entryIt->itemFullId.msg,
|
2022-11-05 12:41:27 +00:00
|
|
|
entryIt->sponsored.from,
|
|
|
|
entryIt->sponsored.textWithEntities,
|
|
|
|
(*lastViewIt)->data());
|
|
|
|
entryIt->item.reset(makedMessage.get());
|
|
|
|
history->addNewInTheMiddle(
|
|
|
|
makedMessage.get(),
|
|
|
|
std::distance(begin(history->blocks), blockIt),
|
|
|
|
std::distance(begin(messages()), lastViewIt) + 1);
|
|
|
|
messages().back().get()->setPendingResize();
|
|
|
|
list.injectedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:49:48 +00:00
|
|
|
bool SponsoredMessages::canHaveFor(not_null<History*> history) const {
|
2021-12-09 07:32:54 +00:00
|
|
|
return history->peer->isChannel();
|
2021-11-03 11:49:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 12:41:27 +00:00
|
|
|
void SponsoredMessages::request(not_null<History*> history, Fn<void()> done) {
|
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);
|
2022-11-05 12:41:27 +00:00
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-09-26 18:24:47 +00:00
|
|
|
_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);
|
2022-11-05 12:41:27 +00:00
|
|
|
}
|
|
|
|
if (const auto postsBetween = data.vposts_between()) {
|
|
|
|
list.postsBetween = postsBetween->v;
|
|
|
|
list.state = State::InjectToMiddle;
|
|
|
|
} else {
|
|
|
|
list.state = State::AppendToEnd;
|
2021-09-26 18:24:47 +00:00
|
|
|
}
|
2022-10-19 07:11:24 +00:00
|
|
|
}, [](const MTPDmessages_sponsoredMessagesEmpty &) {
|
2021-09-26 18:24:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SponsoredMessages::append(
|
|
|
|
not_null<History*> history,
|
|
|
|
List &list,
|
|
|
|
const MTPSponsoredMessage &message) {
|
2022-07-10 14:59:52 +00:00
|
|
|
const auto &data = message.data();
|
2021-12-23 16:48:36 +00:00
|
|
|
const auto randomId = data.vrandom_id().v;
|
2024-04-11 23:42:18 +00:00
|
|
|
const auto from = SponsoredFrom{
|
|
|
|
.title = qs(data.vtitle()),
|
|
|
|
.link = qs(data.vurl()),
|
|
|
|
.buttonText = qs(data.vbutton_text()),
|
|
|
|
.photoId = data.vphoto()
|
|
|
|
? history->session().data().processPhoto(*data.vphoto())->id
|
|
|
|
: PhotoId(0),
|
|
|
|
.backgroundEmojiId = data.vcolor().has_value()
|
|
|
|
? data.vcolor()->data().vbackground_emoji_id().value_or_empty()
|
|
|
|
: uint64(0),
|
|
|
|
.colorIndex = uint8(data.vcolor().has_value()
|
|
|
|
? data.vcolor()->data().vcolor().value_or_empty()
|
|
|
|
: 0),
|
|
|
|
.isLinkInternal = !UrlRequiresConfirmation(qs(data.vurl())),
|
|
|
|
.isRecommended = data.is_recommended(),
|
|
|
|
.canReport = data.is_can_report(),
|
2021-12-31 12:47:23 +00:00
|
|
|
};
|
2023-03-01 13:28:06 +00:00
|
|
|
auto sponsorInfo = data.vsponsor_info()
|
|
|
|
? tr::lng_sponsored_info_submenu(
|
|
|
|
tr::now,
|
|
|
|
lt_text,
|
|
|
|
{ .text = qs(*data.vsponsor_info()) },
|
|
|
|
Ui::Text::RichLangValue)
|
|
|
|
: TextWithEntities();
|
|
|
|
auto additionalInfo = TextWithEntities::Simple(
|
|
|
|
data.vadditional_info() ? qs(*data.vadditional_info()) : QString());
|
2021-12-23 16:48:36 +00:00
|
|
|
auto sharedMessage = SponsoredMessage{
|
|
|
|
.randomId = randomId,
|
2021-12-31 12:47:23 +00:00
|
|
|
.from = from,
|
2021-12-23 16:48:36 +00:00
|
|
|
.textWithEntities = {
|
|
|
|
.text = qs(data.vmessage()),
|
|
|
|
.entities = Api::EntitiesFromMTP(
|
|
|
|
_session,
|
|
|
|
data.ventities().value_or_empty()),
|
|
|
|
},
|
|
|
|
.history = history,
|
2024-04-11 23:42:18 +00:00
|
|
|
.link = from.link,
|
2023-03-01 13:28:06 +00:00
|
|
|
.sponsorInfo = std::move(sponsorInfo),
|
|
|
|
.additionalInfo = std::move(additionalInfo),
|
2021-12-23 16:48:36 +00:00
|
|
|
};
|
2023-11-23 11:45:38 +00:00
|
|
|
list.entries.push_back({ nullptr, {}, std::move(sharedMessage) });
|
2021-09-26 18:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-11-05 12:41:27 +00:00
|
|
|
list.injectedCount = 0;
|
2021-09-26 18:24:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 17:54:15 +00:00
|
|
|
const SponsoredMessages::Entry *SponsoredMessages::find(
|
|
|
|
const FullMsgId &fullId) const {
|
2021-12-09 07:32:54 +00:00
|
|
|
if (!peerIsChannel(fullId.peer)) {
|
2021-11-03 11:49:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-09 07:32:54 +00:00
|
|
|
const auto history = _session->data().history(fullId.peer);
|
2021-11-02 17:54:15 +00:00
|
|
|
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) {
|
2023-11-23 11:45:38 +00:00
|
|
|
return e.itemFullId == fullId;
|
2021-11-02 17:54:15 +00:00
|
|
|
});
|
|
|
|
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;
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-09-26 18:24:47 +00:00
|
|
|
_viewRequests.remove(randomId);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:47:23 +00:00
|
|
|
SponsoredMessages::Details SponsoredMessages::lookupDetails(
|
2021-12-23 16:48:36 +00:00
|
|
|
const FullMsgId &fullId) const {
|
2021-11-02 17:54:15 +00:00
|
|
|
const auto entryPtr = find(fullId);
|
|
|
|
if (!entryPtr) {
|
2021-12-31 12:47:23 +00:00
|
|
|
return {};
|
2021-10-07 16:44:33 +00:00
|
|
|
}
|
2023-03-01 13:28:06 +00:00
|
|
|
const auto &data = entryPtr->sponsored;
|
|
|
|
|
|
|
|
using InfoList = std::vector<TextWithEntities>;
|
2023-11-15 15:18:18 +00:00
|
|
|
auto info = (!data.sponsorInfo.text.isEmpty()
|
2023-03-01 13:28:06 +00:00
|
|
|
&& !data.additionalInfo.text.isEmpty())
|
|
|
|
? InfoList{ data.sponsorInfo, data.additionalInfo }
|
|
|
|
: !data.sponsorInfo.text.isEmpty()
|
|
|
|
? InfoList{ data.sponsorInfo }
|
|
|
|
: !data.additionalInfo.text.isEmpty()
|
|
|
|
? InfoList{ data.additionalInfo }
|
|
|
|
: InfoList{};
|
2021-12-23 16:48:36 +00:00
|
|
|
return {
|
2023-03-01 13:28:06 +00:00
|
|
|
.info = std::move(info),
|
2024-04-11 23:42:18 +00:00
|
|
|
.link = data.link,
|
|
|
|
.buttonText = data.from.buttonText,
|
2024-04-16 15:31:45 +00:00
|
|
|
.photoId = data.from.photoId,
|
2024-04-11 23:42:18 +00:00
|
|
|
.backgroundEmojiId = data.from.backgroundEmojiId,
|
|
|
|
.colorIndex = data.from.colorIndex,
|
|
|
|
.isLinkInternal = data.from.isLinkInternal,
|
2024-03-26 23:56:17 +00:00
|
|
|
.canReport = data.from.canReport,
|
2021-12-23 16:48:36 +00:00
|
|
|
};
|
2021-10-07 16:44:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 16:05:58 +00:00
|
|
|
void SponsoredMessages::clicked(const FullMsgId &fullId) {
|
|
|
|
const auto entryPtr = find(fullId);
|
|
|
|
if (!entryPtr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto randomId = entryPtr->sponsored.randomId;
|
|
|
|
const auto channel = entryPtr->item->history()->peer->asChannel();
|
|
|
|
Assert(channel != nullptr);
|
|
|
|
_session->api().request(MTPchannels_ClickSponsoredMessage(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_bytes(randomId)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2024-03-27 01:47:42 +00:00
|
|
|
|
|
|
|
auto SponsoredMessages::createReportCallback(const FullMsgId &fullId)
|
|
|
|
-> Fn<void(SponsoredReportResult::Id, Fn<void(SponsoredReportResult)>)> {
|
|
|
|
using TLChoose = MTPDchannels_sponsoredMessageReportResultChooseOption;
|
|
|
|
using TLAdsHidden = MTPDchannels_sponsoredMessageReportResultAdsHidden;
|
|
|
|
using TLReported = MTPDchannels_sponsoredMessageReportResultReported;
|
|
|
|
using Result = SponsoredReportResult;
|
|
|
|
|
|
|
|
struct State final {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
~State() {
|
|
|
|
qDebug() << "SponsoredMessages Report ~State().";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
};
|
|
|
|
const auto state = std::make_shared<State>();
|
|
|
|
|
|
|
|
return [=](Result::Id optionId, Fn<void(Result)> done) {
|
|
|
|
const auto entry = find(fullId);
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto history = entry->item->history();
|
|
|
|
const auto channel = history->peer->asChannel();
|
|
|
|
if (!channel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-13 21:44:52 +00:00
|
|
|
const auto erase = [=] {
|
|
|
|
const auto it = _data.find(history);
|
|
|
|
if (it != end(_data)) {
|
|
|
|
auto &list = it->second.entries;
|
|
|
|
const auto proj = [&](const Entry &e) {
|
|
|
|
return e.itemFullId == fullId;
|
|
|
|
};
|
|
|
|
list.erase(ranges::remove_if(list, proj), end(list));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (optionId == Result::Id("-1")) {
|
|
|
|
erase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-27 01:47:42 +00:00
|
|
|
state->requestId = _session->api().request(
|
|
|
|
MTPchannels_ReportSponsoredMessage(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_bytes(entry->sponsored.randomId),
|
|
|
|
MTP_bytes(optionId))
|
|
|
|
).done([=](
|
|
|
|
const MTPchannels_SponsoredMessageReportResult &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
if (state->requestId != requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->requestId = 0;
|
|
|
|
done(result.match([&](const TLChoose &data) {
|
|
|
|
const auto t = qs(data.vtitle());
|
|
|
|
auto list = Result::Options();
|
|
|
|
list.reserve(data.voptions().v.size());
|
|
|
|
for (const auto &tl : data.voptions().v) {
|
|
|
|
list.emplace_back(Result::Option{
|
|
|
|
.id = tl.data().voption().v,
|
|
|
|
.text = qs(tl.data().vtext()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Result{ .options = std::move(list), .title = t };
|
|
|
|
}, [](const TLAdsHidden &data) -> Result {
|
|
|
|
return { .result = Result::FinalStep::Hidden };
|
|
|
|
}, [&](const TLReported &data) -> Result {
|
2024-05-13 21:44:52 +00:00
|
|
|
erase();
|
2024-03-31 17:52:19 +00:00
|
|
|
if (optionId == Result::Id("1")) { // I don't like it.
|
|
|
|
return { .result = Result::FinalStep::Silence };
|
|
|
|
}
|
2024-03-27 01:47:42 +00:00
|
|
|
return { .result = Result::FinalStep::Reported };
|
|
|
|
}));
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
state->requestId = 0;
|
|
|
|
if (error.type() == u"PREMIUM_ACCOUNT_REQUIRED"_q) {
|
|
|
|
done({ .result = Result::FinalStep::Premium });
|
|
|
|
} else {
|
|
|
|
done({ .error = error.type() });
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-05 12:41:27 +00:00
|
|
|
SponsoredMessages::State SponsoredMessages::state(
|
|
|
|
not_null<History*> history) const {
|
|
|
|
const auto it = _data.find(history);
|
|
|
|
return (it == end(_data)) ? State::None : it->second.state;
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:24:47 +00:00
|
|
|
} // namespace Data
|