From 1bb4a2007b993f9975dcb6ade152d1533ab0ae82 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 12 Apr 2024 01:01:26 +0300 Subject: [PATCH] Add td_api::internalLinkTypeBusinessChat. --- td/generate/scheme/td_api.tl | 9 +++++++-- td/telegram/LinkManager.cpp | 36 +++++++++++++++++++++++++++++++++--- td/telegram/LinkManager.h | 1 + td/telegram/Td.cpp | 4 ++-- test/link.cpp | 23 +++++++++++++++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2d9a260bb..4fa308c07 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5964,6 +5964,11 @@ internalLinkTypeBotStart bot_username:string start_parameter:string autostart:Bo //@administrator_rights Expected administrator rights for the bot; may be null internalLinkTypeBotStartInGroup bot_username:string start_parameter:string administrator_rights:chatAdministratorRights = InternalLinkType; +//@description The link is a link to a business chat. Use getBusinessChatLinkInfo with the provided link name to get information about the link, +//-then open received private chat and replace chat draft with the provided text +//@link_name Name of the link +internalLinkTypeBusinessChat link_name:string = InternalLinkType; + //@description The link is a link to the change phone number section of the app internalLinkTypeChangePhoneNumber = InternalLinkType; @@ -9789,8 +9794,8 @@ editBusinessChatLink link:string link_info:inputBusinessChatLink = BusinessChatL //@description Deletes a business chat link of the current account @link The link to delete deleteBusinessChatLink link:string = Ok; -//@description Returns information about a business chat link @link The link -getBusinessChatLinkInfo link:string = BusinessChatLinkInfo; +//@description Returns information about a business chat link @link_name Name of the link +getBusinessChatLinkInfo link_name:string = BusinessChatLinkInfo; //@description Returns an HTTPS link, which can be used to get information about the current user diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index cdaf2a763..04767def3 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -384,6 +384,18 @@ class LinkManager::InternalLinkBotStartInGroup final : public InternalLink { } }; +class LinkManager::InternalLinkBusinessChat final : public InternalLink { + string link_name_; + + td_api::object_ptr get_internal_link_type_object() const final { + return td_api::make_object(link_name_); + } + + public: + explicit InternalLinkBusinessChat(string link_name) : link_name_(std::move(link_name)) { + } +}; + class LinkManager::InternalLinkChangePhoneNumber final : public InternalLink { td_api::object_ptr get_internal_link_type_object() const final { return td_api::make_object(); @@ -1130,9 +1142,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 disallowed_subdomains( - {"addemoji", "addlist", "addstickers", "addtheme", "auth", "boost", "confirmphone", - "contact", "giftcode", "invoice", "joinchat", "login", "proxy", "setlanguage", - "share", "socks", "web", "a", "k", "z"}); + {"addemoji", "addlist", "addstickers", "addtheme", "auth", "boost", "confirmphone", + "contact", "giftcode", "invoice", "joinchat", "login", "m", "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_; @@ -1514,6 +1526,11 @@ unique_ptr LinkManager::parse_tg_link_query(Slice que if (has_arg("slug")) { return td::make_unique(url_query.get_arg("slug").str()); } + } else if (path.size() == 1 && path[0] == "message") { + // message?slug= + if (has_arg("slug")) { + return td::make_unique(url_query.get_arg("slug").str()); + } } else if (path.size() == 1 && (path[0] == "share" || path[0] == "msg" || path[0] == "msg_url")) { // msg_url?url= // msg_url?url=&text= @@ -1669,6 +1686,11 @@ unique_ptr LinkManager::parse_t_me_link_query(Slice q // /giftcode/ return td::make_unique(path[1]); } + } else if (path[0] == "m") { + if (path.size() >= 2 && !path[1].empty()) { + // /m/ + return td::make_unique(path[1]); + } } else if (path[0][0] == '$') { if (path[0].size() >= 2) { // /$ @@ -2039,6 +2061,14 @@ Result LinkManager::get_internal_link_impl(const td_api::InternalLinkTyp return PSTRING() << get_t_me_url() << link->bot_username_ << "?startgroup" << start_parameter << admin; } } + case td_api::internalLinkTypeBusinessChat::ID: { + auto link = static_cast(type_ptr); + if (is_internal) { + return PSTRING() << "tg://message?slug=" << url_encode(link->link_name_); + } else { + return PSTRING() << get_t_me_url() << "m/" << url_encode(link->link_name_); + } + } case td_api::internalLinkTypeChangePhoneNumber::ID: if (!is_internal) { return Status::Error("HTTP link is unavailable for the link type"); diff --git a/td/telegram/LinkManager.h b/td/telegram/LinkManager.h index 68954d5da..c5273b668 100644 --- a/td/telegram/LinkManager.h +++ b/td/telegram/LinkManager.h @@ -124,6 +124,7 @@ class LinkManager final : public Actor { class InternalLinkBotAddToChannel; class InternalLinkBotStart; class InternalLinkBotStartInGroup; + class InternalLinkBusinessChat; class InternalLinkChangePhoneNumber; class InternalLinkConfirmPhone; class InternalLinkDefaultMessageAutoDeleteTimerSettings; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index d39a6b21c..dd1612004 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8013,9 +8013,9 @@ void Td::on_request(uint64 id, td_api::deleteBusinessChatLink &request) { void Td::on_request(uint64 id, td_api::getBusinessChatLinkInfo &request) { CHECK_IS_USER(); - CLEAN_INPUT_STRING(request.link_); + CLEAN_INPUT_STRING(request.link_name_); CREATE_REQUEST_PROMISE(); - business_manager_->get_business_chat_link_info(request.link_, std::move(promise)); + business_manager_->get_business_chat_link_info(request.link_name_, std::move(promise)); } void Td::on_request(uint64 id, td_api::setSupergroupUsername &request) { diff --git a/test/link.cpp b/test/link.cpp index 42f80c3e1..45217026e 100644 --- a/test/link.cpp +++ b/test/link.cpp @@ -219,6 +219,10 @@ static auto bot_start_in_group(const td::string &bot_username, const td::string std::move(administrator_rights)); } +static auto business_chat(const td::string &link_name) { + return td::td_api::make_object(link_name); +} + static auto change_phone_number() { return td::td_api::make_object(); } @@ -672,6 +676,24 @@ TEST(Link, parse_internal_link_part2) { 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("t.me/m?slug=abcdef", nullptr); + parse_internal_link("t.me/m", nullptr); + parse_internal_link("t.me/m/", nullptr); + parse_internal_link("t.me/m//abcdef", nullptr); + parse_internal_link("t.me/m?/abcdef", nullptr); + parse_internal_link("t.me/m/?abcdef", nullptr); + parse_internal_link("t.me/m/#abcdef", nullptr); + parse_internal_link("t.me/m/abacaba", business_chat("abacaba")); + parse_internal_link("t.me/m/aba%20aba", business_chat("aba aba")); + parse_internal_link("t.me/m/123456a", business_chat("123456a")); + parse_internal_link("t.me/m/12345678901", business_chat("12345678901")); + parse_internal_link("t.me/m/123456", business_chat("123456")); + parse_internal_link("t.me/m/123456/123123/12/31/a/s//21w/?asdas#test", business_chat("123456")); + + parse_internal_link("tg:message?slug=abcdef", business_chat("abcdef")); + parse_internal_link("tg:message?slug=abc%30ef", business_chat("abc0ef")); + parse_internal_link("tg://message?slug=", unknown_deep_link("tg://message?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)); @@ -1308,6 +1330,7 @@ TEST(Link, parse_internal_link_part4) { parse_internal_link("invoice.t.me", nullptr); parse_internal_link("joinchat.t.me", nullptr); parse_internal_link("login.t.me", nullptr); + parse_internal_link("m.t.me", nullptr); parse_internal_link("proxy.t.me", nullptr); parse_internal_link("setlanguage.t.me", nullptr); parse_internal_link("share.t.me", nullptr);