Add td_api::internalLinkTypePremiumGiftCode.

This commit is contained in:
levlam 2023-09-29 16:44:59 +03:00
parent acb9da43a7
commit b4609e910a
4 changed files with 61 additions and 2 deletions

View File

@ -5211,6 +5211,10 @@ internalLinkTypePhoneNumberConfirmation hash:string phone_number:string = Intern
//@description The link is a link to the Premium features screen of the application from which the user can subscribe to Telegram Premium. Call getPremiumFeatures with the given referrer to process the link @referrer Referrer specified in the link
internalLinkTypePremiumFeatures referrer:string = InternalLinkType;
//@description The link is a link with a Telegram Premium gift code. Call checkPremiumGiftCode with the given code to process the link. If the code is valid and the user wants to apply it, then call applyPremiumGiftCode
//@code The Telegram Premium gift code
internalLinkTypePremiumGiftCode code:string = InternalLinkType;
//@description The link is a link to the privacy and security section of the app settings
internalLinkTypePrivacyAndSecuritySettings = InternalLinkType;

View File

@ -561,6 +561,18 @@ class LinkManager::InternalLinkPremiumFeatures final : public InternalLink {
}
};
class LinkManager::InternalLinkPremiumGiftCode final : public InternalLink {
string code_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePremiumGiftCode>(code_);
}
public:
explicit InternalLinkPremiumGiftCode(string code) : code_(std::move(code)) {
}
};
class LinkManager::InternalLinkPrivacyAndSecuritySettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePrivacyAndSecuritySettings>();
@ -1085,8 +1097,9 @@ LinkManager::LinkInfo LinkManager::get_link_info(Slice link) {
if (ends_with(host, ".t.me") && host.size() >= 9 && host.find('.') == host.size() - 5) {
Slice subdomain(&host[0], host.size() - 5);
static const FlatHashSet<Slice, SliceHash> disallowed_subdomains(
{"addemoji", "addlist", "addstickers", "addtheme", "auth", "boost", "confirmphone", "contact", "invoice",
"joinchat", "login", "proxy", "setlanguage", "share", "socks", "web", "a", "k", "z"});
{"addemoji", "addlist", "addstickers", "addtheme", "auth", "boost", "confirmphone",
"contact", "giftcode", "invoice", "joinchat", "login", "proxy", "setlanguage",
"share", "socks", "web", "a", "k", "z"});
if (is_valid_username(subdomain) && disallowed_subdomains.count(subdomain) == 0) {
result.type_ = LinkType::TMe;
result.query_ = PSTRING() << '/' << subdomain << http_url.query_;
@ -1460,6 +1473,11 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
if (has_arg("slug")) {
return td::make_unique<InternalLinkInvoice>(url_query.get_arg("slug").str());
}
} else if (path.size() == 1 && path[0] == "giftcode") {
// giftcode?slug=<code>
if (has_arg("slug")) {
return td::make_unique<InternalLinkPremiumGiftCode>(url_query.get_arg("slug").str());
}
} else if (path.size() == 1 && (path[0] == "share" || path[0] == "msg" || path[0] == "msg_url")) {
// msg_url?url=<url>
// msg_url?url=<url>&text=<text>
@ -1610,6 +1628,11 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
// /invoice/<name>
return td::make_unique<InternalLinkInvoice>(path[1]);
}
} else if (path[0] == "giftcode") {
if (path.size() >= 2 && !path[1].empty()) {
// /giftcode/<code>
return td::make_unique<InternalLinkPremiumGiftCode>(path[1]);
}
} else if (path[0][0] == '$') {
if (path[0].size() >= 2) {
// /$<invoice_name>
@ -2179,6 +2202,14 @@ Result<string> LinkManager::get_internal_link_impl(const td_api::InternalLinkTyp
}
return PSTRING() << "tg://premium_offer?ref=" << url_encode(link->referrer_);
}
case td_api::internalLinkTypePremiumGiftCode::ID: {
auto link = static_cast<const td_api::internalLinkTypePremiumGiftCode *>(type_ptr);
if (is_internal) {
return PSTRING() << "tg://giftcode?slug=" << url_encode(link->code_);
} else {
return PSTRING() << get_t_me_url() << "giftcode/" << url_encode(link->code_);
}
}
case td_api::internalLinkTypePrivacyAndSecuritySettings::ID:
if (!is_internal) {
return Status::Error("HTTP link is unavailable for the link type");

View File

@ -141,6 +141,7 @@ class LinkManager final : public Actor {
class InternalLinkMessageDraft;
class InternalLinkPassportDataRequest;
class InternalLinkPremiumFeatures;
class InternalLinkPremiumGiftCode;
class InternalLinkPrivacyAndSecuritySettings;
class InternalLinkProxy;
class InternalLinkPublicDialog;

View File

@ -291,6 +291,10 @@ static auto premium_features(const td::string &referrer) {
return td::td_api::make_object<td::td_api::internalLinkTypePremiumFeatures>(referrer);
}
static auto premium_gift_code(const td::string &code) {
return td::td_api::make_object<td::td_api::internalLinkTypePremiumGiftCode>(code);
}
static auto privacy_and_security_settings() {
return td::td_api::make_object<td::td_api::internalLinkTypePrivacyAndSecuritySettings>();
}
@ -640,6 +644,24 @@ TEST(Link, parse_internal_link_part2) {
parse_internal_link("tg:invoice?slug=abc%30ef", invoice("abc0ef"));
parse_internal_link("tg://invoice?slug=", unknown_deep_link("tg://invoice?slug="));
parse_internal_link("t.me/giftcode?slug=abcdef", nullptr);
parse_internal_link("t.me/giftcode", nullptr);
parse_internal_link("t.me/giftcode/", nullptr);
parse_internal_link("t.me/giftcode//abcdef", nullptr);
parse_internal_link("t.me/giftcode?/abcdef", nullptr);
parse_internal_link("t.me/giftcode/?abcdef", nullptr);
parse_internal_link("t.me/giftcode/#abcdef", nullptr);
parse_internal_link("t.me/giftcode/abacaba", premium_gift_code("abacaba"));
parse_internal_link("t.me/giftcode/aba%20aba", premium_gift_code("aba aba"));
parse_internal_link("t.me/giftcode/123456a", premium_gift_code("123456a"));
parse_internal_link("t.me/giftcode/12345678901", premium_gift_code("12345678901"));
parse_internal_link("t.me/giftcode/123456", premium_gift_code("123456"));
parse_internal_link("t.me/giftcode/123456/123123/12/31/a/s//21w/?asdas#test", premium_gift_code("123456"));
parse_internal_link("tg:giftcode?slug=abcdef", premium_gift_code("abcdef"));
parse_internal_link("tg:giftcode?slug=abc%30ef", premium_gift_code("abc0ef"));
parse_internal_link("tg://giftcode?slug=", unknown_deep_link("tg://giftcode?slug="));
parse_internal_link("tg:share?url=google.com&text=text#asdasd", message_draft("google.com\ntext", true));
parse_internal_link("tg:share?url=google.com&text=", message_draft("google.com", false));
parse_internal_link("tg:share?url=&text=google.com", message_draft("google.com", false));
@ -1261,6 +1283,7 @@ TEST(Link, parse_internal_link_part4) {
parse_internal_link("boost.t.me", nullptr);
parse_internal_link("confirmphone.t.me", nullptr);
parse_internal_link("contact.t.me", nullptr);
parse_internal_link("giftcode.t.me", nullptr);
parse_internal_link("invoice.t.me", nullptr);
parse_internal_link("joinchat.t.me", nullptr);
parse_internal_link("login.t.me", nullptr);