Skip to content

Commit

Permalink
Migrated API for giveaways from MTP to TDLib.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd authored and john-preston committed Nov 19, 2023
1 parent d5b3b89 commit 7863556
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 18 deletions.
78 changes: 74 additions & 4 deletions Telegram/SourceFiles/api/api_premium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For license and copyright information please follow this link:
#include "api/api_text_entities.h"
#include "apiwrap.h"
#include "base/random.h"
#include "data/data_channel.h"
#include "data/data_document.h"
#include "data/data_peer.h"
#include "data/data_peer_values.h"
Expand Down Expand Up @@ -54,25 +55,36 @@ using namespace Tdb;
.giveaway = data.is_via_giveaway(),
};
}
#endif

#if 0 // mtp
[[nodiscard]] Data::SubscriptionOptions GiftCodesFromTL(
const QVector<MTPPremiumGiftCodeOption> &tlOptions) {
#endif
[[nodiscard]] Data::SubscriptionOptions GiftCodesFromTL(
const QVector<Tdb::TLpremiumGiftCodePaymentOption> &tlOptions) {
auto options = SubscriptionOptionsFromTL(tlOptions);
for (auto i = 0; i < options.size(); i++) {
const auto &tlOption = tlOptions[i].data();
const auto perUserText = Ui::FillAmountAndCurrency(
#if 0 // mtp
tlOption.vamount().v / float64(tlOption.vusers().v),
qs(tlOption.vcurrency()),
#endif
tlOption.vamount().v / float64(tlOption.vuser_count().v),
tlOption.vcurrency().v.toUtf8(),
false);
options[i].costPerMonth = perUserText
+ ' '
+ QChar(0x00D7)
+ ' '
+ QString::number(tlOption.vuser_count().v);
#if 0 // mtp
+ QString::number(tlOption.vusers().v);
#endif
}
return options;
}
#endif

} // namespace

Expand Down Expand Up @@ -595,7 +607,7 @@ rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::request() {
return lifetime;
}

#if 0 // todo
#if 0 // mtp
using TLOption = MTPPremiumGiftCodeOption;
_api.request(MTPpayments_GetPremiumGiftCodeOptions(
MTP_flags(
Expand Down Expand Up @@ -635,6 +647,43 @@ rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::request() {
consumer.put_error_copy(error.type());
}).send();
#endif
using TLOption = Tdb::TLpremiumGiftCodePaymentOption;
_api.request(Tdb::TLgetPremiumGiftCodePaymentOptions(
peerToTdbChat(_peer->id)
)).done([=](const Tdb::TLDpremiumGiftCodePaymentOptions &data) {
auto tlMapOptions = base::flat_map<Amount, QVector<TLOption>>();
for (const auto &tlOption : data.voptions().v) {
const auto &data = tlOption.data();
const auto userCount = data.vuser_count().v;
tlMapOptions[userCount].push_back(tlOption);

const auto token = Token{ userCount, data.vmonth_count().v };
_stores[token] = Store{
.amount = uint64(data.vamount().v),
.product = data.vstore_product_id().v.toUtf8(),
.quantity = data.vstore_product_quantity().v,
};
if (!ranges::contains(_availablePresets, userCount)) {
_availablePresets.push_back(userCount);
}
}
for (const auto &[amount, tlOptions] : tlMapOptions) {
if (amount == 1 && _optionsForOnePerson.currency.isEmpty()) {
_optionsForOnePerson.currency =
tlOptions.front().data().vcurrency().v.toUtf8();
for (const auto &option : tlOptions) {
_optionsForOnePerson.months.push_back(
option.data().vmonth_count().v);
_optionsForOnePerson.totalCosts.push_back(
option.data().vamount().v);
}
}
_subscriptionOptions[amount] = GiftCodesFromTL(tlOptions);
}
consumer.put_done();
}).fail([=](const Tdb::Error &error) {
consumer.put_error_copy(error.message);
}).send();

return lifetime;
};
Expand All @@ -650,7 +699,7 @@ rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::applyPrepaid(
return lifetime;
}

#if 0 // todo
#if 0 // mtp
_api.request(MTPpayments_LaunchPrepaidGiveaway(
_peer->input,
MTP_long(prepaidId),
Expand All @@ -662,6 +711,16 @@ rpl::producer<rpl::no_value, QString> PremiumGiftCodeOptions::applyPrepaid(
consumer.put_error_copy(error.type());
}).send();
#endif
using InvoiceGiveaway = Payments::InvoicePremiumGiftCodeGiveaway;
const auto &giveaway = v::get<InvoiceGiveaway>(invoice.purpose);
_api.request(Tdb::TLlaunchPrepaidPremiumGiveaway(
Tdb::tl_int64(prepaidId),
Payments::InvoiceGiftCodeGiveawayToTL(invoice)
)).done([=](const Tdb::TLok &) {
consumer.put_done();
}).fail([=](const Tdb::Error &error) {
consumer.put_error_copy(error.message);
}).send();

return lifetime;
};
Expand Down Expand Up @@ -697,7 +756,7 @@ Data::SubscriptionOptions PremiumGiftCodeOptions::options(int amount) {
if (it != end(_subscriptionOptions)) {
return it->second;
} else {
#if 0 // todo
#if 0 // mtp
auto tlOptions = QVector<MTPPremiumGiftCodeOption>();
for (auto i = 0; i < _optionsForOnePerson.months.size(); i++) {
tlOptions.push_back(MTP_premiumGiftCodeOption(
Expand All @@ -711,6 +770,17 @@ Data::SubscriptionOptions PremiumGiftCodeOptions::options(int amount) {
}
_subscriptionOptions[amount] = GiftCodesFromTL(tlOptions);
#endif
auto tlOptions = QVector<Tdb::TLpremiumGiftCodePaymentOption>();
for (auto i = 0; i < _optionsForOnePerson.months.size(); i++) {
tlOptions.push_back(Tdb::tl_premiumGiftCodePaymentOption(
Tdb::tl_string(_optionsForOnePerson.currency),
Tdb::tl_int53(_optionsForOnePerson.totalCosts[i] * amount),
Tdb::tl_int32(amount),
Tdb::tl_int32(_optionsForOnePerson.months[i]),
Tdb::TLstring(),
Tdb::TLint32()));
}
_subscriptionOptions[amount] = GiftCodesFromTL(tlOptions);
return _subscriptionOptions[amount];
}
}
Expand Down
15 changes: 8 additions & 7 deletions Telegram/SourceFiles/api/api_premium_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ template<typename Option>
#endif
return min.vamount().v / float64(min.vmonth_count().v);
}();
using TLGiftCode = Tdb::TLpremiumGiftCodePaymentOption;
result.reserve(tlOptions.size());
for (const auto &tlOption : tlOptions) {
const auto &option = tlOption.data();
Expand All @@ -51,10 +52,6 @@ template<typename Option>
const auto amount = option.vamount().v;
const auto currency = qs(option.vcurrency());
#endif
const auto lnk = option.vpayment_link();
if (!lnk) {
continue;
}
#if 0 // mtp
const auto botUrl = !lnk
? QString()
Expand All @@ -78,9 +75,13 @@ template<typename Option>
amount,
currency,
botUrl));
result.back().startPayment = [=, link = *lnk](QVariant context) {
Core::HandleLocalUrl(link, context);
};
if constexpr (!std::is_same_v<Option, TLGiftCode>) {
if (const auto lnk = option.vpayment_link()) {
result.back().startPayment = [=, link = *lnk](QVariant c) {
Core::HandleLocalUrl(link, c);
};
}
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ For license and copyright information please follow this link:
#include "ui/painter.h"
#include "styles/style_giveaway.h"

#include "tdb/tdb_tl_scheme.h"

namespace Giveaway {
namespace {

Expand Down Expand Up @@ -244,7 +246,7 @@ Main::Session &MyChannelsListController::session() const {

void MyChannelsListController::prepare() {
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
#if 0 // todo
#if 0 // mtp
const auto api = _apiLifetime.make_state<MTP::Sender>(
&session().api().instance());
api->request(
Expand Down Expand Up @@ -275,6 +277,37 @@ void MyChannelsListController::prepare() {
}
}
}
#endif
const auto api = _apiLifetime.make_state<Tdb::Sender>(
&session().sender());
api->request(
Tdb::TLgetChatsToSendStories()
).done([=](const Tdb::TLDchats &data) {
_apiLifetime.destroy();
auto &owner = session().data();
for (const auto &peerId : data.vchat_ids().v) {
const auto maybePeer = session().data().peerLoaded(
peerFromTdbChat(peerId));
if (maybePeer) {
const auto peer = not_null{ maybePeer };
if (!peer->isChannel() || (peer == _peer)) {
continue;
}
if (!delegate()->peerListFindRow(peer->id.value)) {
if (const auto channel = peer->asChannel()) {
auto row = createRow(channel);
const auto raw = row.get();
delegate()->peerListAppendRow(std::move(row));
if (ranges::contains(_selected, peer)) {
delegate()->peerListSetRowChecked(raw, true);
_selected.erase(
ranges::remove(_selected, peer),
end(_selected));
}
}
}
}
}
for (const auto &selected : _selected) {
if (const auto channel = selected->asChannel()) {
auto row = createRow(channel);
Expand All @@ -286,7 +319,6 @@ void MyChannelsListController::prepare() {
delegate()->peerListRefreshRows();
_selected.clear();
}).send();
#endif
}

void MyChannelsListController::setCheckError(Fn<bool(int)> callback) {
Expand Down
57 changes: 52 additions & 5 deletions Telegram/SourceFiles/payments/payments_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ MTPinputStorePaymentPurpose InvoicePremiumGiftCodeGiveawayToTL(
}
#endif

Tdb::TLpremiumGiveawayParameters InvoiceGiftCodeGiveawayToTL(
const InvoicePremiumGiftCode &invoice) {
const auto &giveaway = v::get<InvoicePremiumGiftCodeGiveaway>(
invoice.purpose);
return Tdb::tl_premiumGiveawayParameters(
peerToTdbChat(giveaway.boostPeer->id),
Tdb::tl_vector(ranges::views::all(
giveaway.additionalChannels
) | ranges::views::transform([](not_null<ChannelData*> c) {
return peerToTdbChat(c->id);
}) | ranges::to<QVector<Tdb::TLint53>>),
Tdb::tl_int32(giveaway.untilDate),
Tdb::tl_bool(giveaway.onlyNewSubscribers),
Tdb::tl_vector(ranges::views::all(
giveaway.countries
) | ranges::views::transform([](QString value) {
return Tdb::tl_string(std::move(value));
}) | ranges::to<QVector<Tdb::TLstring>>));
}

Form::Form(InvoiceId id, bool receipt)
: _id(id)
, _session(SessionFromId(id))
Expand Down Expand Up @@ -400,13 +420,40 @@ MTPInputInvoice Form::inputInvoice() const {
#endif

TLinputInvoice Form::inputInvoice() const {
if (const auto slug = std::get_if<InvoiceSlug>(&_id.value)) {
if (const auto message = std::get_if<InvoiceMessage>(&_id.value)) {
return tl_inputInvoiceMessage(
peerToTdbChat(message->peer->id),
tl_int53(message->itemId.bare));
} else if (const auto slug = std::get_if<InvoiceSlug>(&_id.value)) {
return tl_inputInvoiceName(tl_string(slug->slug));
}
const auto message = v::get<InvoiceMessage>(_id.value);
return tl_inputInvoiceMessage(
peerToTdbChat(message.peer->id),
tl_int53(message.itemId.bare));

const auto &giftCode = v::get<InvoicePremiumGiftCode>(_id.value);
const auto users = std::get_if<InvoicePremiumGiftCodeUsers>(
&giftCode.purpose);
if (users) {
return Tdb::tl_inputInvoiceTelegram(
Tdb::tl_telegramPaymentPurposePremiumGiftCodes(
users->boostPeer
? peerToTdbChat(users->boostPeer->id)
: TLint53(),
Tdb::tl_string(giftCode.currency),
Tdb::tl_int53(giftCode.amount),
Tdb::tl_vector(ranges::views::all(
users->users
) | ranges::views::transform([](not_null<UserData*> user) {
return peerToTdbChat(user->id);
}) | ranges::to<QVector<TLint53>>),
Tdb::tl_int32(giftCode.months)));
} else {
return Tdb::tl_inputInvoiceTelegram(
Tdb::tl_telegramPaymentPurposePremiumGiveaway(
InvoiceGiftCodeGiveawayToTL(giftCode),
Tdb::tl_string(giftCode.currency),
Tdb::tl_int53(giftCode.amount),
Tdb::tl_int32(giftCode.users),
Tdb::tl_int32(giftCode.months)));
}
}

void Form::requestForm() {
Expand Down
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/payments/payments_form.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TLsavedCredentials;
class TLshippingOption;
class TLDpaymentProviderStripe;
class TLDpaymentProviderSmartGlocal;
class TLpremiumGiveawayParameters;
} // namespace Tdb

class Image;
Expand Down Expand Up @@ -246,6 +247,8 @@ struct InvoiceId {
[[nodiscard]] MTPinputStorePaymentPurpose InvoicePremiumGiftCodeGiveawayToTL(
const InvoicePremiumGiftCode &invoice);
#endif
[[nodiscard]] Tdb::TLpremiumGiveawayParameters InvoiceGiftCodeGiveawayToTL(
const InvoicePremiumGiftCode &invoice);

class Form final : public base::has_weak_ptr {
public:
Expand Down

0 comments on commit 7863556

Please sign in to comment.