FIX: Open links in external tabs. (#7444)

FIX: Open links in external tabs. (#7444)

diff --git a/app/assets/javascripts/discourse/lib/click-track.js.es6 b/app/assets/javascripts/discourse/lib/click-track.js.es6
index 4728abf..4df89b5 100644
--- a/app/assets/javascripts/discourse/lib/click-track.js.es6
+++ b/app/assets/javascripts/discourse/lib/click-track.js.es6
@@ -50,9 +50,7 @@ export default {
     }
 
     const $link = $(e.currentTarget);
-    if (!isValidLink($link)) {
-      return true;
-    }
+    const tracking = isValidLink($link);
 
     if ($link.hasClass("attachment")) {
       // Warn the user if they cannot download the file.
@@ -81,7 +79,7 @@ export default {
     const ownLink = userId && userId === Discourse.User.currentProp("id");
 
     // Update badge clicks unless it's our own.
-    if (!ownLink) {
+    if (tracking && !ownLink) {
       const $badge = $("span.badge", $link);
       if ($badge.length === 1) {
         const html = $badge.html();
@@ -93,13 +91,15 @@ export default {
       }
     }
 
-    const trackPromise = ajax("/clicks/track", {
-      data: {
-        url: href,
-        post_id: postId,
-        topic_id: topicId
-      }
-    });
+    const trackPromise = tracking
+      ? ajax("/clicks/track", {
+          data: {
+            url: href,
+            post_id: postId,
+            topic_id: topicId
+          }
+        })
+      : Ember.RSVP.resolve();
 
     const isInternal = DiscourseURL.isInternal(href);
     const openExternalInNewTab = Discourse.User.currentProp(
diff --git a/test/javascripts/lib/click-track-test.js.es6 b/test/javascripts/lib/click-track-test.js.es6
index e72d3f5..51d045b 100644
--- a/test/javascripts/lib/click-track-test.js.es6
+++ b/test/javascripts/lib/click-track-test.js.es6
@@ -115,16 +115,15 @@ QUnit.test(
 );
 
 QUnit.test("does not track clicks on lightboxes", async assert => {
-  var clickEvent = generateClickEventOn(".lightbox");
-  assert.ok(track(clickEvent));
+  assert.notOk(track(generateClickEventOn(".lightbox")));
 });
 
 QUnit.test("does not track clicks when forcibly disabled", async assert => {
-  assert.ok(track(generateClickEventOn(".no-track-link")));
+  assert.notOk(track(generateClickEventOn(".no-track-link")));
 });
 
 QUnit.test("does not track clicks on back buttons", async assert => {
-  assert.ok(track(generateClickEventOn(".back")));
+  assert.notOk(track(generateClickEventOn(".back")));
 });
 
 QUnit.test("does not track right clicks inside quotes", async assert => {
@@ -134,11 +133,13 @@ QUnit.test("does not track right clicks inside quotes", async assert => {
 });
 
 QUnit.test("does not track clicks links in quotes", async assert => {
-  assert.ok(track(generateClickEventOn(".quote a:last-child")));
+  Discourse.User.currentProp("external_links_in_new_tab", true);
+  assert.notOk(track(generateClickEventOn(".quote a:last-child")));
+  assert.ok(window.open.calledWith("https://google.com", "_blank"));
 });
 
 QUnit.test("does not track clicks on category badges", async assert => {
-  assert.ok(track(generateClickEventOn(".hashtag")));
+  assert.notOk(track(generateClickEventOn(".hashtag")));
 });
 
 QUnit.test("does not track clicks on mailto", async assert => {

GitHub sha: aef35fae