From 88199d18650da6658c48a5c1ee7d90c59e76d618 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 19 Oct 2023 13:20:48 +0300 Subject: [PATCH] Skip confirmation in link preview opening for visible URLs. --- td/telegram/MessageContent.cpp | 9 ++++++--- td/telegram/MessageEntity.cpp | 17 +++++++++++++++++ td/telegram/MessageEntity.h | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 22ca3f4d2..6b43a40c8 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -6170,10 +6170,13 @@ tl_object_ptr get_message_content_object(const MessageCo return td_api::make_object(std::move(animated_emoji), m->text.text); } } + auto web_page = td->web_pages_manager_->get_web_page_object(m->web_page_id, m->force_small_media, + m->force_large_media, m->skip_web_page_confirmation); + if (web_page != nullptr && !web_page->skip_confirmation_ && is_visible_url(m->text, web_page->url_)) { + web_page->skip_confirmation_ = true; + } return make_tl_object( - get_formatted_text_object(m->text, skip_bot_commands, max_media_timestamp), - td->web_pages_manager_->get_web_page_object(m->web_page_id, m->force_small_media, m->force_large_media, - m->skip_web_page_confirmation), + get_formatted_text_object(m->text, skip_bot_commands, max_media_timestamp), std::move(web_page), m->web_page_url); } case MessageContentType::Unsupported: diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index f78ec477d..cc6c4f7a7 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -1834,6 +1834,23 @@ string get_first_url(const FormattedText &text) { return string(); } +bool is_visible_url(const FormattedText &text, const string &url) { + auto url_size = static_cast(utf8_utf16_length(url)); + auto cur_offset = 0; + Slice left_text = text.text; + for (auto &entity : text.entities) { + if (entity.type == MessageEntity::Type::Url && url_size == entity.length) { + left_text = utf8_utf16_substr(left_text, entity.offset - cur_offset); + cur_offset = entity.offset; + if (begins_with(left_text, url)) { + return true; + } + } + } + + return false; +} + Result> parse_markdown(string &text) { size_t result_size = 0; vector entities; diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index ea20d5e81..a8d2ec50c 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -185,6 +185,8 @@ void remove_empty_entities(vector &entities); string get_first_url(const FormattedText &text); +bool is_visible_url(const FormattedText &text, const string &url); + Result> parse_markdown(string &text); Result> parse_markdown_v2(string &text);