api_premium.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "api/api_premium.h"
  8. #include "api/api_premium_option.h"
  9. #include "api/api_text_entities.h"
  10. #include "apiwrap.h"
  11. #include "base/random.h"
  12. #include "data/data_channel.h"
  13. #include "data/data_document.h"
  14. #include "data/data_peer.h"
  15. #include "data/data_peer_values.h"
  16. #include "data/data_session.h"
  17. #include "data/data_user.h"
  18. #include "history/view/history_view_element.h"
  19. #include "history/history.h"
  20. #include "history/history_item.h"
  21. #include "main/main_app_config.h"
  22. #include "main/main_session.h"
  23. #include "payments/payments_form.h"
  24. #include "ui/text/format_values.h"
  25. namespace Api {
  26. namespace {
  27. [[nodiscard]] GiftCode Parse(const MTPDpayments_checkedGiftCode &data) {
  28. return {
  29. .from = data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(),
  30. .to = data.vto_id() ? peerFromUser(*data.vto_id()) : PeerId(),
  31. .giveawayId = data.vgiveaway_msg_id().value_or_empty(),
  32. .date = data.vdate().v,
  33. .used = data.vused_date().value_or_empty(),
  34. .months = data.vmonths().v,
  35. .giveaway = data.is_via_giveaway(),
  36. };
  37. }
  38. [[nodiscard]] Data::PremiumSubscriptionOptions GiftCodesFromTL(
  39. const QVector<MTPPremiumGiftCodeOption> &tlOptions) {
  40. auto options = PremiumSubscriptionOptionsFromTL(tlOptions);
  41. for (auto i = 0; i < options.size(); i++) {
  42. const auto &tlOption = tlOptions[i].data();
  43. const auto perUserText = Ui::FillAmountAndCurrency(
  44. tlOption.vamount().v / float64(tlOption.vusers().v),
  45. qs(tlOption.vcurrency()),
  46. false);
  47. options[i].costPerMonth = perUserText
  48. + ' '
  49. + QChar(0x00D7)
  50. + ' '
  51. + QString::number(tlOption.vusers().v);
  52. }
  53. return options;
  54. }
  55. } // namespace
  56. Premium::Premium(not_null<ApiWrap*> api)
  57. : _session(&api->session())
  58. , _api(&api->instance()) {
  59. crl::on_main(_session, [=] {
  60. // You can't use _session->user() in the constructor,
  61. // only queued, because it is not constructed yet.
  62. Data::AmPremiumValue(
  63. _session
  64. ) | rpl::start_with_next([=] {
  65. reload();
  66. if (_session->premium()) {
  67. reloadCloudSet();
  68. }
  69. }, _session->lifetime());
  70. });
  71. }
  72. rpl::producer<TextWithEntities> Premium::statusTextValue() const {
  73. return _statusTextUpdates.events_starting_with_copy(
  74. _statusText.value_or(TextWithEntities()));
  75. }
  76. auto Premium::videos() const
  77. -> const base::flat_map<QString, not_null<DocumentData*>> & {
  78. return _videos;
  79. }
  80. rpl::producer<> Premium::videosUpdated() const {
  81. return _videosUpdated.events();
  82. }
  83. auto Premium::stickers() const
  84. -> const std::vector<not_null<DocumentData*>> & {
  85. return _stickers;
  86. }
  87. rpl::producer<> Premium::stickersUpdated() const {
  88. return _stickersUpdated.events();
  89. }
  90. auto Premium::cloudSet() const
  91. -> const std::vector<not_null<DocumentData*>> & {
  92. return _cloudSet;
  93. }
  94. rpl::producer<> Premium::cloudSetUpdated() const {
  95. return _cloudSetUpdated.events();
  96. }
  97. auto Premium::helloStickers() const
  98. -> const std::vector<not_null<DocumentData*>> & {
  99. if (_helloStickers.empty()) {
  100. const_cast<Premium*>(this)->reloadHelloStickers();
  101. }
  102. return _helloStickers;
  103. }
  104. rpl::producer<> Premium::helloStickersUpdated() const {
  105. return _helloStickersUpdated.events();
  106. }
  107. int64 Premium::monthlyAmount() const {
  108. return _monthlyAmount;
  109. }
  110. QString Premium::monthlyCurrency() const {
  111. return _monthlyCurrency;
  112. }
  113. void Premium::reload() {
  114. reloadPromo();
  115. reloadStickers();
  116. }
  117. void Premium::reloadPromo() {
  118. if (_promoRequestId) {
  119. return;
  120. }
  121. _promoRequestId = _api.request(MTPhelp_GetPremiumPromo(
  122. )).done([=](const MTPhelp_PremiumPromo &result) {
  123. _promoRequestId = 0;
  124. const auto &data = result.data();
  125. _session->data().processUsers(data.vusers());
  126. _subscriptionOptions = PremiumSubscriptionOptionsFromTL(
  127. data.vperiod_options().v);
  128. for (const auto &option : data.vperiod_options().v) {
  129. if (option.data().vmonths().v == 1) {
  130. _monthlyAmount = option.data().vamount().v;
  131. _monthlyCurrency = qs(option.data().vcurrency());
  132. }
  133. }
  134. auto text = TextWithEntities{
  135. qs(data.vstatus_text()),
  136. EntitiesFromMTP(_session, data.vstatus_entities().v),
  137. };
  138. _statusText = text;
  139. _statusTextUpdates.fire(std::move(text));
  140. auto videos = base::flat_map<QString, not_null<DocumentData*>>();
  141. const auto count = int(std::min(
  142. data.vvideo_sections().v.size(),
  143. data.vvideos().v.size()));
  144. videos.reserve(count);
  145. for (auto i = 0; i != count; ++i) {
  146. const auto document = _session->data().processDocument(
  147. data.vvideos().v[i]);
  148. if ((!document->isVideoFile() && !document->isGifv())
  149. || !document->supportsStreaming()) {
  150. document->forceIsStreamedAnimation();
  151. }
  152. videos.emplace(
  153. qs(data.vvideo_sections().v[i]),
  154. document);
  155. }
  156. if (_videos != videos) {
  157. _videos = std::move(videos);
  158. _videosUpdated.fire({});
  159. }
  160. }).fail([=] {
  161. _promoRequestId = 0;
  162. }).send();
  163. }
  164. void Premium::reloadStickers() {
  165. if (_stickersRequestId) {
  166. return;
  167. }
  168. _stickersRequestId = _api.request(MTPmessages_GetStickers(
  169. MTP_string("\xe2\xad\x90\xef\xb8\x8f\xe2\xad\x90\xef\xb8\x8f"),
  170. MTP_long(_stickersHash)
  171. )).done([=](const MTPmessages_Stickers &result) {
  172. _stickersRequestId = 0;
  173. result.match([&](const MTPDmessages_stickersNotModified &) {
  174. }, [&](const MTPDmessages_stickers &data) {
  175. _stickersHash = data.vhash().v;
  176. const auto owner = &_session->data();
  177. _stickers.clear();
  178. for (const auto &sticker : data.vstickers().v) {
  179. const auto document = owner->processDocument(sticker);
  180. if (document->isPremiumSticker()) {
  181. _stickers.push_back(document);
  182. }
  183. }
  184. _stickersUpdated.fire({});
  185. });
  186. }).fail([=] {
  187. _stickersRequestId = 0;
  188. }).send();
  189. }
  190. void Premium::reloadCloudSet() {
  191. if (_cloudSetRequestId) {
  192. return;
  193. }
  194. _cloudSetRequestId = _api.request(MTPmessages_GetStickers(
  195. MTP_string("\xf0\x9f\x93\x82\xe2\xad\x90\xef\xb8\x8f"),
  196. MTP_long(_cloudSetHash)
  197. )).done([=](const MTPmessages_Stickers &result) {
  198. _cloudSetRequestId = 0;
  199. result.match([&](const MTPDmessages_stickersNotModified &) {
  200. }, [&](const MTPDmessages_stickers &data) {
  201. _cloudSetHash = data.vhash().v;
  202. const auto owner = &_session->data();
  203. _cloudSet.clear();
  204. for (const auto &sticker : data.vstickers().v) {
  205. const auto document = owner->processDocument(sticker);
  206. if (document->isPremiumSticker()) {
  207. _cloudSet.push_back(document);
  208. }
  209. }
  210. _cloudSetUpdated.fire({});
  211. });
  212. }).fail([=] {
  213. _cloudSetRequestId = 0;
  214. }).send();
  215. }
  216. void Premium::reloadHelloStickers() {
  217. if (_helloStickersRequestId) {
  218. return;
  219. }
  220. _helloStickersRequestId = _api.request(MTPmessages_GetStickers(
  221. MTP_string("\xf0\x9f\x91\x8b\xe2\xad\x90\xef\xb8\x8f"),
  222. MTP_long(_helloStickersHash)
  223. )).done([=](const MTPmessages_Stickers &result) {
  224. _helloStickersRequestId = 0;
  225. result.match([&](const MTPDmessages_stickersNotModified &) {
  226. }, [&](const MTPDmessages_stickers &data) {
  227. _helloStickersHash = data.vhash().v;
  228. const auto owner = &_session->data();
  229. _helloStickers.clear();
  230. for (const auto &sticker : data.vstickers().v) {
  231. const auto document = owner->processDocument(sticker);
  232. if (document->sticker()) {
  233. _helloStickers.push_back(document);
  234. }
  235. }
  236. _helloStickersUpdated.fire({});
  237. });
  238. }).fail([=] {
  239. _helloStickersRequestId = 0;
  240. }).send();
  241. }
  242. void Premium::checkGiftCode(
  243. const QString &slug,
  244. Fn<void(GiftCode)> done) {
  245. if (_giftCodeRequestId) {
  246. if (_giftCodeSlug == slug) {
  247. return;
  248. }
  249. _api.request(_giftCodeRequestId).cancel();
  250. }
  251. _giftCodeSlug = slug;
  252. _giftCodeRequestId = _api.request(MTPpayments_CheckGiftCode(
  253. MTP_string(slug)
  254. )).done([=](const MTPpayments_CheckedGiftCode &result) {
  255. _giftCodeRequestId = 0;
  256. const auto &data = result.data();
  257. _session->data().processUsers(data.vusers());
  258. _session->data().processChats(data.vchats());
  259. done(updateGiftCode(slug, Parse(data)));
  260. }).fail([=](const MTP::Error &error) {
  261. _giftCodeRequestId = 0;
  262. done(updateGiftCode(slug, {}));
  263. }).send();
  264. }
  265. GiftCode Premium::updateGiftCode(
  266. const QString &slug,
  267. const GiftCode &code) {
  268. auto &now = _giftCodes[slug];
  269. if (now != code) {
  270. now = code;
  271. _giftCodeUpdated.fire_copy(slug);
  272. }
  273. return code;
  274. }
  275. rpl::producer<GiftCode> Premium::giftCodeValue(const QString &slug) const {
  276. return _giftCodeUpdated.events_starting_with_copy(
  277. slug
  278. ) | rpl::filter(rpl::mappers::_1 == slug) | rpl::map([=] {
  279. const auto i = _giftCodes.find(slug);
  280. return (i != end(_giftCodes)) ? i->second : GiftCode();
  281. });
  282. }
  283. void Premium::applyGiftCode(const QString &slug, Fn<void(QString)> done) {
  284. _api.request(MTPpayments_ApplyGiftCode(
  285. MTP_string(slug)
  286. )).done([=](const MTPUpdates &result) {
  287. _session->api().applyUpdates(result);
  288. done({});
  289. }).fail([=](const MTP::Error &error) {
  290. done(error.type());
  291. }).send();
  292. }
  293. void Premium::resolveGiveawayInfo(
  294. not_null<PeerData*> peer,
  295. MsgId messageId,
  296. Fn<void(GiveawayInfo)> done) {
  297. Expects(done != nullptr);
  298. _giveawayInfoDone = std::move(done);
  299. if (_giveawayInfoRequestId) {
  300. if (_giveawayInfoPeer == peer
  301. && _giveawayInfoMessageId == messageId) {
  302. return;
  303. }
  304. _api.request(_giveawayInfoRequestId).cancel();
  305. }
  306. _giveawayInfoPeer = peer;
  307. _giveawayInfoMessageId = messageId;
  308. _giveawayInfoRequestId = _api.request(MTPpayments_GetGiveawayInfo(
  309. _giveawayInfoPeer->input,
  310. MTP_int(_giveawayInfoMessageId.bare)
  311. )).done([=](const MTPpayments_GiveawayInfo &result) {
  312. _giveawayInfoRequestId = 0;
  313. auto info = GiveawayInfo();
  314. result.match([&](const MTPDpayments_giveawayInfo &data) {
  315. info.participating = data.is_participating();
  316. info.state = data.is_preparing_results()
  317. ? GiveawayState::Preparing
  318. : GiveawayState::Running;
  319. info.adminChannelId = data.vadmin_disallowed_chat_id()
  320. ? ChannelId(*data.vadmin_disallowed_chat_id())
  321. : ChannelId();
  322. info.disallowedCountry = qs(
  323. data.vdisallowed_country().value_or_empty());
  324. info.tooEarlyDate
  325. = data.vjoined_too_early_date().value_or_empty();
  326. info.startDate = data.vstart_date().v;
  327. }, [&](const MTPDpayments_giveawayInfoResults &data) {
  328. info.state = data.is_refunded()
  329. ? GiveawayState::Refunded
  330. : GiveawayState::Finished;
  331. info.giftCode = qs(data.vgift_code_slug().value_or_empty());
  332. info.activatedCount = data.vactivated_count().value_or_empty();
  333. info.finishDate = data.vfinish_date().v;
  334. info.startDate = data.vstart_date().v;
  335. info.credits = data.vstars_prize().value_or_empty();
  336. });
  337. _giveawayInfoDone(std::move(info));
  338. }).fail([=] {
  339. _giveawayInfoRequestId = 0;
  340. _giveawayInfoDone({});
  341. }).send();
  342. }
  343. const Data::PremiumSubscriptionOptions &Premium::subscriptionOptions() const {
  344. return _subscriptionOptions;
  345. }
  346. rpl::producer<> Premium::someMessageMoneyRestrictionsResolved() const {
  347. return _someMessageMoneyRestrictionsResolved.events();
  348. }
  349. void Premium::resolveMessageMoneyRestrictions(not_null<UserData*> user) {
  350. _resolveMessageMoneyRequiredUsers.emplace(user);
  351. if (!_messageMoneyRequestScheduled
  352. && _resolveMessageMoneyRequestedUsers.empty()) {
  353. _messageMoneyRequestScheduled = true;
  354. crl::on_main(_session, [=] {
  355. requestPremiumRequiredSlice();
  356. });
  357. }
  358. }
  359. void Premium::requestPremiumRequiredSlice() {
  360. _messageMoneyRequestScheduled = false;
  361. if (!_resolveMessageMoneyRequestedUsers.empty()
  362. || _resolveMessageMoneyRequiredUsers.empty()) {
  363. return;
  364. }
  365. constexpr auto kPerRequest = 100;
  366. auto users = MTP_vector_from_range(_resolveMessageMoneyRequiredUsers
  367. | ranges::views::transform(&UserData::inputUser));
  368. if (users.v.size() > kPerRequest) {
  369. auto shortened = users.v;
  370. shortened.resize(kPerRequest);
  371. users = MTP_vector<MTPInputUser>(std::move(shortened));
  372. const auto from = begin(_resolveMessageMoneyRequiredUsers);
  373. _resolveMessageMoneyRequestedUsers = { from, from + kPerRequest };
  374. _resolveMessageMoneyRequiredUsers.erase(from, from + kPerRequest);
  375. } else {
  376. _resolveMessageMoneyRequestedUsers
  377. = base::take(_resolveMessageMoneyRequiredUsers);
  378. }
  379. const auto finish = [=](const QVector<MTPRequirementToContact> &list) {
  380. auto index = 0;
  381. for (const auto &user : base::take(_resolveMessageMoneyRequestedUsers)) {
  382. const auto set = [&](bool requirePremium, int stars) {
  383. using Flag = UserDataFlag;
  384. constexpr auto me = Flag::RequiresPremiumToWrite;
  385. constexpr auto known = Flag::MessageMoneyRestrictionsKnown;
  386. constexpr auto hasPrem = Flag::HasRequirePremiumToWrite;
  387. constexpr auto hasStars = Flag::HasStarsPerMessage;
  388. user->setStarsPerMessage(stars);
  389. user->setFlags((user->flags() & ~(me | hasPrem | hasStars))
  390. | known
  391. | (requirePremium ? (me | hasPrem) : Flag())
  392. | (stars ? hasStars : Flag()));
  393. };
  394. if (index >= list.size()) {
  395. set(false, 0);
  396. continue;
  397. }
  398. list[index++].match([&](const MTPDrequirementToContactEmpty &) {
  399. set(false, 0);
  400. }, [&](const MTPDrequirementToContactPremium &) {
  401. set(true, 0);
  402. }, [&](const MTPDrequirementToContactPaidMessages &data) {
  403. set(false, data.vstars_amount().v);
  404. });
  405. }
  406. if (!_messageMoneyRequestScheduled
  407. && !_resolveMessageMoneyRequiredUsers.empty()) {
  408. _messageMoneyRequestScheduled = true;
  409. crl::on_main(_session, [=] {
  410. requestPremiumRequiredSlice();
  411. });
  412. }
  413. _someMessageMoneyRestrictionsResolved.fire({});
  414. };
  415. _session->api().request(
  416. MTPusers_GetRequirementsToContact(std::move(users))
  417. ).done([=](const MTPVector<MTPRequirementToContact> &result) {
  418. finish(result.v);
  419. }).fail([=] {
  420. finish({});
  421. }).send();
  422. }
  423. PremiumGiftCodeOptions::PremiumGiftCodeOptions(not_null<PeerData*> peer)
  424. : _peer(peer)
  425. , _api(&peer->session().api().instance()) {
  426. }
  427. rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::request() {
  428. return [=](auto consumer) {
  429. auto lifetime = rpl::lifetime();
  430. using TLOption = MTPPremiumGiftCodeOption;
  431. _api.request(MTPpayments_GetPremiumGiftCodeOptions(
  432. MTP_flags(_peer->isChannel()
  433. ? MTPpayments_GetPremiumGiftCodeOptions::Flag::f_boost_peer
  434. : MTPpayments_GetPremiumGiftCodeOptions::Flag(0)),
  435. _peer->input
  436. )).done([=](const MTPVector<TLOption> &result) {
  437. auto tlMapOptions = base::flat_map<Amount, QVector<TLOption>>();
  438. for (const auto &tlOption : result.v) {
  439. const auto &data = tlOption.data();
  440. tlMapOptions[data.vusers().v].push_back(tlOption);
  441. if (qs(data.vcurrency()) == Ui::kCreditsCurrency) {
  442. continue;
  443. }
  444. const auto token = Token{ data.vusers().v, data.vmonths().v };
  445. _stores[token] = Store{
  446. .amount = data.vamount().v,
  447. .currency = qs(data.vcurrency()),
  448. .product = qs(data.vstore_product().value_or_empty()),
  449. .quantity = data.vstore_quantity().value_or_empty(),
  450. };
  451. if (!ranges::contains(_availablePresets, data.vusers().v)) {
  452. _availablePresets.push_back(data.vusers().v);
  453. }
  454. }
  455. for (const auto &[amount, tlOptions] : tlMapOptions) {
  456. if (amount == 1 && _optionsForOnePerson.currencies.empty()) {
  457. for (const auto &option : tlOptions) {
  458. _optionsForOnePerson.months.push_back(
  459. option.data().vmonths().v);
  460. _optionsForOnePerson.totalCosts.push_back(
  461. option.data().vamount().v);
  462. _optionsForOnePerson.currencies.push_back(
  463. qs(option.data().vcurrency()));
  464. }
  465. }
  466. _subscriptionOptions[amount] = GiftCodesFromTL(tlOptions);
  467. }
  468. consumer.put_done();
  469. }).fail([=](const MTP::Error &error) {
  470. consumer.put_error_copy(error.type());
  471. }).send();
  472. return lifetime;
  473. };
  474. }
  475. rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::applyPrepaid(
  476. const Payments::InvoicePremiumGiftCode &invoice,
  477. uint64 prepaidId) {
  478. return [=](auto consumer) {
  479. auto lifetime = rpl::lifetime();
  480. const auto channel = _peer->asChannel();
  481. if (!channel) {
  482. return lifetime;
  483. }
  484. _api.request(MTPpayments_LaunchPrepaidGiveaway(
  485. _peer->input,
  486. MTP_long(prepaidId),
  487. invoice.giveawayCredits
  488. ? Payments::InvoiceCreditsGiveawayToTL(invoice)
  489. : Payments::InvoicePremiumGiftCodeGiveawayToTL(invoice)
  490. )).done([=](const MTPUpdates &result) {
  491. _peer->session().api().applyUpdates(result);
  492. consumer.put_done();
  493. }).fail([=](const MTP::Error &error) {
  494. consumer.put_error_copy(error.type());
  495. }).send();
  496. return lifetime;
  497. };
  498. }
  499. const std::vector<int> &PremiumGiftCodeOptions::availablePresets() const {
  500. return _availablePresets;
  501. }
  502. [[nodiscard]] int PremiumGiftCodeOptions::monthsFromPreset(int monthsIndex) {
  503. Expects(monthsIndex >= 0 && monthsIndex < _availablePresets.size());
  504. return _optionsForOnePerson.months[monthsIndex];
  505. }
  506. Payments::InvoicePremiumGiftCode PremiumGiftCodeOptions::invoice(
  507. int users,
  508. int months) {
  509. const auto randomId = base::RandomValue<uint64>();
  510. const auto token = Token{ users, months };
  511. const auto &store = _stores[token];
  512. return Payments::InvoicePremiumGiftCode{
  513. .currency = store.currency,
  514. .storeProduct = store.product,
  515. .randomId = randomId,
  516. .amount = store.amount,
  517. .storeQuantity = store.quantity,
  518. .users = token.users,
  519. .months = token.months,
  520. };
  521. }
  522. std::vector<GiftOptionData> PremiumGiftCodeOptions::optionsForPeer() const {
  523. auto result = std::vector<GiftOptionData>();
  524. if (!_optionsForOnePerson.currencies.empty()) {
  525. const auto count = int(_optionsForOnePerson.months.size());
  526. result.reserve(count);
  527. for (auto i = 0; i != count; ++i) {
  528. Assert(i < _optionsForOnePerson.totalCosts.size());
  529. Assert(i < _optionsForOnePerson.currencies.size());
  530. result.push_back({
  531. .cost = _optionsForOnePerson.totalCosts[i],
  532. .currency = _optionsForOnePerson.currencies[i],
  533. .months = _optionsForOnePerson.months[i],
  534. });
  535. }
  536. }
  537. return result;
  538. }
  539. Data::PremiumSubscriptionOptions PremiumGiftCodeOptions::options(int amount) {
  540. const auto it = _subscriptionOptions.find(amount);
  541. if (it != end(_subscriptionOptions)) {
  542. return it->second;
  543. } else {
  544. auto tlOptions = QVector<MTPPremiumGiftCodeOption>();
  545. for (auto i = 0; i < _optionsForOnePerson.months.size(); i++) {
  546. tlOptions.push_back(MTP_premiumGiftCodeOption(
  547. MTP_flags(MTPDpremiumGiftCodeOption::Flags(0)),
  548. MTP_int(amount),
  549. MTP_int(_optionsForOnePerson.months[i]),
  550. MTPstring(),
  551. MTPint(),
  552. MTP_string(_optionsForOnePerson.currencies[i]),
  553. MTP_long(_optionsForOnePerson.totalCosts[i] * amount)));
  554. }
  555. _subscriptionOptions[amount] = GiftCodesFromTL(tlOptions);
  556. return _subscriptionOptions[amount];
  557. }
  558. }
  559. auto PremiumGiftCodeOptions::requestStarGifts()
  560. -> rpl::producer<rpl::no_value, QString> {
  561. return [=](auto consumer) {
  562. auto lifetime = rpl::lifetime();
  563. _api.request(MTPpayments_GetStarGifts(
  564. MTP_int(0)
  565. )).done([=](const MTPpayments_StarGifts &result) {
  566. result.match([&](const MTPDpayments_starGifts &data) {
  567. _giftsHash = data.vhash().v;
  568. const auto &list = data.vgifts().v;
  569. const auto session = &_peer->session();
  570. auto gifts = std::vector<Data::StarGift>();
  571. gifts.reserve(list.size());
  572. for (const auto &gift : list) {
  573. if (auto parsed = FromTL(session, gift)) {
  574. gifts.push_back(std::move(*parsed));
  575. }
  576. }
  577. _gifts = std::move(gifts);
  578. }, [&](const MTPDpayments_starGiftsNotModified &) {
  579. });
  580. consumer.put_done();
  581. }).fail([=](const MTP::Error &error) {
  582. consumer.put_error_copy(error.type());
  583. }).send();
  584. return lifetime;
  585. };
  586. }
  587. auto PremiumGiftCodeOptions::starGifts() const
  588. -> const std::vector<Data::StarGift> & {
  589. return _gifts;
  590. }
  591. int PremiumGiftCodeOptions::giveawayBoostsPerPremium() const {
  592. constexpr auto kFallbackCount = 4;
  593. return _peer->session().appConfig().get<int>(
  594. u"giveaway_boosts_per_premium"_q,
  595. kFallbackCount);
  596. }
  597. int PremiumGiftCodeOptions::giveawayCountriesMax() const {
  598. constexpr auto kFallbackCount = 10;
  599. return _peer->session().appConfig().get<int>(
  600. u"giveaway_countries_max"_q,
  601. kFallbackCount);
  602. }
  603. int PremiumGiftCodeOptions::giveawayAddPeersMax() const {
  604. constexpr auto kFallbackCount = 10;
  605. return _peer->session().appConfig().get<int>(
  606. u"giveaway_add_peers_max"_q,
  607. kFallbackCount);
  608. }
  609. int PremiumGiftCodeOptions::giveawayPeriodMax() const {
  610. constexpr auto kFallbackCount = 3600 * 24 * 7;
  611. return _peer->session().appConfig().get<int>(
  612. u"giveaway_period_max"_q,
  613. kFallbackCount);
  614. }
  615. bool PremiumGiftCodeOptions::giveawayGiftsPurchaseAvailable() const {
  616. return _peer->session().appConfig().get<bool>(
  617. u"giveaway_gifts_purchase_available"_q,
  618. false);
  619. }
  620. SponsoredToggle::SponsoredToggle(not_null<Main::Session*> session)
  621. : _api(&session->api().instance()) {
  622. }
  623. rpl::producer<bool> SponsoredToggle::toggled() {
  624. return [=](auto consumer) {
  625. auto lifetime = rpl::lifetime();
  626. _api.request(MTPusers_GetFullUser(
  627. MTP_inputUserSelf()
  628. )).done([=](const MTPusers_UserFull &result) {
  629. consumer.put_next_copy(
  630. result.data().vfull_user().data().is_sponsored_enabled());
  631. }).fail([=] { consumer.put_next(false); }).send();
  632. return lifetime;
  633. };
  634. }
  635. rpl::producer<rpl::no_value, QString> SponsoredToggle::setToggled(bool v) {
  636. return [=](auto consumer) {
  637. auto lifetime = rpl::lifetime();
  638. _api.request(MTPaccount_ToggleSponsoredMessages(
  639. MTP_bool(v)
  640. )).done([=] {
  641. consumer.put_done();
  642. }).fail([=](const MTP::Error &error) {
  643. consumer.put_error_copy(error.type());
  644. }).send();
  645. return lifetime;
  646. };
  647. }
  648. MessageMoneyRestriction ResolveMessageMoneyRestrictions(
  649. not_null<PeerData*> peer,
  650. History *maybeHistory) {
  651. if (const auto channel = peer->asChannel()) {
  652. return {
  653. .starsPerMessage = channel->starsPerMessageChecked(),
  654. .known = true,
  655. };
  656. }
  657. const auto user = peer->asUser();
  658. if (!user) {
  659. return { .known = true };
  660. } else if (user->messageMoneyRestrictionsKnown()) {
  661. return {
  662. .starsPerMessage = user->starsPerMessageChecked(),
  663. .premiumRequired = (user->requiresPremiumToWrite()
  664. && !user->session().premium()),
  665. .known = true,
  666. };
  667. } else if (user->hasStarsPerMessage()) {
  668. return {};
  669. } else if (!user->hasRequirePremiumToWrite()) {
  670. return { .known = true };
  671. } else if (user->flags() & UserDataFlag::MutualContact) {
  672. return { .known = true };
  673. } else if (!maybeHistory) {
  674. return {};
  675. }
  676. const auto update = [&](bool require) {
  677. using Flag = UserDataFlag;
  678. constexpr auto known = Flag::MessageMoneyRestrictionsKnown;
  679. constexpr auto me = Flag::RequiresPremiumToWrite;
  680. user->setFlags((user->flags() & ~me)
  681. | known
  682. | (require ? me : Flag()));
  683. };
  684. // We allow this potentially-heavy loop because in case we've opened
  685. // the chat and have a lot of messages `requires_premium` will be known.
  686. for (const auto &block : maybeHistory->blocks) {
  687. for (const auto &view : block->messages) {
  688. const auto item = view->data();
  689. if (!item->out() && !item->isService()) {
  690. update(false);
  691. return { .known = true };
  692. }
  693. }
  694. }
  695. if (user->isContact() // Here we know, that we're not in his contacts.
  696. && maybeHistory->loadedAtTop() // And no incoming messages.
  697. && maybeHistory->loadedAtBottom()) {
  698. return {
  699. .premiumRequired = !user->session().premium(),
  700. .known = true,
  701. };
  702. }
  703. return {};
  704. }
  705. rpl::producer<DocumentData*> RandomHelloStickerValue(
  706. not_null<Main::Session*> session) {
  707. const auto premium = &session->api().premium();
  708. const auto random = [=] {
  709. const auto &v = premium->helloStickers();
  710. Assert(!v.empty());
  711. return v[base::RandomIndex(v.size())].get();
  712. };
  713. const auto &v = premium->helloStickers();
  714. if (!v.empty()) {
  715. return rpl::single(random());
  716. }
  717. return rpl::single<DocumentData*>(
  718. nullptr
  719. ) | rpl::then(premium->helloStickersUpdated(
  720. ) | rpl::filter([=] {
  721. return !premium->helloStickers().empty();
  722. }) | rpl::take(1) | rpl::map(random));
  723. }
  724. std::optional<Data::StarGift> FromTL(
  725. not_null<Main::Session*> session,
  726. const MTPstarGift &gift) {
  727. return gift.match([&](const MTPDstarGift &data) {
  728. const auto document = session->data().processDocument(
  729. data.vsticker());
  730. const auto remaining = data.vavailability_remains();
  731. const auto total = data.vavailability_total();
  732. if (!document->sticker()) {
  733. return std::optional<Data::StarGift>();
  734. }
  735. return std::optional<Data::StarGift>(Data::StarGift{
  736. .id = uint64(data.vid().v),
  737. .stars = int64(data.vstars().v),
  738. .starsConverted = int64(data.vconvert_stars().v),
  739. .starsToUpgrade = int64(data.vupgrade_stars().value_or_empty()),
  740. .document = document,
  741. .limitedLeft = remaining.value_or_empty(),
  742. .limitedCount = total.value_or_empty(),
  743. .firstSaleDate = data.vfirst_sale_date().value_or_empty(),
  744. .lastSaleDate = data.vlast_sale_date().value_or_empty(),
  745. .upgradable = data.vupgrade_stars().has_value(),
  746. .birthday = data.is_birthday(),
  747. .soldOut = data.is_sold_out(),
  748. });
  749. }, [&](const MTPDstarGiftUnique &data) {
  750. const auto total = data.vavailability_total().v;
  751. auto model = std::optional<Data::UniqueGiftModel>();
  752. auto pattern = std::optional<Data::UniqueGiftPattern>();
  753. for (const auto &attribute : data.vattributes().v) {
  754. attribute.match([&](const MTPDstarGiftAttributeModel &data) {
  755. model = FromTL(session, data);
  756. }, [&](const MTPDstarGiftAttributePattern &data) {
  757. pattern = FromTL(session, data);
  758. }, [&](const MTPDstarGiftAttributeBackdrop &data) {
  759. }, [&](const MTPDstarGiftAttributeOriginalDetails &data) {
  760. });
  761. }
  762. if (!model
  763. || !model->document->sticker()
  764. || !pattern
  765. || !pattern->document->sticker()) {
  766. return std::optional<Data::StarGift>();
  767. }
  768. auto result = Data::StarGift{
  769. .id = uint64(data.vid().v),
  770. .unique = std::make_shared<Data::UniqueGift>(Data::UniqueGift{
  771. .id = data.vid().v,
  772. .slug = qs(data.vslug()),
  773. .title = qs(data.vtitle()),
  774. .ownerAddress = qs(data.vowner_address().value_or_empty()),
  775. .ownerName = qs(data.vowner_name().value_or_empty()),
  776. .ownerId = (data.vowner_id()
  777. ? peerFromMTP(*data.vowner_id())
  778. : PeerId()),
  779. .number = data.vnum().v,
  780. .model = *model,
  781. .pattern = *pattern,
  782. }),
  783. .document = model->document,
  784. .limitedLeft = (total - data.vavailability_issued().v),
  785. .limitedCount = total,
  786. };
  787. const auto unique = result.unique.get();
  788. for (const auto &attribute : data.vattributes().v) {
  789. attribute.match([&](const MTPDstarGiftAttributeModel &data) {
  790. }, [&](const MTPDstarGiftAttributePattern &data) {
  791. }, [&](const MTPDstarGiftAttributeBackdrop &data) {
  792. unique->backdrop = FromTL(data);
  793. }, [&](const MTPDstarGiftAttributeOriginalDetails &data) {
  794. unique->originalDetails = FromTL(session, data);
  795. });
  796. }
  797. return std::make_optional(result);
  798. });
  799. }
  800. std::optional<Data::SavedStarGift> FromTL(
  801. not_null<PeerData*> to,
  802. const MTPsavedStarGift &gift) {
  803. const auto session = &to->session();
  804. const auto &data = gift.data();
  805. auto parsed = FromTL(session, data.vgift());
  806. if (!parsed) {
  807. return {};
  808. } else if (const auto unique = parsed->unique.get()) {
  809. unique->starsForTransfer = data.vtransfer_stars().value_or(-1);
  810. unique->exportAt = data.vcan_export_at().value_or_empty();
  811. }
  812. using Id = Data::SavedStarGiftId;
  813. const auto hasUnique = parsed->unique != nullptr;
  814. return Data::SavedStarGift{
  815. .info = std::move(*parsed),
  816. .manageId = (to->isUser()
  817. ? Id::User(data.vmsg_id().value_or_empty())
  818. : Id::Chat(to, data.vsaved_id().value_or_empty())),
  819. .message = (data.vmessage()
  820. ? TextWithEntities{
  821. .text = qs(data.vmessage()->data().vtext()),
  822. .entities = Api::EntitiesFromMTP(
  823. session,
  824. data.vmessage()->data().ventities().v),
  825. }
  826. : TextWithEntities()),
  827. .starsConverted = int64(data.vconvert_stars().value_or_empty()),
  828. .starsUpgradedBySender = int64(
  829. data.vupgrade_stars().value_or_empty()),
  830. .fromId = (data.vfrom_id()
  831. ? peerFromMTP(*data.vfrom_id())
  832. : PeerId()),
  833. .date = data.vdate().v,
  834. .upgradable = data.is_can_upgrade(),
  835. .anonymous = data.is_name_hidden(),
  836. .pinned = data.is_pinned_to_top() && hasUnique,
  837. .hidden = data.is_unsaved(),
  838. .mine = to->isSelf(),
  839. };
  840. }
  841. Data::UniqueGiftModel FromTL(
  842. not_null<Main::Session*> session,
  843. const MTPDstarGiftAttributeModel &data) {
  844. auto result = Data::UniqueGiftModel{
  845. .document = session->data().processDocument(data.vdocument()),
  846. };
  847. result.name = qs(data.vname());
  848. result.rarityPermille = data.vrarity_permille().v;
  849. return result;
  850. }
  851. Data::UniqueGiftPattern FromTL(
  852. not_null<Main::Session*> session,
  853. const MTPDstarGiftAttributePattern &data) {
  854. auto result = Data::UniqueGiftPattern{
  855. .document = session->data().processDocument(data.vdocument()),
  856. };
  857. result.document->overrideEmojiUsesTextColor(true);
  858. result.name = qs(data.vname());
  859. result.rarityPermille = data.vrarity_permille().v;
  860. return result;
  861. }
  862. Data::UniqueGiftBackdrop FromTL(const MTPDstarGiftAttributeBackdrop &data) {
  863. auto result = Data::UniqueGiftBackdrop();
  864. result.name = qs(data.vname());
  865. result.rarityPermille = data.vrarity_permille().v;
  866. result.centerColor = Ui::ColorFromSerialized(
  867. data.vcenter_color());
  868. result.edgeColor = Ui::ColorFromSerialized(
  869. data.vedge_color());
  870. result.patternColor = Ui::ColorFromSerialized(
  871. data.vpattern_color());
  872. result.textColor = Ui::ColorFromSerialized(
  873. data.vtext_color());
  874. return result;
  875. }
  876. Data::UniqueGiftOriginalDetails FromTL(
  877. not_null<Main::Session*> session,
  878. const MTPDstarGiftAttributeOriginalDetails &data) {
  879. auto result = Data::UniqueGiftOriginalDetails();
  880. result.date = data.vdate().v;
  881. result.senderId = data.vsender_id()
  882. ? peerFromMTP(*data.vsender_id())
  883. : PeerId();
  884. result.recipientId = peerFromMTP(data.vrecipient_id());
  885. result.message = data.vmessage()
  886. ? ParseTextWithEntities(session, *data.vmessage())
  887. : TextWithEntities();
  888. return result;
  889. }
  890. } // namespace Api