FIX: respect nofollow exclusion setting in topic featured links. (#11858)

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.

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 5c3f0b4..4544817 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 b3661ee..061bb13 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 4196457..e37e505 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 da7b823..6a64b90 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

GitHub sha: 9b200aba161787a2499df1dfa31acb37d093820b

This commit appears in #11858 which was approved by eviltrout. It was merged by vinothkannans.