From 9b200aba161787a2499df1dfa31acb37d093820b Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 15 Jun 2021 15:27:43 +0530 Subject: [PATCH] FIX: respect nofollow exclusion setting in topic featured links. (#11858) Previously, nofollow attribute is not removed even when a domain is added to the `exclude_rel_nofollow_domains` site setting. --- .../app/lib/render-topic-featured-link.js | 14 ++++++- .../discourse/tests/acceptance/topic-test.js | 39 +++++++++++++++++++ .../discourse/tests/fixtures/topic.js | 1 + config/site_settings.yml | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/render-topic-featured-link.js b/app/assets/javascripts/discourse/app/lib/render-topic-featured-link.js index 5c3f0b4b24b..45448178fd4 100644 --- a/app/assets/javascripts/discourse/app/lib/render-topic-featured-link.js +++ b/app/assets/javascripts/discourse/app/lib/render-topic-featured-link.js @@ -11,6 +11,16 @@ export function addFeaturedLinkMetaDecorator(decorator) { export function extractLinkMeta(topic) { const href = topic.get("featured_link"); const target = User.currentProp("external_links_in_new_tab") ? "_blank" : ""; + const domain = topic.get("featured_link_root_domain"); + let allowList = topic.siteSettings.exclude_rel_nofollow_domains; + let rel = "nofollow ugc"; + + if (allowList) { + allowList = allowList.split("|"); + if (allowList.includes(domain)) { + rel = rel.replace("nofollow ", ""); + } + } if (!href) { return; @@ -19,8 +29,8 @@ export function extractLinkMeta(topic) { const meta = { target: target, href, - domain: topic.get("featured_link_root_domain"), - rel: "nofollow ugc", + domain: domain, + rel: rel, }; if (_decorators.length) { diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js index b3661ee4cb5..061bb13851c 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js @@ -259,6 +259,45 @@ acceptance("Topic", function (needs) { assert.ok(exists(".category-moderator"), "it has a class applied"); assert.ok(exists(".d-icon-shield-alt"), "it shows an icon"); }); +}); + +acceptance("Topic featured links", function (needs) { + needs.user(); + needs.settings({ + topic_featured_link_enabled: true, + max_topic_title_length: 80, + exclude_rel_nofollow_domains: "example.com", + }); + + test("remove nofollow attribute", async function (assert) { + await visit("/t/-/299/1"); + + const link = queryAll(".title-wrapper .topic-featured-link"); + assert.equal(link.text(), " example.com"); + assert.equal(link.attr("rel"), "ugc"); + }); + + test("remove featured link", async function (assert) { + await visit("/t/-/299/1"); + assert.ok( + exists(".title-wrapper .topic-featured-link"), + "link is shown with topic title" + ); + + await click(".title-wrapper .edit-topic"); + assert.ok( + exists(".title-wrapper .remove-featured-link"), + "link to remove featured link" + ); + + // TODO: decide if we want to test this, test is flaky so it + // was commented out. + // If not fixed by May 2021, delete this code block + // + //await click(".title-wrapper .remove-featured-link"); + //await click(".title-wrapper .submit-edit"); + //assert.ok(!exists(".title-wrapper .topic-featured-link"), "link is gone"); + }); test("Converting to a public topic", async function (assert) { await visit("/t/test-pm/34"); diff --git a/app/assets/javascripts/discourse/tests/fixtures/topic.js b/app/assets/javascripts/discourse/tests/fixtures/topic.js index 4196457e6b0..e37e505657b 100644 --- a/app/assets/javascripts/discourse/tests/fixtures/topic.js +++ b/app/assets/javascripts/discourse/tests/fixtures/topic.js @@ -4648,6 +4648,7 @@ export default { pinned_at: null, pinned_until: null, featured_link: "http://www.example.com/has-title.html", + featured_link_root_domain: "example.com", details: { auto_close_at: null, auto_close_hours: null, diff --git a/config/site_settings.yml b/config/site_settings.yml index da7b823e98a..6a64b90ad9f 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -47,6 +47,7 @@ required: default: "" type: group exclude_rel_nofollow_domains: + client: true default: "" type: list list_type: simple