Add td_api::internalLinkTypeUserPhoneNumber.

This commit is contained in:
levlam 2022-02-18 18:56:58 +03:00
parent a92d1b2ae4
commit 867c95bdb2
4 changed files with 60 additions and 6 deletions

View File

@ -3347,6 +3347,9 @@ internalLinkTypeUnknownDeepLink link:string = InternalLinkType;
//@description The link is a link to an unsupported proxy. An alert can be shown to the user //@description The link is a link to an unsupported proxy. An alert can be shown to the user
internalLinkTypeUnsupportedProxy = InternalLinkType; internalLinkTypeUnsupportedProxy = InternalLinkType;
//@description The link is a link to a user by its phone number. Call searchUserByPhoneNumber with the given phone number to process the link @phone_number Phone number of the user
internalLinkTypeUserPhoneNumber phone_number:string = InternalLinkType;
//@description The link is a link to a video chat. Call searchPublicChat with the given chat username, and then joinGroupCall with the given invite hash to process the link //@description The link is a link to a video chat. Call searchPublicChat with the given chat username, and then joinGroupCall with the given invite hash to process the link
//@chat_username Username of the chat with the video chat @invite_hash If non-empty, invite hash to be used to join the video chat without being muted by administrators //@chat_username Username of the chat with the video chat @invite_hash If non-empty, invite hash to be used to join the video chat without being muted by administrators
//@is_live_stream True, if the video chat is expected to be a live stream in a channel or a broadcast group //@is_live_stream True, if the video chat is expected to be a live stream in a channel or a broadcast group

View File

@ -67,6 +67,18 @@ static bool is_valid_username(Slice username) {
return true; return true;
} }
static bool is_valid_phone_number(Slice phone_number) {
if (phone_number.empty() || phone_number.size() > 32) {
return false;
}
for (auto c : phone_number) {
if (!is_digit(c)) {
return false;
}
}
return true;
}
static string get_url_query_hash(bool is_tg, const HttpUrlQuery &url_query) { static string get_url_query_hash(bool is_tg, const HttpUrlQuery &url_query) {
const auto &path = url_query.path_; const auto &path = url_query.path_;
if (is_tg) { if (is_tg) {
@ -80,6 +92,9 @@ static string get_url_query_hash(bool is_tg, const HttpUrlQuery &url_query) {
return path[1]; return path[1];
} }
if (!path.empty() && path[0].size() >= 2 && (path[0][0] == ' ' || path[0][0] == '+')) { if (!path.empty() && path[0].size() >= 2 && (path[0][0] == ' ' || path[0][0] == '+')) {
if (is_valid_phone_number(Slice(path[0]).substr(1))) {
return string();
}
// /+<link> // /+<link>
return path[0].substr(1); return path[0].substr(1);
} }
@ -363,6 +378,18 @@ class LinkManager::InternalLinkUnsupportedProxy final : public InternalLink {
} }
}; };
class LinkManager::InternalLinkUserPhoneNumber final : public InternalLink {
string phone_number_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeUserPhoneNumber>(phone_number_);
}
public:
explicit InternalLinkUserPhoneNumber(string phone_number) : phone_number_(std::move(phone_number)) {
}
};
class LinkManager::InternalLinkVoiceChat final : public InternalLink { class LinkManager::InternalLinkVoiceChat final : public InternalLink {
string dialog_username_; string dialog_username_;
string invite_hash_; string invite_hash_;
@ -803,6 +830,9 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
} }
// resolve?domain=<username> // resolve?domain=<username>
return td::make_unique<InternalLinkPublicDialog>(std::move(username)); return td::make_unique<InternalLinkPublicDialog>(std::move(username));
} else if (is_valid_phone_number(get_arg("phone"))) {
// resolve?phone=12345
return td::make_unique<InternalLinkUserPhoneNumber>(get_arg("phone"));
} }
} else if (path.size() == 1 && path[0] == "login") { } else if (path.size() == 1 && path[0] == "login") {
// login?code=123456 // login?code=123456
@ -959,9 +989,14 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
} }
} else if (path[0][0] == ' ' || path[0][0] == '+') { } else if (path[0][0] == ' ' || path[0][0] == '+') {
if (path[0].size() >= 2) { if (path[0].size() >= 2) {
// /+<link> if (is_valid_phone_number(Slice(path[0]).substr(1))) {
return td::make_unique<InternalLinkDialogInvite>(PSTRING() << "tg:join?invite=" // /+<phone_number>
<< url_encode(get_url_query_hash(false, url_query))); return td::make_unique<InternalLinkUserPhoneNumber>(path[0].substr(1));
} else {
// /+<link>
return td::make_unique<InternalLinkDialogInvite>(PSTRING() << "tg:join?invite="
<< url_encode(get_url_query_hash(false, url_query)));
}
} }
} else if (path[0] == "addstickers") { } else if (path[0] == "addstickers") {
if (path.size() >= 2 && !path[1].empty()) { if (path.size() >= 2 && !path[1].empty()) {

View File

@ -104,6 +104,7 @@ class LinkManager final : public Actor {
class InternalLinkThemeSettings; class InternalLinkThemeSettings;
class InternalLinkUnknownDeepLink; class InternalLinkUnknownDeepLink;
class InternalLinkUnsupportedProxy; class InternalLinkUnsupportedProxy;
class InternalLinkUserPhoneNumber;
class InternalLinkVoiceChat; class InternalLinkVoiceChat;
struct LinkInfo { struct LinkInfo {

View File

@ -174,6 +174,9 @@ TEST(Link, parse_internal_link) {
auto unsupported_proxy = [] { auto unsupported_proxy = [] {
return td::td_api::make_object<td::td_api::internalLinkTypeUnsupportedProxy>(); return td::td_api::make_object<td::td_api::internalLinkTypeUnsupportedProxy>();
}; };
auto user_phone_number = [](const td::string &phone_number) {
return td::td_api::make_object<td::td_api::internalLinkTypeUserPhoneNumber>(phone_number);
};
auto video_chat = [](const td::string &chat_username, const td::string &invite_hash, bool is_live_stream) { auto video_chat = [](const td::string &chat_username, const td::string &invite_hash, bool is_live_stream) {
return td::td_api::make_object<td::td_api::internalLinkTypeVideoChat>(chat_username, invite_hash, is_live_stream); return td::td_api::make_object<td::td_api::internalLinkTypeVideoChat>(chat_username, invite_hash, is_live_stream);
}; };
@ -211,6 +214,18 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:resolve?domain=&post=12345&single", parse_internal_link("tg:resolve?domain=&post=12345&single",
unknown_deep_link("tg://resolve?domain=&post=12345&single")); unknown_deep_link("tg://resolve?domain=&post=12345&single"));
parse_internal_link("tg:resolve?domain=telegram&post=&single", public_chat("telegram")); parse_internal_link("tg:resolve?domain=telegram&post=&single", public_chat("telegram"));
parse_internal_link("tg:resolve?domain=123456&post=&single",
unknown_deep_link("tg://resolve?domain=123456&post=&single"));
parse_internal_link("tg:resolve?phone=1", user_phone_number("1"));
parse_internal_link("tg:resolve?phone=123456", user_phone_number("123456"));
parse_internal_link("tg:resolve?phone=01234567890123456789012345678912",
user_phone_number("01234567890123456789012345678912"));
parse_internal_link("tg:resolve?phone=012345678901234567890123456789123",
unknown_deep_link("tg://resolve?phone=012345678901234567890123456789123"));
parse_internal_link("tg:resolve?phone=", unknown_deep_link("tg://resolve?phone="));
parse_internal_link("tg:resolve?phone=+123", unknown_deep_link("tg://resolve?phone=+123"));
parse_internal_link("tg:resolve?phone=123456 ", unknown_deep_link("tg://resolve?phone=123456 "));
parse_internal_link("t.me/username/12345?single", message("tg:resolve?domain=username&post=12345&single")); parse_internal_link("t.me/username/12345?single", message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("t.me/username/12345?asdasd", message("tg:resolve?domain=username&post=12345")); parse_internal_link("t.me/username/12345?asdasd", message("tg:resolve?domain=username&post=12345"));
@ -386,9 +401,9 @@ TEST(Link, parse_internal_link) {
parse_internal_link("t.me/+aba%20aba", chat_invite("aba%20aba")); parse_internal_link("t.me/+aba%20aba", chat_invite("aba%20aba"));
parse_internal_link("t.me/+aba%30aba", chat_invite("aba0aba")); parse_internal_link("t.me/+aba%30aba", chat_invite("aba0aba"));
parse_internal_link("t.me/+123456a", chat_invite("123456a")); parse_internal_link("t.me/+123456a", chat_invite("123456a"));
parse_internal_link("t.me/%2012345678901", chat_invite("12345678901")); parse_internal_link("t.me/%2012345678901", user_phone_number("12345678901"));
parse_internal_link("t.me/+123456", chat_invite("123456")); parse_internal_link("t.me/+123456", user_phone_number("123456"));
parse_internal_link("t.me/ 123456/123123/12/31/a/s//21w/?asdas#test", chat_invite("123456")); parse_internal_link("t.me/ 123456/123123/12/31/a/s//21w/?asdas#test", user_phone_number("123456"));
parse_internal_link("t.me/ /123456/123123/12/31/a/s//21w/?asdas#test", nullptr); parse_internal_link("t.me/ /123456/123123/12/31/a/s//21w/?asdas#test", nullptr);
parse_internal_link("tg:join?invite=abcdef", chat_invite("abcdef")); parse_internal_link("tg:join?invite=abcdef", chat_invite("abcdef"));