tdesktop/Telegram/SourceFiles/api/api_premium.cpp

191 lines
4.9 KiB
C++
Raw Normal View History

2022-05-31 17:11:59 +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 "api/api_premium.h"
#include "api/api_premium_option.h"
2022-05-31 17:11:59 +00:00
#include "api/api_text_entities.h"
#include "main/main_session.h"
#include "data/data_peer_values.h"
2022-06-10 08:03:47 +00:00
#include "data/data_document.h"
2022-05-31 17:11:59 +00:00
#include "data/data_session.h"
#include "data/data_peer.h"
#include "apiwrap.h"
namespace Api {
Premium::Premium(not_null<ApiWrap*> api)
: _session(&api->session())
, _api(&api->instance()) {
crl::on_main(_session, [=] {
// You can't use _session->user() in the constructor,
// only queued, because it is not constructed yet.
Data::AmPremiumValue(
_session
2022-06-10 08:03:47 +00:00
) | rpl::start_with_next([=] {
2022-05-31 17:11:59 +00:00
reload();
if (_session->premium()) {
reloadCloudSet();
}
2022-05-31 17:11:59 +00:00
}, _session->lifetime());
});
}
rpl::producer<TextWithEntities> Premium::statusTextValue() const {
return _statusTextUpdates.events_starting_with_copy(
_statusText.value_or(TextWithEntities()));
}
2022-06-10 08:03:47 +00:00
auto Premium::videos() const
-> const base::flat_map<QString, not_null<DocumentData*>> & {
return _videos;
}
rpl::producer<> Premium::videosUpdated() const {
return _videosUpdated.events();
}
auto Premium::stickers() const
-> const std::vector<not_null<DocumentData*>> & {
return _stickers;
}
rpl::producer<> Premium::stickersUpdated() const {
return _stickersUpdated.events();
}
auto Premium::cloudSet() const
-> const std::vector<not_null<DocumentData*>> & {
return _cloudSet;
}
rpl::producer<> Premium::cloudSetUpdated() const {
return _cloudSetUpdated.events();
}
int64 Premium::monthlyAmount() const {
return _monthlyAmount;
}
QString Premium::monthlyCurrency() const {
return _monthlyCurrency;
}
2022-05-31 17:11:59 +00:00
void Premium::reload() {
2022-06-10 08:03:47 +00:00
reloadPromo();
reloadStickers();
}
void Premium::reloadPromo() {
if (_promoRequestId) {
2022-05-31 17:11:59 +00:00
return;
}
2022-06-10 08:03:47 +00:00
_promoRequestId = _api.request(MTPhelp_GetPremiumPromo(
2022-05-31 17:11:59 +00:00
)).done([=](const MTPhelp_PremiumPromo &result) {
2022-06-10 08:03:47 +00:00
_promoRequestId = 0;
const auto &data = result.data();
_session->data().processUsers(data.vusers());
_subscriptionOptions = SubscriptionOptionsFromTL(
data.vperiod_options().v);
for (const auto &option : data.vperiod_options().v) {
if (option.data().vmonths().v == 1) {
_monthlyAmount = option.data().vamount().v;
_monthlyCurrency = qs(option.data().vcurrency());
2022-08-24 06:30:11 +00:00
}
}
auto text = TextWithEntities{
qs(data.vstatus_text()),
EntitiesFromMTP(_session, data.vstatus_entities().v),
};
_statusText = text;
_statusTextUpdates.fire(std::move(text));
auto videos = base::flat_map<QString, not_null<DocumentData*>>();
const auto count = int(std::min(
data.vvideo_sections().v.size(),
data.vvideos().v.size()));
videos.reserve(count);
for (auto i = 0; i != count; ++i) {
const auto document = _session->data().processDocument(
data.vvideos().v[i]);
if ((!document->isVideoFile() && !document->isGifv())
|| !document->supportsStreaming()) {
document->forceIsStreamedAnimation();
2022-06-10 08:03:47 +00:00
}
videos.emplace(
qs(data.vvideo_sections().v[i]),
document);
}
if (_videos != videos) {
_videos = std::move(videos);
_videosUpdated.fire({});
}
2022-06-10 08:03:47 +00:00
}).fail([=] {
_promoRequestId = 0;
}).send();
}
void Premium::reloadStickers() {
if (_stickersRequestId) {
return;
}
_stickersRequestId = _api.request(MTPmessages_GetStickers(
MTP_string("\xe2\xad\x90\xef\xb8\x8f\xe2\xad\x90\xef\xb8\x8f"),
MTP_long(_stickersHash)
)).done([=](const MTPmessages_Stickers &result) {
_stickersRequestId = 0;
result.match([&](const MTPDmessages_stickersNotModified &) {
}, [&](const MTPDmessages_stickers &data) {
_stickersHash = data.vhash().v;
const auto owner = &_session->data();
_stickers.clear();
for (const auto &sticker : data.vstickers().v) {
const auto document = owner->processDocument(sticker);
if (document->isPremiumSticker()) {
_stickers.push_back(document);
}
}
_stickersUpdated.fire({});
2022-05-31 17:11:59 +00:00
});
}).fail([=] {
2022-06-10 08:03:47 +00:00
_stickersRequestId = 0;
2022-05-31 17:11:59 +00:00
}).send();
}
void Premium::reloadCloudSet() {
if (_cloudSetRequestId) {
return;
}
_cloudSetRequestId = _api.request(MTPmessages_GetStickers(
MTP_string("\xf0\x9f\x93\x82\xe2\xad\x90\xef\xb8\x8f"),
MTP_long(_cloudSetHash)
)).done([=](const MTPmessages_Stickers &result) {
_cloudSetRequestId = 0;
result.match([&](const MTPDmessages_stickersNotModified &) {
}, [&](const MTPDmessages_stickers &data) {
_cloudSetHash = data.vhash().v;
const auto owner = &_session->data();
_cloudSet.clear();
for (const auto &sticker : data.vstickers().v) {
const auto document = owner->processDocument(sticker);
if (document->isPremiumSticker()) {
_cloudSet.push_back(document);
}
}
_cloudSetUpdated.fire({});
});
}).fail([=] {
_cloudSetRequestId = 0;
}).send();
}
const Data::SubscriptionOptions &Premium::subscriptionOptions() const {
return _subscriptionOptions;
}
2022-05-31 17:11:59 +00:00
} // namespace Api