From 27eb3e45bed5916fcb018b9a427fc26b8cfd9787 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 30 May 2024 23:28:30 +0400 Subject: [PATCH] Allow only t.me links in factchecks. --- Telegram/Resources/langs/lang.strings | 1 + .../chat_helpers/message_field.cpp | 57 +++++++++++++++++-- .../view/history_view_context_menu.cpp | 2 +- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 4d1798501..1b8491c85 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -3305,6 +3305,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_factcheck_edit_done" = "Fact check edited."; "lng_factcheck_remove_done" = "Fact check removed."; "lng_factcheck_bottom" = "This clarification was provided by a fact checking agency assigned by the department of the government of your country ({country}) responsible for combatting misinformation."; +"lng_factcheck_links" = "Only **t.me/** links are allowed."; "lng_translate_show_original" = "Show Original"; "lng_translate_bar_to" = "Translate to {name}"; diff --git a/Telegram/SourceFiles/chat_helpers/message_field.cpp b/Telegram/SourceFiles/chat_helpers/message_field.cpp index 8b5f3c373..779bfb4d5 100644 --- a/Telegram/SourceFiles/chat_helpers/message_field.cpp +++ b/Telegram/SourceFiles/chat_helpers/message_field.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/shortcuts.h" #include "core/application.h" #include "core/core_settings.h" +#include "ui/text/text_utilities.h" #include "ui/toast/toast.h" #include "ui/wrap/vertical_layout.h" #include "ui/widgets/buttons.h" @@ -115,7 +116,8 @@ void EditLinkBox( const QString &startText, const QString &startLink, Fn callback, - const style::InputField *fieldStyle) { + const style::InputField *fieldStyle, + Fn validate) { Expects(callback != nullptr); const auto &fieldSt = fieldStyle ? *fieldStyle : st::defaultInputField; @@ -160,7 +162,7 @@ void EditLinkBox( const auto submit = [=] { const auto linkText = text->getLastText(); - const auto linkUrl = qthelp::validate_url(url->getLastText()); + const auto linkUrl = validate(url->getLastText()); if (linkText.isEmpty()) { text->showError(); return; @@ -312,7 +314,8 @@ Fn FactcheckEditLinkCallback( + std::shared_ptr show, + not_null field) { + const auto weak = Ui::MakeWeak(field); + return [=]( + EditLinkSelection selection, + QString text, + QString link, + EditLinkAction action) { + const auto validate = [=](QString url) { + if (IsGoodFactcheckUrl(url)) { + const auto start = u"https://"_q; + return url.startsWith(start) ? url : (start + url); + } + show->showToast( + tr::lng_factcheck_links(tr::now, Ui::Text::RichLangValue)); + return QString(); + }; + if (action == EditLinkAction::Check) { + return IsGoodFactcheckUrl(link); + } + auto callback = [=](const QString &text, const QString &link) { + if (const auto strong = weak.data()) { + strong->commitMarkdownLinkEdit(selection, text, link); + } + }; + show->showBox(Box( + EditLinkBox, + show, + text, + link, + std::move(callback), + nullptr, + validate)); + return true; + }; +} + Fn)> FactcheckFieldIniter( std::shared_ptr show) { Expects(show != nullptr); @@ -374,7 +423,7 @@ Fn)> FactcheckFieldIniter( } } )); - field->setEditLinkCallback(DefaultEditLinkCallback(show, field)); + field->setEditLinkCallback(FactcheckEditLinkCallback(show, field)); InitSpellchecker(show, field); }; } diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index b217777a5..a53ab2028 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -734,7 +734,7 @@ void AddFactcheckAction( const auto limit = session->factchecks().lengthLimit(); const auto controller = request.navigation->parentController(); controller->show(Box(EditFactcheckBox, text, limit, [=]( - TextWithEntities result) { + TextWithEntities result) { const auto show = controller->uiShow(); session->factchecks().save(itemId, text, result, show); }, FactcheckFieldIniter(controller->uiShow())));