mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Unified parsing of MTP gift and subscription options in separate file.
This commit is contained in:
parent
fc759ac688
commit
6f3d19914d
8 changed files with 149 additions and 73 deletions
|
@ -140,6 +140,8 @@ PRIVATE
|
||||||
api/api_polls.h
|
api/api_polls.h
|
||||||
api/api_premium.cpp
|
api/api_premium.cpp
|
||||||
api/api_premium.h
|
api/api_premium.h
|
||||||
|
api/api_premium_option.cpp
|
||||||
|
api/api_premium_option.h
|
||||||
api/api_report.cpp
|
api/api_report.cpp
|
||||||
api/api_report.h
|
api/api_report.h
|
||||||
api/api_ringtones.cpp
|
api/api_ringtones.cpp
|
||||||
|
|
|
@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#include "api/api_premium.h"
|
#include "api/api_premium.h"
|
||||||
|
|
||||||
|
#include "api/api_premium_option.h"
|
||||||
#include "api/api_text_entities.h"
|
#include "api/api_text_entities.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
#include "data/data_peer_values.h"
|
#include "data/data_peer_values.h"
|
||||||
|
@ -86,43 +87,43 @@ void Premium::reloadPromo() {
|
||||||
_promoRequestId = _api.request(MTPhelp_GetPremiumPromo(
|
_promoRequestId = _api.request(MTPhelp_GetPremiumPromo(
|
||||||
)).done([=](const MTPhelp_PremiumPromo &result) {
|
)).done([=](const MTPhelp_PremiumPromo &result) {
|
||||||
_promoRequestId = 0;
|
_promoRequestId = 0;
|
||||||
result.match([&](const MTPDhelp_premiumPromo &data) {
|
const auto &data = result.data();
|
||||||
_session->data().processUsers(data.vusers());
|
_session->data().processUsers(data.vusers());
|
||||||
for (const auto &option : data.vperiod_options().v) {
|
|
||||||
option.match([&](const MTPDpremiumSubscriptionOption &data) {
|
_subscriptionOptions = SubscriptionOptionsFromTL(
|
||||||
if (data.vmonths().v == 1) {
|
data.vperiod_options().v);
|
||||||
_monthlyAmount = data.vamount().v;
|
for (const auto &option : data.vperiod_options().v) {
|
||||||
_monthlyCurrency = qs(data.vcurrency());
|
if (option.data().vmonths().v == 1) {
|
||||||
}
|
_monthlyAmount = option.data().vamount().v;
|
||||||
});
|
_monthlyCurrency = qs(option.data().vcurrency());
|
||||||
}
|
}
|
||||||
auto text = TextWithEntities{
|
}
|
||||||
qs(data.vstatus_text()),
|
auto text = TextWithEntities{
|
||||||
EntitiesFromMTP(_session, data.vstatus_entities().v),
|
qs(data.vstatus_text()),
|
||||||
};
|
EntitiesFromMTP(_session, data.vstatus_entities().v),
|
||||||
_statusText = text;
|
};
|
||||||
_statusTextUpdates.fire(std::move(text));
|
_statusText = text;
|
||||||
auto videos = base::flat_map<QString, not_null<DocumentData*>>();
|
_statusTextUpdates.fire(std::move(text));
|
||||||
const auto count = int(std::min(
|
auto videos = base::flat_map<QString, not_null<DocumentData*>>();
|
||||||
data.vvideo_sections().v.size(),
|
const auto count = int(std::min(
|
||||||
data.vvideos().v.size()));
|
data.vvideo_sections().v.size(),
|
||||||
videos.reserve(count);
|
data.vvideos().v.size()));
|
||||||
for (auto i = 0; i != count; ++i) {
|
videos.reserve(count);
|
||||||
const auto document = _session->data().processDocument(
|
for (auto i = 0; i != count; ++i) {
|
||||||
data.vvideos().v[i]);
|
const auto document = _session->data().processDocument(
|
||||||
if ((!document->isVideoFile() && !document->isGifv())
|
data.vvideos().v[i]);
|
||||||
|| !document->supportsStreaming()) {
|
if ((!document->isVideoFile() && !document->isGifv())
|
||||||
document->forceIsStreamedAnimation();
|
|| !document->supportsStreaming()) {
|
||||||
}
|
document->forceIsStreamedAnimation();
|
||||||
videos.emplace(
|
|
||||||
qs(data.vvideo_sections().v[i]),
|
|
||||||
document);
|
|
||||||
}
|
}
|
||||||
if (_videos != videos) {
|
videos.emplace(
|
||||||
_videos = std::move(videos);
|
qs(data.vvideo_sections().v[i]),
|
||||||
_videosUpdated.fire({});
|
document);
|
||||||
}
|
}
|
||||||
});
|
if (_videos != videos) {
|
||||||
|
_videos = std::move(videos);
|
||||||
|
_videosUpdated.fire({});
|
||||||
|
}
|
||||||
}).fail([=] {
|
}).fail([=] {
|
||||||
_promoRequestId = 0;
|
_promoRequestId = 0;
|
||||||
}).send();
|
}).send();
|
||||||
|
@ -182,4 +183,8 @@ void Premium::reloadCloudSet() {
|
||||||
}).send();
|
}).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Data::SubscriptionOptions &Premium::subscriptionOptions() const {
|
||||||
|
return _subscriptionOptions;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Api
|
} // namespace Api
|
||||||
|
|
|
@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "data/data_subscription_option.h"
|
||||||
#include "mtproto/sender.h"
|
#include "mtproto/sender.h"
|
||||||
|
|
||||||
class ApiWrap;
|
class ApiWrap;
|
||||||
|
@ -39,6 +40,9 @@ public:
|
||||||
[[nodiscard]] int64 monthlyAmount() const;
|
[[nodiscard]] int64 monthlyAmount() const;
|
||||||
[[nodiscard]] QString monthlyCurrency() const;
|
[[nodiscard]] QString monthlyCurrency() const;
|
||||||
|
|
||||||
|
[[nodiscard]] auto subscriptionOptions() const
|
||||||
|
-> const Data::SubscriptionOptions &;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void reloadPromo();
|
void reloadPromo();
|
||||||
void reloadStickers();
|
void reloadStickers();
|
||||||
|
@ -67,6 +71,8 @@ private:
|
||||||
int64 _monthlyAmount = 0;
|
int64 _monthlyAmount = 0;
|
||||||
QString _monthlyCurrency;
|
QString _monthlyCurrency;
|
||||||
|
|
||||||
|
Data::SubscriptionOptions _subscriptionOptions;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Api
|
} // namespace Api
|
||||||
|
|
40
Telegram/SourceFiles/api/api_premium_option.cpp
Normal file
40
Telegram/SourceFiles/api/api_premium_option.cpp
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
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_option.h"
|
||||||
|
|
||||||
|
#include "ui/text/format_values.h"
|
||||||
|
|
||||||
|
namespace Api {
|
||||||
|
|
||||||
|
constexpr auto kDiscountDivider = 5.;
|
||||||
|
|
||||||
|
Data::SubscriptionOption CreateSubscriptionOption(
|
||||||
|
int months,
|
||||||
|
int monthlyAmount,
|
||||||
|
int64 amount,
|
||||||
|
const QString ¤cy,
|
||||||
|
const QString &botUrl) {
|
||||||
|
const auto discount = [&] {
|
||||||
|
const auto percent = monthlyAmount * months / float64(amount) - 1.;
|
||||||
|
return std::round(percent * 100. / kDiscountDivider)
|
||||||
|
* kDiscountDivider;
|
||||||
|
}();
|
||||||
|
return {
|
||||||
|
.duration = Ui::FormatTTL(months * 86400 * 31),
|
||||||
|
.discount = discount
|
||||||
|
? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount)
|
||||||
|
: QString(),
|
||||||
|
.costPerMonth = Ui::FillAmountAndCurrency(
|
||||||
|
amount / float64(months),
|
||||||
|
currency),
|
||||||
|
.costTotal = Ui::FillAmountAndCurrency(amount, currency),
|
||||||
|
.botUrl = botUrl,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Api
|
50
Telegram/SourceFiles/api/api_premium_option.h
Normal file
50
Telegram/SourceFiles/api/api_premium_option.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
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 "data/data_subscription_option.h"
|
||||||
|
|
||||||
|
namespace Api {
|
||||||
|
|
||||||
|
[[nodiscard]] Data::SubscriptionOption CreateSubscriptionOption(
|
||||||
|
int months,
|
||||||
|
int monthlyAmount,
|
||||||
|
int64 amount,
|
||||||
|
const QString ¤cy,
|
||||||
|
const QString &botUrl);
|
||||||
|
|
||||||
|
template<typename Option>
|
||||||
|
[[nodiscard]] Data::SubscriptionOptions SubscriptionOptionsFromTL(
|
||||||
|
const QVector<Option> &tlOptions) {
|
||||||
|
auto result = Data::SubscriptionOptions();
|
||||||
|
const auto monthlyAmount = [&] {
|
||||||
|
const auto &min = ranges::min_element(
|
||||||
|
tlOptions,
|
||||||
|
ranges::less(),
|
||||||
|
[](const Option &o) { return o.data().vamount().v; }
|
||||||
|
)->data();
|
||||||
|
return min.vamount().v / float64(min.vmonths().v);
|
||||||
|
}();
|
||||||
|
result.reserve(tlOptions.size());
|
||||||
|
for (const auto &tlOption : tlOptions) {
|
||||||
|
const auto &option = tlOption.data();
|
||||||
|
const auto botUrl = qs(option.vbot_url());
|
||||||
|
const auto months = option.vmonths().v;
|
||||||
|
const auto amount = option.vamount().v;
|
||||||
|
const auto currency = qs(option.vcurrency());
|
||||||
|
result.push_back(CreateSubscriptionOption(
|
||||||
|
months,
|
||||||
|
monthlyAmount,
|
||||||
|
amount,
|
||||||
|
currency,
|
||||||
|
botUrl));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Api
|
|
@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "boxes/gift_premium_box.h"
|
#include "boxes/gift_premium_box.h"
|
||||||
|
|
||||||
#include "apiwrap.h"
|
#include "apiwrap.h"
|
||||||
|
#include "api/api_premium_option.h"
|
||||||
#include "base/weak_ptr.h"
|
#include "base/weak_ptr.h"
|
||||||
#include "core/click_handler_types.h" // ClickHandlerContext.
|
#include "core/click_handler_types.h" // ClickHandlerContext.
|
||||||
#include "core/local_url_handlers.h" // TryConvertUrlToLocal.
|
#include "core/local_url_handlers.h" // TryConvertUrlToLocal.
|
||||||
|
@ -49,41 +50,12 @@ GiftOptions GiftOptionFromTL(const MTPDuserFull &data) {
|
||||||
if (!gifts) {
|
if (!gifts) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
const auto monthlyAmount = [&] {
|
result = Api::SubscriptionOptionsFromTL(gifts->v);
|
||||||
const auto &min = ranges::min_element(
|
for (auto &option : result) {
|
||||||
gifts->v,
|
option.costPerMonth = tr::lng_premium_gift_per(
|
||||||
ranges::less(),
|
tr::now,
|
||||||
[](const MTPPremiumGiftOption &o) { return o.data().vamount().v; }
|
lt_cost,
|
||||||
)->data();
|
option.costPerMonth);
|
||||||
return min.vamount().v / float64(min.vmonths().v);
|
|
||||||
}();
|
|
||||||
result.reserve(gifts->v.size());
|
|
||||||
for (const auto &gift : gifts->v) {
|
|
||||||
const auto &option = gift.data();
|
|
||||||
const auto botUrl = qs(option.vbot_url());
|
|
||||||
const auto months = option.vmonths().v;
|
|
||||||
const auto amount = option.vamount().v;
|
|
||||||
const auto currency = qs(option.vcurrency());
|
|
||||||
const auto discount = [&] {
|
|
||||||
const auto percent = monthlyAmount * months / float64(amount)
|
|
||||||
- 1.;
|
|
||||||
return std::round(percent * 100. / kDiscountDivider)
|
|
||||||
* kDiscountDivider;
|
|
||||||
}();
|
|
||||||
result.push_back({
|
|
||||||
.duration = Ui::FormatTTL(months * 86400 * 31),
|
|
||||||
.discount = discount
|
|
||||||
? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount)
|
|
||||||
: QString(),
|
|
||||||
.perMonth = tr::lng_premium_gift_per(
|
|
||||||
tr::now,
|
|
||||||
lt_cost,
|
|
||||||
Ui::FillAmountAndCurrency(
|
|
||||||
amount / float64(months),
|
|
||||||
currency)),
|
|
||||||
.total = Ui::FillAmountAndCurrency(amount, currency),
|
|
||||||
.botUrl = botUrl,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,8 @@ namespace Data {
|
||||||
struct SubscriptionOption {
|
struct SubscriptionOption {
|
||||||
QString duration;
|
QString duration;
|
||||||
QString discount;
|
QString discount;
|
||||||
QString perMonth;
|
QString costPerMonth;
|
||||||
|
QString costTotal;
|
||||||
QString total;
|
QString total;
|
||||||
QString botUrl;
|
QString botUrl;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1056,12 +1056,12 @@ void AddGiftOptions(
|
||||||
0);
|
0);
|
||||||
p.setPen(st::windowSubTextFg);
|
p.setPen(st::windowSubTextFg);
|
||||||
p.setFont(st::shareBoxListItem.nameStyle.font);
|
p.setFont(st::shareBoxListItem.nameStyle.font);
|
||||||
p.drawText(perRect, info.perMonth, style::al_left);
|
p.drawText(perRect, info.costPerMonth, style::al_left);
|
||||||
|
|
||||||
const auto totalRect = row->rect()
|
const auto totalRect = row->rect()
|
||||||
- QMargins(0, 0, st::premiumGiftRowMargins.right(), 0);
|
- QMargins(0, 0, st::premiumGiftRowMargins.right(), 0);
|
||||||
p.setFont(st::normalFont);
|
p.setFont(st::normalFont);
|
||||||
p.drawText(totalRect, info.total, style::al_right);
|
p.drawText(totalRect, info.costTotal, style::al_right);
|
||||||
}, row->lifetime());
|
}, row->lifetime());
|
||||||
|
|
||||||
row->setClickedCallback([=, duration = st::defaultCheck.duration] {
|
row->setClickedCallback([=, duration = st::defaultCheck.duration] {
|
||||||
|
|
Loading…
Add table
Reference in a new issue