tdesktop/Telegram/SourceFiles/data/components/sponsored_messages.h

152 lines
3.4 KiB
C
Raw Normal View History

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
*/
#pragma once
#include "base/timer.h"
#include "history/history_item.h"
#include "ui/image/image_location.h"
#include "window/window_session_controller_link_info.h"
2021-09-26 18:24:47 +00:00
class History;
namespace Main {
class Session;
} // namespace Main
namespace Data {
struct SponsoredReportResult final {
using Id = QByteArray;
struct Option final {
Id id = 0;
QString text;
};
using Options = std::vector<Option>;
enum class FinalStep {
Hidden,
Reported,
Premium,
Silence,
};
Options options;
QString title;
QString error;
FinalStep result;
};
struct SponsoredFrom {
QString title;
QString link;
QString buttonText;
PhotoId photoId = PhotoId(0);
uint64 backgroundEmojiId = 0;
uint8 colorIndex : 6 = 0;
bool isLinkInternal = false;
bool isRecommended = false;
bool canReport = false;
};
struct SponsoredMessage {
2021-09-26 18:24:47 +00:00
QByteArray randomId;
SponsoredFrom from;
2021-09-26 18:24:47 +00:00
TextWithEntities textWithEntities;
History *history = nullptr;
QString link;
TextWithEntities sponsorInfo;
TextWithEntities additionalInfo;
2021-09-26 18:24:47 +00:00
};
class SponsoredMessages final {
public:
enum class State {
None,
AppendToEnd,
InjectToMiddle,
};
struct Details {
std::vector<TextWithEntities> info;
QString link;
QString buttonText;
PhotoId photoId = PhotoId(0);
uint64 backgroundEmojiId = 0;
uint8 colorIndex : 6 = 0;
bool isLinkInternal = false;
bool canReport = false;
};
2021-09-26 18:24:47 +00:00
using RandomId = QByteArray;
explicit SponsoredMessages(not_null<Main::Session*> session);
2021-09-26 18:24:47 +00:00
~SponsoredMessages();
[[nodiscard]] bool canHaveFor(not_null<History*> history) const;
void request(not_null<History*> history, Fn<void()> done);
2021-09-26 18:24:47 +00:00
void clearItems(not_null<History*> history);
[[nodiscard]] Details lookupDetails(const FullMsgId &fullId) const;
2023-06-23 16:05:58 +00:00
void clicked(const FullMsgId &fullId);
2021-09-26 18:24:47 +00:00
[[nodiscard]] bool append(not_null<History*> history);
void inject(
not_null<History*> history,
MsgId injectAfterMsgId,
int betweenHeight,
int fallbackWidth);
void view(const FullMsgId &fullId);
[[nodiscard]] State state(not_null<History*> history) const;
[[nodiscard]] auto createReportCallback(const FullMsgId &fullId)
-> Fn<void(SponsoredReportResult::Id, Fn<void(SponsoredReportResult)>)>;
void clear();
2021-09-26 18:24:47 +00:00
private:
using OwnedItem = std::unique_ptr<HistoryItem, HistoryItem::Destroyer>;
struct Entry {
OwnedItem item;
FullMsgId itemFullId;
2021-09-26 18:24:47 +00:00
SponsoredMessage sponsored;
};
struct List {
std::vector<Entry> entries;
// Data between history displays.
size_t injectedCount = 0;
2021-09-26 18:24:47 +00:00
bool showedAll = false;
//
crl::time received = 0;
int postsBetween = 0;
State state = State::None;
2021-09-26 18:24:47 +00:00
};
struct Request {
mtpRequestId requestId = 0;
crl::time lastReceived = 0;
};
void parse(
not_null<History*> history,
const MTPmessages_sponsoredMessages &list);
void append(
not_null<History*> history,
List &list,
const MTPSponsoredMessage &message);
void clearOldRequests();
const Entry *find(const FullMsgId &fullId) const;
2021-09-26 18:24:47 +00:00
const not_null<Main::Session*> _session;
base::Timer _clearTimer;
base::flat_map<not_null<History*>, List> _data;
base::flat_map<not_null<History*>, Request> _requests;
base::flat_map<RandomId, Request> _viewRequests;
rpl::lifetime _lifetime;
};
} // namespace Data