From 6393923d3a7a5433c7c58a8cab2a8db356ed85cc Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 20 Aug 2021 20:56:07 +0300 Subject: [PATCH] Make internalLinkTypeUnknownDeepLink self-contained. --- td/generate/scheme/td_api.tl | 8 ++-- td/telegram/LinkManager.cpp | 18 +++++--- td/telegram/LinkManager.h | 3 +- test/link.cpp | 89 +++++++++++++++++++++--------------- 4 files changed, 71 insertions(+), 47 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 47589c2d1..8760f42cc 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3057,8 +3057,8 @@ internalLinkTypeBotStartInGroup bot_username:string start_parameter:string = Int //@description The link is a link to the change phone number section of the app internalLinkTypeChangePhoneNumber = InternalLinkType; -//@description The link is a chat invite link. Call checkChatInviteLink with the given URL to process the link @url Internal representation of the link -internalLinkTypeChatInvite url:string = InternalLinkType; +//@description The link is a chat invite link. Call checkChatInviteLink with the given invite link to process the link @invite_link Internal representation of the invite link +internalLinkTypeChatInvite invite_link:string = InternalLinkType; //@description The link is a link to the filter settings section of the app internalLinkTypeFilterSettings = InternalLinkType; @@ -3109,8 +3109,8 @@ internalLinkTypeTheme theme_name:string = InternalLinkType; //@description The link is a link to the theme settings section of the app internalLinkTypeThemeSettings = InternalLinkType; -//@description The link is an unknown tg: link. Call getDeepLinkInfo to process the link -internalLinkTypeUnknownDeepLink = InternalLinkType; +//@description The link is an unknown tg: link. Call getDeepLinkInfo to process the link @link Link to be passed to getDeepLinkInfo +internalLinkTypeUnknownDeepLink link:string = InternalLinkType; //@description The link is a link to a voice chat. Call searchPublicChat with the given chat username, and then joinGoupCall with the given invite hash to process the link //@chat_username Username of the chat with the voice chat @invite_hash If non-empty, invite hash to be used to join the voice chat without being muted by administrators diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index 674f18cca..3dc8934e6 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -343,8 +343,14 @@ class LinkManager::InternalLinkThemeSettings final : public InternalLink { }; class LinkManager::InternalLinkUnknownDeepLink final : public InternalLink { + string link_; + td_api::object_ptr get_internal_link_type_object() const final { - return td_api::make_object(); + return td_api::make_object(link_); + } + + public: + explicit InternalLinkUnknownDeepLink(string link) : link_(std::move(link)) { } }; @@ -785,7 +791,7 @@ unique_ptr LinkManager::parse_tg_link_query(Slice que } if (username == "telegrampassport") { // resolve?domain=telegrampassport&bot_id=&scope=&public_key=&nonce= - return get_internal_link_passport(url_query.args_); + return get_internal_link_passport(query, url_query.args_); } // resolve?domain= return td::make_unique(std::move(username)); @@ -801,7 +807,7 @@ unique_ptr LinkManager::parse_tg_link_query(Slice que } } else if (path.size() == 1 && path[0] == "passport") { // passport?bot_id=&scope=&public_key=&nonce= - return get_internal_link_passport(url_query.args_); + return get_internal_link_passport(query, url_query.args_); } else if (path.size() >= 1 && path[0] == "settings") { if (path.size() == 2 && path[1] == "change_number") { // settings/change_number @@ -895,7 +901,7 @@ unique_ptr LinkManager::parse_tg_link_query(Slice que return get_internal_link_message_draft(get_arg("url"), get_arg("text")); } if (!path.empty() && !path[0].empty()) { - return td::make_unique(); + return td::make_unique(PSTRING() << "tg://" << query); } return nullptr; } @@ -1071,7 +1077,7 @@ unique_ptr LinkManager::get_internal_link_message_dra } unique_ptr LinkManager::get_internal_link_passport( - const vector> &args) { + Slice query, const vector> &args) { auto get_arg = [&args](Slice key) { for (auto &arg : args) { if (arg.first == key) { @@ -1091,7 +1097,7 @@ unique_ptr LinkManager::get_internal_link_passport( auto callback_url = get_arg("callback_url"); if (!bot_user_id.is_valid() || scope.empty() || public_key.empty() || nonce.empty()) { - return td::make_unique(); + return td::make_unique(PSTRING() << "tg://" << query); } return td::make_unique(bot_user_id, scope.str(), public_key.str(), nonce.str(), callback_url.str()); diff --git a/td/telegram/LinkManager.h b/td/telegram/LinkManager.h index 6a58bacf2..b6e830eb9 100644 --- a/td/telegram/LinkManager.h +++ b/td/telegram/LinkManager.h @@ -112,7 +112,8 @@ class LinkManager final : public Actor { static unique_ptr parse_t_me_link_query(Slice query); - static unique_ptr get_internal_link_passport(const vector> &args); + static unique_ptr get_internal_link_passport(Slice query, + const vector> &args); static unique_ptr get_internal_link_message_draft(Slice url, Slice text); diff --git a/test/link.cpp b/test/link.cpp index e3ecf8c55..9e8d524ba 100644 --- a/test/link.cpp +++ b/test/link.cpp @@ -167,8 +167,8 @@ TEST(Link, parse_internal_link) { auto theme_settings = [] { return td::td_api::make_object(); }; - auto unknown_deep_link = [] { - return td::td_api::make_object(); + auto unknown_deep_link = [](td::string link) { + return td::td_api::make_object(link); }; auto voice_chat = [](td::string chat_username, td::string invite_hash) { return td::td_api::make_object(chat_username, invite_hash); @@ -204,7 +204,8 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username&post=12345&single", nullptr); parse_internal_link("tg:http://resolve?domain=username&post=12345&single", nullptr); parse_internal_link("tg:https://resolve?domain=username&post=12345&single", nullptr); - parse_internal_link("tg:resolve?domain=&post=12345&single", unknown_deep_link()); + parse_internal_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("t.me/username/12345?single", message("tg:resolve?domain=username&post=12345&single")); @@ -224,8 +225,10 @@ TEST(Link, parse_internal_link) { parse_internal_link("t.me//12345?single", nullptr); parse_internal_link("https://telegram.dog/telegram/?single", public_chat("telegram")); - parse_internal_link("tg:privatepost?domain=username/12345&single", unknown_deep_link()); - parse_internal_link("tg:privatepost?channel=username/12345&single", unknown_deep_link()); + parse_internal_link("tg:privatepost?domain=username/12345&single", + unknown_deep_link("tg://privatepost?domain=username/12345&single")); + parse_internal_link("tg:privatepost?channel=username/12345&single", + unknown_deep_link("tg://privatepost?channel=username/12345&single")); parse_internal_link("tg:privatepost?channel=username&msg_id=12345", message("tg:privatepost?channel=username&msg_id=12345")); @@ -255,7 +258,8 @@ TEST(Link, parse_internal_link) { background("test?mode=12&intensity=2&bg_color=3&rotation=4")); parse_internal_link("tg:bg?mode=12&&slug=test&intensity=2&bg_color=3", background("test?mode=12&intensity=2&bg_color=3")); - parse_internal_link("tg:bg?mode=12&intensity=2&bg_color=3", unknown_deep_link()); + parse_internal_link("tg:bg?mode=12&intensity=2&bg_color=3", + unknown_deep_link("tg://bg?mode=12&intensity=2&bg_color=3")); parse_internal_link("tg:bg?color=111111#asdasd", background("111111")); parse_internal_link("tg:bg?color=11111%31", background("111111")); @@ -273,7 +277,8 @@ TEST(Link, parse_internal_link) { background("test?mode=12&intensity=2&bg_color=3&rotation=4")); parse_internal_link("tg:bg?mode=12&&slug=test&intensity=2&bg_color=3", background("test?mode=12&intensity=2&bg_color=3")); - parse_internal_link("tg:bg?mode=12&intensity=2&bg_color=3", unknown_deep_link()); + parse_internal_link("tg:bg?mode=12&intensity=2&bg_color=3", + unknown_deep_link("tg://bg?mode=12&intensity=2&bg_color=3")); parse_internal_link("%54.me/bg/111111#asdasd", background("111111")); parse_internal_link("t.me/bg/11111%31", background("111111")); @@ -329,8 +334,8 @@ TEST(Link, parse_internal_link) { parse_internal_link("https://t.me/msg?url=@&text=@", message_draft(" @\n@", true)); parse_internal_link("https://t.me/msg?url=%FF&text=1", nullptr); - parse_internal_link("tg:login?codec=12345", unknown_deep_link()); - parse_internal_link("tg:login", unknown_deep_link()); + parse_internal_link("tg:login?codec=12345", unknown_deep_link("tg://login?codec=12345")); + parse_internal_link("tg:login", unknown_deep_link("tg://login")); parse_internal_link("tg:login?code=abacaba", authentication_code("abacaba")); parse_internal_link("tg:login?code=123456", authentication_code("123456")); @@ -349,7 +354,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("t.me/login/123456/123123/12/31/a/s//21w/?asdas#test", authentication_code("123456")); parse_internal_link("tg:login?token=abacaba", qr_code_authentication()); - parse_internal_link("tg:login?token=", unknown_deep_link()); + parse_internal_link("tg:login?token=", unknown_deep_link("tg://login?token=")); parse_internal_link("t.me/joinchat?invite=abcdef", nullptr); parse_internal_link("t.me/joinchat", nullptr); @@ -385,7 +390,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:join?invite=abcdef", chat_invite("abcdef")); parse_internal_link("tg:join?invite=abc%20def", chat_invite("abc%20def")); parse_internal_link("tg://join?invite=abc%30def", chat_invite("abc0def")); - parse_internal_link("tg:join?invite=", unknown_deep_link()); + parse_internal_link("tg:join?invite=", unknown_deep_link("tg://join?invite=")); parse_internal_link("t.me/addstickers?set=abcdef", nullptr); parse_internal_link("t.me/addstickers", nullptr); @@ -403,7 +408,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:addstickers?set=abcdef", sticker_set("abcdef")); parse_internal_link("tg:addstickers?set=abc%30ef", sticker_set("abc0ef")); - parse_internal_link("tg://addstickers?set=", unknown_deep_link()); + parse_internal_link("tg://addstickers?set=", unknown_deep_link("tg://addstickers?set=")); parse_internal_link("t.me/confirmphone?hash=abc%30ef&phone=", nullptr); parse_internal_link("t.me/confirmphone/123456/123123/12/31/a/s//21w/?hash=abc%30ef&phone=123456789", @@ -411,13 +416,16 @@ TEST(Link, parse_internal_link) { parse_internal_link("t.me/confirmphone?hash=abc%30ef&phone=123456789", phone_number_confirmation("abc0ef", "123456789")); - parse_internal_link("tg:confirmphone?hash=abc%30ef&phone=", unknown_deep_link()); + parse_internal_link("tg:confirmphone?hash=abc%30ef&phone=", + unknown_deep_link("tg://confirmphone?hash=abc%30ef&phone=")); parse_internal_link("tg:confirmphone?hash=abc%30ef&phone=123456789", phone_number_confirmation("abc0ef", "123456789")); parse_internal_link("tg://confirmphone?hash=123&phone=123456789123456789", phone_number_confirmation("123", "123456789123456789")); - parse_internal_link("tg://confirmphone?hash=&phone=123456789123456789", unknown_deep_link()); - parse_internal_link("tg://confirmphone?hash=123456789123456789&phone=", unknown_deep_link()); + parse_internal_link("tg://confirmphone?hash=&phone=123456789123456789", + unknown_deep_link("tg://confirmphone?hash=&phone=123456789123456789")); + parse_internal_link("tg://confirmphone?hash=123456789123456789&phone=", + unknown_deep_link("tg://confirmphone?hash=123456789123456789&phone=")); parse_internal_link("t.me/setlanguage?lang=abcdef", nullptr); parse_internal_link("t.me/setlanguage", nullptr); @@ -435,7 +443,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:setlanguage?lang=abcdef", language_pack("abcdef")); parse_internal_link("tg:setlanguage?lang=abc%30ef", language_pack("abc0ef")); - parse_internal_link("tg://setlanguage?lang=", unknown_deep_link()); + parse_internal_link("tg://setlanguage?lang=", unknown_deep_link("tg://setlanguage?lang=")); parse_internal_link("t.me/addtheme?slug=abcdef", nullptr); parse_internal_link("t.me/addtheme", nullptr); @@ -453,7 +461,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:addtheme?slug=abcdef", theme("abcdef")); parse_internal_link("tg:addtheme?slug=abc%30ef", theme("abc0ef")); - parse_internal_link("tg://addtheme?slug=", unknown_deep_link()); + parse_internal_link("tg://addtheme?slug=", unknown_deep_link("tg://addtheme?slug=")); parse_internal_link("t.me/proxy?server=1.2.3.4&port=80&secret=1234567890abcdef1234567890ABCDEF", proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF")); @@ -482,16 +490,19 @@ TEST(Link, parse_internal_link) { proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF")); parse_internal_link("tg:proxy?server=1.2.3.4&port=80adasdas&secret=1234567890abcdef1234567890ABCDEF", proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF")); - parse_internal_link("tg:proxy?server=1.2.3.4&port=adasdas&secret=1234567890abcdef1234567890ABCDEF", - unknown_deep_link()); - parse_internal_link("tg:proxy?server=1.2.3.4&port=65536&secret=1234567890abcdef1234567890ABCDEF", - unknown_deep_link()); + parse_internal_link( + "tg:proxy?server=1.2.3.4&port=adasdas&secret=1234567890abcdef1234567890ABCDEF", + unknown_deep_link("tg://proxy?server=1.2.3.4&port=adasdas&secret=1234567890abcdef1234567890ABCDEF")); + parse_internal_link( + "tg:proxy?server=1.2.3.4&port=65536&secret=1234567890abcdef1234567890ABCDEF", + unknown_deep_link("tg://proxy?server=1.2.3.4&port=65536&secret=1234567890abcdef1234567890ABCDEF")); parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=1234567890abcdef1234567890ABCDEF", proxy_mtproto("google.com", 80, "1234567890abcdef1234567890ABCDEF")); parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=dd1234567890abcdef1234567890ABCDEF", proxy_mtproto("google.com", 80, "dd1234567890abcdef1234567890ABCDEF")); - parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=de1234567890abcdef1234567890ABCDEF", - unknown_deep_link()); + parse_internal_link( + "tg:proxy?server=google.com&port=8%30&secret=de1234567890abcdef1234567890ABCDEF", + unknown_deep_link("tg://proxy?server=google.com&port=8%30&secret=de1234567890abcdef1234567890ABCDEF")); parse_internal_link("t.me/socks?server=1.2.3.4&port=80", proxy_socks("1.2.3.4", 80, "", "")); parse_internal_link("t.me/socks?server=1.2.3.4&port=80adasdas", proxy_socks("1.2.3.4", 80, "", "")); @@ -504,8 +515,9 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:socks?server=1.2.3.4&port=80", proxy_socks("1.2.3.4", 80, "", "")); parse_internal_link("tg:socks?server=1.2.3.4&port=80adasdas", proxy_socks("1.2.3.4", 80, "", "")); - parse_internal_link("tg:socks?server=1.2.3.4&port=adasdas", unknown_deep_link()); - parse_internal_link("tg:socks?server=1.2.3.4&port=65536", unknown_deep_link()); + parse_internal_link("tg:socks?server=1.2.3.4&port=adasdas", + unknown_deep_link("tg://socks?server=1.2.3.4&port=adasdas")); + parse_internal_link("tg:socks?server=1.2.3.4&port=65536", unknown_deep_link("tg://socks?server=1.2.3.4&port=65536")); parse_internal_link("tg:socks?server=google.com&port=8%30", proxy_socks("google.com", 80, "", "")); parse_internal_link("tg:socks?server=google.com&port=8%30&user=1&pass=", proxy_socks("google.com", 80, "1", "")); parse_internal_link("tg:socks?server=google.com&port=8%30&user=&pass=2", proxy_socks("google.com", 80, "", "2")); @@ -517,7 +529,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username&voicechat=", nullptr); parse_internal_link("tg:http://resolve?domain=username&voicechat=", nullptr); parse_internal_link("tg:https://resolve?domain=username&voicechat=", nullptr); - parse_internal_link("tg:resolve?domain=&voicechat=", unknown_deep_link()); + parse_internal_link("tg:resolve?domain=&voicechat=", unknown_deep_link("tg://resolve?domain=&voicechat=")); parse_internal_link("tg:resolve?domain=telegram&&&&&&&voicechat=%30", voice_chat("telegram", "0")); parse_internal_link("t.me/username/0/a//s/as?voicechat=", voice_chat("username", "")); @@ -535,7 +547,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username&start=", nullptr); parse_internal_link("tg:http://resolve?domain=username&start=", nullptr); parse_internal_link("tg:https://resolve?domain=username&start=", nullptr); - parse_internal_link("tg:resolve?domain=&start=", unknown_deep_link()); + parse_internal_link("tg:resolve?domain=&start=", unknown_deep_link("tg://resolve?domain=&start=")); parse_internal_link("tg:resolve?domain=telegram&&&&&&&start=%30", bot_start("telegram", "0")); parse_internal_link("t.me/username/0/a//s/as?start=", bot_start("username", "")); @@ -553,7 +565,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username&startgroup=", nullptr); parse_internal_link("tg:http://resolve?domain=username&startgroup=", nullptr); parse_internal_link("tg:https://resolve?domain=username&startgroup=", nullptr); - parse_internal_link("tg:resolve?domain=&startgroup=", unknown_deep_link()); + parse_internal_link("tg:resolve?domain=&startgroup=", unknown_deep_link("tg://resolve?domain=&startgroup=")); parse_internal_link("tg:resolve?domain=telegram&&&&&&&startgroup=%30", bot_start_in_group("telegram", "0")); parse_internal_link("t.me/username/0/a//s/as?startgroup=", bot_start_in_group("username", "")); @@ -571,7 +583,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username&game=asd", nullptr); parse_internal_link("tg:http://resolve?domain=username&game=asd", nullptr); parse_internal_link("tg:https://resolve?domain=username&game=asd", nullptr); - parse_internal_link("tg:resolve?domain=&game=asd", unknown_deep_link()); + parse_internal_link("tg:resolve?domain=&game=asd", unknown_deep_link("tg://resolve?domain=&game=asd")); parse_internal_link("tg:resolve?domain=telegram&&&&&&&game=%30", game("telegram", "0")); parse_internal_link("t.me/username/0/a//s/as?game=asd", game("username", "asd")); @@ -589,7 +601,7 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:resolve:80?domain=username", nullptr); parse_internal_link("tg:http://resolve?domain=username", nullptr); parse_internal_link("tg:https://resolve?domain=username", nullptr); - parse_internal_link("tg:resolve?domain=", unknown_deep_link()); + parse_internal_link("tg:resolve?domain=", unknown_deep_link("tg://resolve?domain=")); parse_internal_link("tg:resolve?&&&&&&&domain=telegram", public_chat("telegram")); parse_internal_link("t.me/a", public_chat("a")); @@ -621,16 +633,21 @@ TEST(Link, parse_internal_link) { passport_data_request(12345, "asd", "key", "nonce", "")); parse_internal_link("tg://passport?bot_id=12345&public_key=key&scope=asd&payload=nonce", passport_data_request(12345, "asd", "key", "nonce", "")); - parse_internal_link("tg://passport?bot_id=0&public_key=key&scope=asd&payload=nonce", unknown_deep_link()); - parse_internal_link("tg://passport?bot_id=-1&public_key=key&scope=asd&payload=nonce", unknown_deep_link()); - parse_internal_link("tg://passport?bot_id=12345&public_key=&scope=asd&payload=nonce", unknown_deep_link()); - parse_internal_link("tg://passport?bot_id=12345&public_key=key&scope=&payload=nonce", unknown_deep_link()); - parse_internal_link("tg://passport?bot_id=12345&public_key=key&scope=asd&payload=", unknown_deep_link()); + parse_internal_link("tg://passport?bot_id=0&public_key=key&scope=asd&payload=nonce", + unknown_deep_link("tg://passport?bot_id=0&public_key=key&scope=asd&payload=nonce")); + parse_internal_link("tg://passport?bot_id=-1&public_key=key&scope=asd&payload=nonce", + unknown_deep_link("tg://passport?bot_id=-1&public_key=key&scope=asd&payload=nonce")); + parse_internal_link("tg://passport?bot_id=12345&public_key=&scope=asd&payload=nonce", + unknown_deep_link("tg://passport?bot_id=12345&public_key=&scope=asd&payload=nonce")); + parse_internal_link("tg://passport?bot_id=12345&public_key=key&scope=&payload=nonce", + unknown_deep_link("tg://passport?bot_id=12345&public_key=key&scope=&payload=nonce")); + parse_internal_link("tg://passport?bot_id=12345&public_key=key&scope=asd&payload=", + unknown_deep_link("tg://passport?bot_id=12345&public_key=key&scope=asd&payload=")); parse_internal_link("t.me/telegrampassport?bot_id=12345&public_key=key&scope=asd&payload=nonce", public_chat("telegrampassport")); parse_internal_link("tg://settings", settings()); - parse_internal_link("tg://setting", unknown_deep_link()); + parse_internal_link("tg://setting", unknown_deep_link("tg://setting")); parse_internal_link("tg://settings?asdsa?D?SADasD?asD", settings()); parse_internal_link("tg://settings#test", settings()); parse_internal_link("tg://settings/#test", settings());