From bf0caca138ef60a0adbddbd52842c1bb43470da1 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 31 May 2022 01:41:01 +0300 Subject: [PATCH] Simplify LinkManager::check_link usage. --- td/telegram/BotMenuButton.cpp | 3 +-- td/telegram/LinkManager.cpp | 23 ++++++++++++++++++++++- td/telegram/LinkManager.h | 7 ++++++- td/telegram/MessageEntity.cpp | 29 ++++++++++++++--------------- td/telegram/ReplyMarkup.cpp | 20 ++++++++------------ 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/td/telegram/BotMenuButton.cpp b/td/telegram/BotMenuButton.cpp index 4b6cfc341..4676f856d 100644 --- a/td/telegram/BotMenuButton.cpp +++ b/td/telegram/BotMenuButton.cpp @@ -143,8 +143,7 @@ void set_menu_button(Td *td, UserId user_id, td_api::object_ptrurl_, true, !G()->is_test_dc()); if (r_url.is_error()) { - return promise.set_error(Status::Error(400, PSLICE() << "Menu button web app URL '" << menu_button->url_ - << "' is invalid: " << r_url.error().message())); + return promise.set_error(Status::Error(400, PSLICE() << "Menu button Web App " << r_url.error().message())); } input_bot_menu_button = telegram_api::make_object(menu_button->text_, r_url.ok()); } diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index e16acd39d..ca07fc961 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -708,7 +708,28 @@ static bool tolower_begins_with(Slice str, Slice prefix) { return true; } -Result LinkManager::check_link(Slice link, bool http_only, bool https_only) { +Result LinkManager::check_link(CSlice link, bool http_only, bool https_only) { + auto result = check_link_impl(link, http_only, https_only); + if (result.is_ok()) { + return std::move(result); + } + auto error = result.move_as_error(); + if (check_utf8(link)) { + return Status::Error(400, PSLICE() << "URL '" << link << "' is invalid: " << error.message()); + } else { + return Status::Error(400, PSLICE() << "URL is invalid: " << error.message()); + } +} + +string LinkManager::get_checked_link(Slice link, bool http_only, bool https_only) { + auto result = check_link_impl(link, http_only, https_only); + if (result.is_ok()) { + return result.move_as_ok(); + } + return string(); +} + +Result LinkManager::check_link_impl(Slice link, bool http_only, bool https_only) { bool is_tg = false; bool is_ton = false; if (tolower_begins_with(link, "tg:")) { diff --git a/td/telegram/LinkManager.h b/td/telegram/LinkManager.h index 810129e06..9fcf91e16 100644 --- a/td/telegram/LinkManager.h +++ b/td/telegram/LinkManager.h @@ -47,7 +47,10 @@ class LinkManager final : public Actor { }; // checks whether the link is a valid tg, ton or HTTP(S) URL and returns it in a canonical form - static Result check_link(Slice link, bool http_only = false, bool https_only = false); + static Result check_link(CSlice link, bool http_only = false, bool https_only = false); + + // same as check_link, but returns an empty string instead of an error + static string get_checked_link(Slice link, bool http_only = false, bool https_only = false); // checks whether the link is a supported tg or t.me link and parses it static unique_ptr parse_internal_link(Slice link); @@ -128,6 +131,8 @@ class LinkManager final : public Actor { static unique_ptr get_internal_link_message_draft(Slice url, Slice text); + static Result check_link_impl(Slice link, bool http_only, bool https_only); + Td *td_; ActorShared<> parent_; diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index db4c2029f..e0561b8e7 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -1876,9 +1876,9 @@ Result> parse_markdown(string &text) { if (user_id.is_valid()) { entities.emplace_back(entity_offset, entity_length, user_id); } else { - auto r_url = LinkManager::check_link(url); - if (r_url.is_ok()) { - entities.emplace_back(MessageEntity::Type::TextUrl, entity_offset, entity_length, r_url.move_as_ok()); + url = LinkManager::get_checked_link(url); + if (!url.empty()) { + entities.emplace_back(MessageEntity::Type::TextUrl, entity_offset, entity_length, std::move(url)); } } break; @@ -2092,11 +2092,11 @@ static Result> do_parse_markdown_v2(CSlice text, string &r } user_id = LinkManager::get_link_user_id(url); if (!user_id.is_valid()) { - auto r_url = LinkManager::check_link(url); - if (r_url.is_error()) { + url = LinkManager::get_checked_link(url); + if (url.empty()) { skip_entity = true; } else { - argument = r_url.move_as_ok(); + argument = std::move(url); } } break; @@ -2169,7 +2169,7 @@ static vector find_text_url_entities_v3(Slice text) { if (url_end < size) { Slice url = text.substr(url_begin + 1, url_end - url_begin - 1); - if (LinkManager::check_link(url).is_ok()) { + if (!LinkManager::get_checked_link(url).empty()) { result.push_back(text.substr(text_begin, text_end - text_begin + 1)); result.push_back(text.substr(url_begin, url_end - url_begin + 1)); } @@ -2241,7 +2241,7 @@ static FormattedText parse_text_url_entities_v3(Slice text, const vector> do_parse_html(CSlice text, string &result) if (user_id.is_valid()) { entities.emplace_back(entity_offset, entity_length, user_id); } else { - auto r_url = LinkManager::check_link(url); - if (r_url.is_ok()) { - entities.emplace_back(MessageEntity::Type::TextUrl, entity_offset, entity_length, r_url.move_as_ok()); + url = LinkManager::get_checked_link(url); + if (!url.empty()) { + entities.emplace_back(MessageEntity::Type::TextUrl, entity_offset, entity_length, std::move(url)); } } } else if (tag_name == "pre") { @@ -3284,7 +3284,7 @@ Result> get_message_entities(const ContactsManager *contac } auto r_url = LinkManager::check_link(entity->url_); if (r_url.is_error()) { - return Status::Error(400, PSTRING() << "Wrong URL entity specified: " << r_url.error().message()); + return Status::Error(400, PSTRING() << "Entity " << r_url.error().message()); } entities.emplace_back(MessageEntity::Type::TextUrl, offset, length, r_url.move_as_ok()); break; @@ -3418,8 +3418,7 @@ vector get_message_entities(const ContactsManager *contacts_manag auto entity = static_cast(server_entity.get()); auto r_url = LinkManager::check_link(entity->url_); if (r_url.is_error()) { - LOG(ERROR) << "Wrong URL entity: \"" << entity->url_ << "\": " << r_url.error().message() << " from " - << source; + LOG(ERROR) << "Entity " << r_url.error().message() << " from " << source; continue; } entities.emplace_back(MessageEntity::Type::TextUrl, entity->offset_, entity->length_, r_url.move_as_ok()); @@ -3540,7 +3539,7 @@ vector get_message_entities(vectorurl_); if (r_url.is_error()) { - LOG(WARNING) << "Wrong URL entity: \"" << entity->url_ << "\": " << r_url.error().message(); + LOG(WARNING) << "Entity " << r_url.error().message(); continue; } entities.emplace_back(MessageEntity::Type::TextUrl, entity->offset_, entity->length_, r_url.move_as_ok()); diff --git a/td/telegram/ReplyMarkup.cpp b/td/telegram/ReplyMarkup.cpp index 0ddc9dc2f..26815179e 100644 --- a/td/telegram/ReplyMarkup.cpp +++ b/td/telegram/ReplyMarkup.cpp @@ -442,18 +442,17 @@ static Result get_keyboard_button(tl_object_ptr(button->type_); auto user_id = LinkManager::get_link_user_id(button_type->url_); if (user_id.is_valid()) { - return Status::Error(400, "Link to a user can't be used in web app URL buttons"); + return Status::Error(400, "Link to a user can't be used in Web App URL buttons"); } auto r_url = LinkManager::check_link(button_type->url_, true, !G()->is_test_dc()); if (r_url.is_error()) { - return Status::Error(400, PSLICE() << "Inline keyboard button web app URL '" << button_type->url_ - << "' is invalid: " << r_url.error().message()); + return Status::Error(400, PSLICE() << "Keyboard button Web App " << r_url.error().message()); } current_button.type = KeyboardButton::Type::WebView; current_button.url = std::move(button_type->url_); @@ -491,8 +490,7 @@ static Result get_inline_keyboard_button(tl_object_ptrurl_); if (r_url.is_error()) { - return Status::Error(400, PSLICE() << "Inline keyboard button URL '" << button_type->url_ - << "' is invalid: " << r_url.error().message()); + return Status::Error(400, PSLICE() << "Inline keyboard button " << r_url.error().message()); } current_button.type = InlineKeyboardButton::Type::Url; current_button.data = r_url.move_as_ok(); @@ -541,8 +539,7 @@ static Result get_inline_keyboard_button(tl_object_ptrurl_, true); if (r_url.is_error()) { - return Status::Error(400, PSLICE() << "Inline keyboard button login URL '" << button_type->url_ - << "' is invalid: " << r_url.error().message()); + return Status::Error(400, PSLICE() << "Inline keyboard button login " << r_url.error().message()); } current_button.type = InlineKeyboardButton::Type::UrlAuth; current_button.data = r_url.move_as_ok(); @@ -573,17 +570,16 @@ static Result get_inline_keyboard_button(tl_object_ptr(button->type_); auto user_id = LinkManager::get_link_user_id(button_type->url_); if (user_id.is_valid()) { - return Status::Error(400, "Link to a user can't be used in web app URL buttons"); + return Status::Error(400, "Link to a user can't be used in Web App URL buttons"); } auto r_url = LinkManager::check_link(button_type->url_, true, !G()->is_test_dc()); if (r_url.is_error()) { - return Status::Error(400, PSLICE() << "Inline keyboard button web app URL '" << button_type->url_ - << "' is invalid: " << r_url.error().message()); + return Status::Error(400, PSLICE() << "Inline keyboard button Web App " << r_url.error().message()); } current_button.type = InlineKeyboardButton::Type::WebView; current_button.data = r_url.move_as_ok(); if (!clean_input_string(current_button.data)) { - return Status::Error(400, "Inline keyboard button web app URL must be encoded in UTF-8"); + return Status::Error(400, "Inline keyboard button Web App URL must be encoded in UTF-8"); } break; }