From 0025c4164b8d406f1e51004aca88b6678dd9edaf Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 16 Sep 2022 01:30:54 +0300 Subject: [PATCH] Add internalLinkTypeInstantView. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/LinkManager.cpp | 18 ++++++++++++++++++ td/telegram/LinkManager.h | 1 + tdutils/td/utils/HttpUrl.cpp | 2 +- test/link.cpp | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 3f802c80a..524c6e9d0 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3725,6 +3725,9 @@ internalLinkTypeFilterSettings = InternalLinkType; //@bot_username Username of the bot that owns the game @game_short_name Short name of the game internalLinkTypeGame bot_username:string game_short_name:string = InternalLinkType; +//@description The link must be opened in an Instant View. Call getWebPageInstantView with the given URL to process the link @url URL to be passed to getWebPageInstantView +internalLinkTypeInstantView url:string = InternalLinkType; + //@description The link is a link to an invoice. Call getPaymentForm with the given invoice name to process the link @invoice_name Name of the invoice internalLinkTypeInvoice invoice_name:string = InternalLinkType; diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index 20f644252..10217df8f 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -337,6 +337,18 @@ class LinkManager::InternalLinkGame final : public InternalLink { } }; +class LinkManager::InternalLinkInstantView final : public InternalLink { + string url_; + + td_api::object_ptr get_internal_link_type_object() const final { + return td_api::make_object(url_); + } + + public: + explicit InternalLinkInstantView(string url) : url_(std::move(url)) { + } +}; + class LinkManager::InternalLinkInvoice final : public InternalLink { string invoice_name_; @@ -1336,6 +1348,12 @@ unique_ptr LinkManager::parse_t_me_link_query(Slice q // /share/url?url=&text= return get_internal_link_message_draft(get_arg("url"), get_arg("text")); } + } else if (path[0] == "iv") { + if (path.size() == 1 && has_arg("url")) { + // /iv?url=&rhash= + return td::make_unique(PSTRING() + << "https://t.me/iv" << copy_arg("url") << copy_arg("rhash")); + } } else if (is_valid_username(path[0])) { if (path.size() >= 2 && to_integer(path[1]) > 0) { // //12345?single&thread=&comment=&t= diff --git a/td/telegram/LinkManager.h b/td/telegram/LinkManager.h index 579fcba97..d9da23a6c 100644 --- a/td/telegram/LinkManager.h +++ b/td/telegram/LinkManager.h @@ -101,6 +101,7 @@ class LinkManager final : public Actor { class InternalLinkDialogInvite; class InternalLinkFilterSettings; class InternalLinkGame; + class InternalLinkInstantView; class InternalLinkInvoice; class InternalLinkLanguage; class InternalLinkLanguageSettings; diff --git a/tdutils/td/utils/HttpUrl.cpp b/tdutils/td/utils/HttpUrl.cpp index ced0a2860..e1d46964b 100644 --- a/tdutils/td/utils/HttpUrl.cpp +++ b/tdutils/td/utils/HttpUrl.cpp @@ -197,7 +197,7 @@ HttpUrlQuery parse_url_query(Slice query) { HttpUrlQuery result; result.path_ = full_split(url_decode(query.substr(0, path_size), false), '/'); - if (!result.path_.empty() && result.path_.back().empty()) { + while (!result.path_.empty() && result.path_.back().empty()) { result.path_.pop_back(); } diff --git a/test/link.cpp b/test/link.cpp index b81774f31..991b3b9c8 100644 --- a/test/link.cpp +++ b/test/link.cpp @@ -88,6 +88,7 @@ static void parse_internal_link(const td::string &url, td::td_api::object_ptr(bot_username, game_short_name); }; + auto instant_view = [](const td::string &url) { + return td::td_api::make_object(url); + }; auto invoice = [](const td::string &invoice_name) { return td::td_api::make_object(invoice_name); }; @@ -638,6 +642,20 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg:setlanguage?lang=abc%30ef", language_pack("abc0ef")); parse_internal_link("tg://setlanguage?lang=", unknown_deep_link("tg://setlanguage?lang=")); + parse_internal_link("http://telegram.dog/iv?url=https://telegram.org&rhash=abcdef&test=1&tg_rhash=1", + instant_view("https://t.me/iv?url=https%3A%2F%2Ftelegram.org&rhash=abcdef")); + parse_internal_link("t.me/iva?url=https://telegram.org&rhash=abcdef", public_chat("iva")); + parse_internal_link("t.me/iv?url=&rhash=abcdef", nullptr); + parse_internal_link("t.me/iv?url=https://telegram.org&rhash=", + instant_view("https://t.me/iv?url=https%3A%2F%2Ftelegram.org&rhash")); + parse_internal_link("t.me/iv//////?url=https://telegram.org&rhash=", + instant_view("https://t.me/iv?url=https%3A%2F%2Ftelegram.org&rhash")); + parse_internal_link("t.me/iv/////1/?url=https://telegram.org&rhash=", nullptr); + parse_internal_link("t.me/iv", nullptr); + parse_internal_link("t.me/iv?#url=https://telegram.org&rhash=abcdef", nullptr); + parse_internal_link("tg:iv?url=https://telegram.org&rhash=abcdef", + unknown_deep_link("tg://iv?url=https://telegram.org&rhash=abcdef")); + parse_internal_link("t.me/addtheme?slug=abcdef", nullptr); parse_internal_link("t.me/addtheme", nullptr); parse_internal_link("t.me/addtheme/", nullptr);