api_earn.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_earn.h"
  8. #include "api/api_cloud_password.h"
  9. #include "apiwrap.h"
  10. #include "ui/layers/generic_box.h"
  11. #include "boxes/passcode_box.h"
  12. #include "data/data_channel.h"
  13. #include "data/data_session.h"
  14. #include "lang/lang_keys.h"
  15. #include "main/main_session.h"
  16. #include "ui/basic_click_handlers.h"
  17. #include "ui/widgets/buttons.h"
  18. namespace Api {
  19. void RestrictSponsored(
  20. not_null<ChannelData*> channel,
  21. bool restricted,
  22. Fn<void(QString)> failed) {
  23. channel->session().api().request(MTPchannels_RestrictSponsoredMessages(
  24. channel->inputChannel,
  25. MTP_bool(restricted))
  26. ).done([=](const MTPUpdates &updates) {
  27. channel->session().api().applyUpdates(updates);
  28. }).fail([=](const MTP::Error &error) {
  29. failed(error.type());
  30. }).send();
  31. }
  32. void HandleWithdrawalButton(
  33. RewardReceiver receiver,
  34. not_null<Ui::RippleButton*> button,
  35. std::shared_ptr<Ui::Show> show) {
  36. Expects(receiver.currencyReceiver
  37. || (receiver.creditsReceiver && receiver.creditsAmount));
  38. struct State {
  39. rpl::lifetime lifetime;
  40. bool loading = false;
  41. };
  42. const auto channel = receiver.currencyReceiver;
  43. const auto peer = receiver.creditsReceiver;
  44. const auto state = button->lifetime().make_state<State>();
  45. const auto session = (channel ? &channel->session() : &peer->session());
  46. using ChannelOutUrl = MTPstats_BroadcastRevenueWithdrawalUrl;
  47. using CreditsOutUrl = MTPpayments_StarsRevenueWithdrawalUrl;
  48. session->api().cloudPassword().reload();
  49. const auto processOut = [=] {
  50. if (state->loading) {
  51. return;
  52. } else if (peer && !receiver.creditsAmount()) {
  53. return;
  54. }
  55. state->loading = true;
  56. state->lifetime = session->api().cloudPassword().state(
  57. ) | rpl::take(
  58. 1
  59. ) | rpl::start_with_next([=](const Core::CloudPasswordState &pass) {
  60. state->loading = false;
  61. auto fields = PasscodeBox::CloudFields::From(pass);
  62. fields.customTitle = channel
  63. ? tr::lng_channel_earn_balance_password_title()
  64. : tr::lng_bot_earn_balance_password_title();
  65. fields.customDescription = channel
  66. ? tr::lng_channel_earn_balance_password_description(tr::now)
  67. : tr::lng_bot_earn_balance_password_description(tr::now);
  68. fields.customSubmitButton = tr::lng_passcode_submit();
  69. fields.customCheckCallback = crl::guard(button, [=](
  70. const Core::CloudPasswordResult &result,
  71. QPointer<PasscodeBox> box) {
  72. const auto done = [=](const QString &result) {
  73. if (!result.isEmpty()) {
  74. UrlClickHandler::Open(result);
  75. if (box) {
  76. box->closeBox();
  77. }
  78. }
  79. };
  80. const auto fail = [=](const MTP::Error &error) {
  81. const auto message = error.type();
  82. if (box && !box->handleCustomCheckError(message)) {
  83. show->showToast(message);
  84. }
  85. };
  86. if (channel) {
  87. session->api().request(
  88. MTPstats_GetBroadcastRevenueWithdrawalUrl(
  89. channel->input,
  90. result.result
  91. )).done([=](const ChannelOutUrl &r) {
  92. done(qs(r.data().vurl()));
  93. }).fail(fail).send();
  94. } else if (peer) {
  95. session->api().request(
  96. MTPpayments_GetStarsRevenueWithdrawalUrl(
  97. peer->input,
  98. MTP_long(receiver.creditsAmount()),
  99. result.result
  100. )).done([=](const CreditsOutUrl &r) {
  101. done(qs(r.data().vurl()));
  102. }).fail(fail).send();
  103. }
  104. });
  105. show->show(Box<PasscodeBox>(session, fields));
  106. });
  107. };
  108. button->setClickedCallback([=] {
  109. if (state->loading) {
  110. return;
  111. }
  112. const auto fail = [=](const MTP::Error &error) {
  113. auto box = PrePasswordErrorBox(
  114. error.type(),
  115. session,
  116. TextWithEntities{
  117. tr::lng_channel_earn_out_check_password_about(tr::now),
  118. });
  119. if (box) {
  120. show->show(std::move(box));
  121. state->loading = false;
  122. } else {
  123. processOut();
  124. }
  125. };
  126. if (channel) {
  127. session->api().request(
  128. MTPstats_GetBroadcastRevenueWithdrawalUrl(
  129. channel->input,
  130. MTP_inputCheckPasswordEmpty()
  131. )).fail(fail).send();
  132. } else if (peer) {
  133. session->api().request(
  134. MTPpayments_GetStarsRevenueWithdrawalUrl(
  135. peer->input,
  136. MTP_long(std::numeric_limits<int64_t>::max()),
  137. MTP_inputCheckPasswordEmpty()
  138. )).fail(fail).send();
  139. }
  140. });
  141. }
  142. } // namespace Api