Allow only t.me links in factchecks.

This commit is contained in:
John Preston 2024-05-30 23:28:30 +04:00
parent 4953246c5d
commit 27eb3e45be
3 changed files with 55 additions and 5 deletions

View file

@ -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}";

View file

@ -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<void(QString, QString)> callback,
const style::InputField *fieldStyle) {
const style::InputField *fieldStyle,
Fn<QString(QString)> 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<bool(
text,
link,
std::move(callback),
fieldStyle));
fieldStyle,
qthelp::validate_url));
return true;
};
}
@ -343,6 +346,52 @@ void InitMessageFieldHandlers(
}
}
[[nodiscard]] bool IsGoodFactcheckUrl(QStringView url) {
return url.startsWith(u"t.me/"_q) || url.startsWith(u"https://t.me/"_q);
}
[[nodiscard]] Fn<bool(
Ui::InputField::EditLinkSelection selection,
QString text,
QString link,
EditLinkAction action)> FactcheckEditLinkCallback(
std::shared_ptr<Main::SessionShow> show,
not_null<Ui::InputField*> 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<void(not_null<Ui::InputField*>)> FactcheckFieldIniter(
std::shared_ptr<Main::SessionShow> show) {
Expects(show != nullptr);
@ -374,7 +423,7 @@ Fn<void(not_null<Ui::InputField*>)> FactcheckFieldIniter(
}
}
));
field->setEditLinkCallback(DefaultEditLinkCallback(show, field));
field->setEditLinkCallback(FactcheckEditLinkCallback(show, field));
InitSpellchecker(show, field);
};
}

View file

@ -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())));