Handle already-premium state in gift link usage.

This commit is contained in:
John Preston 2024-04-01 12:47:38 +04:00
parent d35c8232d2
commit d21418cf04
2 changed files with 58 additions and 9 deletions

View File

@ -1103,7 +1103,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_edit_privacy_everyone" = "Everybody";
"lng_edit_privacy_contacts" = "My contacts";
"lng_edit_privacy_close_friends" = "Close friends";
"lng_edit_privacy_contacts_and_premium" = "Contacts and Premium";
"lng_edit_privacy_contacts_and_premium" = "Contacts & Premium";
"lng_edit_privacy_nobody" = "Nobody";
"lng_edit_privacy_premium" = "Premium users";
"lng_edit_privacy_exceptions" = "Add exceptions";
@ -2687,6 +2687,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_gift_link_also_send" = "You can also {link} to a friend as a gift.";
"lng_gift_link_also_send_link" = "send this link";
"lng_gift_link_use" = "Use Link";
"lng_gift_link_already_title" = "You already have Telegram Premium";
"lng_gift_link_already_about" = "You can activate this link after {date} or {link} to a friend.";
"lng_gift_link_already_link" = "send the link";
"lng_gift_link_used_title" = "Used Gift Link";
"lng_gift_link_used_about" = "This link was used to activate\na **Telegram Premium** subscription.";
"lng_gift_link_used_footer" = "This link was used on {date}.";

View File

@ -45,6 +45,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/rect.h"
#include "ui/vertical_list.h"
#include "ui/text/text_utilities.h"
#include "ui/toast/toast.h"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/gradient_round_button.h"
#include "ui/wrap/padding_wrap.h"
@ -892,6 +893,52 @@ void AddTable(
}
}
void ShareWithFriend(
not_null<Window::SessionNavigation*> navigation,
const QString &slug) {
const auto chosen = [=](not_null<Data::Thread*> thread) {
const auto content = navigation->parentController()->content();
return content->shareUrl(
thread,
MakeGiftCodeLink(&navigation->session(), slug).link,
QString());
};
Window::ShowChooseRecipientBox(navigation, chosen);
}
[[nodiscard]] void ShowAlreadyPremiumToast(
not_null<Window::SessionNavigation*> navigation,
const QString &slug,
TimeId date) {
const auto instance = std::make_shared<
base::weak_ptr<Ui::Toast::Instance>
>();
const auto shareLink = [=](
const ClickHandlerPtr &,
Qt::MouseButton button) {
if (button == Qt::LeftButton) {
if (const auto strong = instance->get()) {
strong->hideAnimated();
}
ShareWithFriend(navigation, slug);
}
return false;
};
*instance = navigation->showToast({
.title = tr::lng_gift_link_already_title(tr::now),
.text = tr::lng_gift_link_already_about(
tr::now,
lt_date,
Ui::Text::Bold(langDateTime(base::unixtime::parse(date))),
lt_link,
Ui::Text::Link(
Ui::Text::Bold(tr::lng_gift_link_already_link(tr::now))),
Ui::Text::WithEntities),
.duration = 6 * crl::time(1000),
.filter = crl::guard(navigation, shareLink),
});
}
} // namespace
GiftPremiumValidator::GiftPremiumValidator(
@ -1157,14 +1204,7 @@ void GiftCodeBox(
st::giveawayGiftCodeFooter),
st::giveawayGiftCodeFooterMargin);
footer->setClickHandlerFilter([=](const auto &...) {
const auto chosen = [=](not_null<Data::Thread*> thread) {
const auto content = controller->parentController()->content();
return content->shareUrl(
thread,
MakeGiftCodeLink(&controller->session(), slug).link,
QString());
};
Window::ShowChooseRecipientBox(controller, chosen);
ShareWithFriend(controller, slug);
return false;
});
@ -1189,14 +1229,20 @@ void GiftCodeBox(
} else if (!state->sent) {
state->sent = true;
const auto done = crl::guard(box, [=](const QString &error) {
const auto activePrefix = u"PREMIUM_SUB_ACTIVE_UNTIL_"_q;
if (error.isEmpty()) {
auto copy = state->data.current();
copy.used = base::unixtime::now();
state->data = std::move(copy);
Ui::StartFireworks(box->parentWidget());
} else if (error.startsWith(activePrefix)) {
const auto date = error.mid(activePrefix.size()).toInt();
ShowAlreadyPremiumToast(controller, slug, date);
state->sent = false;
} else {
box->uiShow()->showToast(error);
state->sent = false;
}
});
controller->session().api().premium().applyGiftCode(slug, done);