From d080ae4d2dbc37cb8fdd19148c185ed262464735 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Mon, 8 Apr 2013 02:24:12 +0200
Subject: [PATCH] don't track links in oneboxes

---
 .../discourse/components/click_track.js       | 58 +++++++++----------
 .../templates/discourse_post_onebox.hbrs      |  0
 lib/oneboxer/templates/gist_onebox.hbrs       |  2 +-
 .../templates/github_blob_onebox.hbrs         |  2 +-
 .../templates/github_commit_onebox.hbrs       |  2 +-
 lib/oneboxer/templates/oembed_onebox.hbrs     |  2 +-
 lib/oneboxer/templates/simple_onebox.hbrs     |  2 +-
 lib/oneboxer/templates/twitter_onebox.hbrs    |  2 +-
 .../components/oneboxer/amazon_onebox_spec.rb |  2 +-
 .../oneboxer/android_app_store_onebox_spec.rb |  2 +-
 .../oneboxer/apple_app_onebox_spec.rb         |  2 +-
 ...onebox.rb => github_commit_onebox_spec.rb} |  2 +-
 .../oneboxer/wikipedia_onebox_spec.rb         |  2 +-
 13 files changed, 39 insertions(+), 41 deletions(-)
 delete mode 100644 lib/oneboxer/templates/discourse_post_onebox.hbrs
 rename spec/components/oneboxer/{github_commit_onebox.rb => github_commit_onebox_spec.rb} (95%)

diff --git a/app/assets/javascripts/discourse/components/click_track.js b/app/assets/javascripts/discourse/components/click_track.js
index f68943f3c11..d03b38555c2 100644
--- a/app/assets/javascripts/discourse/components/click_track.js
+++ b/app/assets/javascripts/discourse/components/click_track.js
@@ -14,38 +14,39 @@ Discourse.ClickTrack = {
     @param {jQuery.Event} e The click event that occurred
   **/
   trackClick: function(e) {
-    var $a, $article, $badge, count, destination, href, ownLink, postId, topicId, trackingUrl, userId;
-    $a = $(e.currentTarget);
-    if ($a.hasClass('lightbox')) {
-      return;
-    }
+    var $link = $(e.currentTarget);
+    if ($link.hasClass('lightbox')) return true;
+
     e.preventDefault();
 
     // We don't track clicks on quote back buttons
-    if ($a.hasClass('back') || $a.hasClass('quote-other-topic')) return true;
+    if ($link.hasClass('back') || $link.hasClass('quote-other-topic')) return true;
+
+    // We don't track clicks in oneboxes
+    // except when we force it with the "track-link" class
+    if ($link.closest('.onebox-result') && !$link.hasClass('track-link')) return true;
 
     // Remove the href, put it as a data attribute
-    if (!$a.data('href')) {
-      $a.addClass('no-href');
-      $a.data('href', $a.attr('href'));
-      $a.attr('href', null);
+    if (!$link.data('href')) {
+      $link.addClass('no-href');
+      $link.data('href', $link.attr('href'));
+      $link.attr('href', null);
       // Don't route to this URL
-      $a.data('auto-route', true);
+      $link.data('auto-route', true);
     }
 
-    href = $a.data('href');
-    $article = $a.closest('article');
-    postId = $article.data('post-id');
-    topicId = $('#topic').data('topic-id');
-    userId = $a.data('user-id');
-    if (!userId) {
-      userId = $article.data('user-id');
-    }
-    ownLink = userId && (userId === Discourse.get('currentUser.id'));
+    var href = $link.data('href'),
+        $article = $link.closest('article'),
+        postId = $article.data('post-id'),
+        topicId = $('#topic').data('topic-id'),
+        userId = $link.data('user-id');
+
+    if (!userId) userId = $article.data('user-id');
+    var ownLink = userId && (userId === Discourse.get('currentUser.id'));
 
     // Build a Redirect URL
-    trackingUrl = Discourse.getURL("/clicks/track?url=" + encodeURIComponent(href));
-    if (postId && (!$a.data('ignore-post-id'))) {
+    var trackingUrl = Discourse.getURL("/clicks/track?url=" + encodeURIComponent(href));
+    if (postId && (!$link.data('ignore-post-id'))) {
       trackingUrl += "&post_id=" + encodeURI(postId);
     }
     if (topicId) {
@@ -54,23 +55,21 @@ Discourse.ClickTrack = {
 
     // Update badge clicks unless it's our own
     if (!ownLink) {
-      $badge = $('span.badge', $a);
+      var $badge = $('span.badge', $link);
       if ($badge.length === 1) {
-        count = parseInt($badge.html(), 10);
-        $badge.html(count + 1);
+        $badge.html(parseInt($badge.html(), 10) + 1);
       }
     }
 
     // If they right clicked, change the destination href
     if (e.which === 3) {
-      destination = Discourse.SiteSettings.track_external_right_clicks ? trackingUrl : href;
-      $a.attr('href', destination);
+      var destination = Discourse.SiteSettings.track_external_right_clicks ? trackingUrl : href;
+      $link.attr('href', destination);
       return true;
     }
 
     // if they want to open in a new tab, do an AJAX request
     if (e.metaKey || e.ctrlKey || e.which === 2) {
-
       Discourse.ajax(Discourse.getURL("/clicks/track"), {
         data: {
           url: href,
@@ -101,8 +100,7 @@ Discourse.ClickTrack = {
     if (Discourse.get('currentUser.external_links_in_new_tab')) {
       var win = window.open(trackingUrl, '_blank');
       win.focus();
-    }
-    else {
+    } else {
       window.location = trackingUrl;
     }
 
diff --git a/lib/oneboxer/templates/discourse_post_onebox.hbrs b/lib/oneboxer/templates/discourse_post_onebox.hbrs
deleted file mode 100644
index e69de29bb2d..00000000000
diff --git a/lib/oneboxer/templates/gist_onebox.hbrs b/lib/oneboxer/templates/gist_onebox.hbrs
index 4717c38e59e..a63cb8000a6 100644
--- a/lib/oneboxer/templates/gist_onebox.hbrs
+++ b/lib/oneboxer/templates/gist_onebox.hbrs
@@ -1,6 +1,6 @@
 <div class='onebox-result'>
   {{#host}}
-    <a href='{{original_url}}' class='source' target="_blank">
+    <a href='{{original_url}}' class='source track-link' target="_blank">
       {{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
     </a>
   {{/host}}
diff --git a/lib/oneboxer/templates/github_blob_onebox.hbrs b/lib/oneboxer/templates/github_blob_onebox.hbrs
index d27d5eb633e..8899fd4eaa8 100644
--- a/lib/oneboxer/templates/github_blob_onebox.hbrs
+++ b/lib/oneboxer/templates/github_blob_onebox.hbrs
@@ -1,6 +1,6 @@
 <div class='onebox-result'>
   {{#host}}
-    <a href='{{original_url}}' class='source' target="_blank">
+    <a href='{{original_url}}' class='source track-link' target="_blank">
       {{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
     </a>
   {{/host}}
diff --git a/lib/oneboxer/templates/github_commit_onebox.hbrs b/lib/oneboxer/templates/github_commit_onebox.hbrs
index fce8c071870..6d8363b79ca 100644
--- a/lib/oneboxer/templates/github_commit_onebox.hbrs
+++ b/lib/oneboxer/templates/github_commit_onebox.hbrs
@@ -2,7 +2,7 @@
   {{#host}}
     <div class="source">
       <div class="info">
-        <a href="{{html_url}}" target="_blank">
+        <a href="{{html_url}}" class="track-link" target="_blank">
           {{#favicon}}<img class="favicon" src="{{favicon}}"> {{/favicon}}{{host}}
         </a>
       </div>
diff --git a/lib/oneboxer/templates/oembed_onebox.hbrs b/lib/oneboxer/templates/oembed_onebox.hbrs
index ac4da55f036..732cbd86ed6 100644
--- a/lib/oneboxer/templates/oembed_onebox.hbrs
+++ b/lib/oneboxer/templates/oembed_onebox.hbrs
@@ -2,7 +2,7 @@
   {{#host}}
     <div class="source">
       <div class="info">
-        <a href='{{original_url}}' class='source' target="_blank">
+        <a href='{{original_url}}' class='source track-link' target="_blank">
           {{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
         </a>
       </div>
diff --git a/lib/oneboxer/templates/simple_onebox.hbrs b/lib/oneboxer/templates/simple_onebox.hbrs
index 5eda1f98ff0..13983175d6b 100644
--- a/lib/oneboxer/templates/simple_onebox.hbrs
+++ b/lib/oneboxer/templates/simple_onebox.hbrs
@@ -2,7 +2,7 @@
   {{#host}}
     <div class='source'>
       <div class='info'>
-        <a href='{{original_url}}' target="_blank">
+        <a href='{{original_url}}' class="track-link" target="_blank">
           {{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
         </a>
       </div>
diff --git a/lib/oneboxer/templates/twitter_onebox.hbrs b/lib/oneboxer/templates/twitter_onebox.hbrs
index e0c6b1c4c92..e53e550facb 100644
--- a/lib/oneboxer/templates/twitter_onebox.hbrs
+++ b/lib/oneboxer/templates/twitter_onebox.hbrs
@@ -2,7 +2,7 @@
   {{#host}}
     <div class="source">
       <div class="info">
-        <a href='{{original_url}}' target="_blank">
+        <a href='{{original_url}}' class="track-link" target="_blank">
           {{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
         </a>
       </div>
diff --git a/spec/components/oneboxer/amazon_onebox_spec.rb b/spec/components/oneboxer/amazon_onebox_spec.rb
index af994534d23..161b7ae81cf 100644
--- a/spec/components/oneboxer/amazon_onebox_spec.rb
+++ b/spec/components/oneboxer/amazon_onebox_spec.rb
@@ -24,7 +24,7 @@ private
 <div class='onebox-result'>
     <div class='source'>
       <div class='info'>
-        <a href='http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177' target="_blank">
+        <a href='http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177' class="track-link" target="_blank">
           <img class='favicon' src="/assets/favicons/amazon.png"> amazon.com
         </a>
       </div>
diff --git a/spec/components/oneboxer/android_app_store_onebox_spec.rb b/spec/components/oneboxer/android_app_store_onebox_spec.rb
index f28eeab64ac..b7cd4458242 100644
--- a/spec/components/oneboxer/android_app_store_onebox_spec.rb
+++ b/spec/components/oneboxer/android_app_store_onebox_spec.rb
@@ -20,7 +20,7 @@ private
 <div class='onebox-result'>
     <div class='source'>
       <div class='info'>
-        <a href='https://play.google.com/store/apps/details?id=com.moosoft.parrot' target="_blank">
+        <a href='https://play.google.com/store/apps/details?id=com.moosoft.parrot' class="track-link" target="_blank">
           <img class='favicon' src="/assets/favicons/google_play.png"> play.google.com
         </a>
       </div>
diff --git a/spec/components/oneboxer/apple_app_onebox_spec.rb b/spec/components/oneboxer/apple_app_onebox_spec.rb
index 3e6a5c99637..a2812fc3ce4 100644
--- a/spec/components/oneboxer/apple_app_onebox_spec.rb
+++ b/spec/components/oneboxer/apple_app_onebox_spec.rb
@@ -20,7 +20,7 @@ private
 <div class='onebox-result'>
     <div class='source'>
       <div class='info'>
-        <a href='https://itunes.apple.com/us/app/minecraft-pocket-edition-lite/id479651754' target="_blank">
+        <a href='https://itunes.apple.com/us/app/minecraft-pocket-edition-lite/id479651754' class="track-link" target="_blank">
           <img class='favicon' src="/assets/favicons/apple.png"> itunes.apple.com
         </a>
       </div>
diff --git a/spec/components/oneboxer/github_commit_onebox.rb b/spec/components/oneboxer/github_commit_onebox_spec.rb
similarity index 95%
rename from spec/components/oneboxer/github_commit_onebox.rb
rename to spec/components/oneboxer/github_commit_onebox_spec.rb
index ae1f01d7594..7d0c962c1bf 100644
--- a/spec/components/oneboxer/github_commit_onebox.rb
+++ b/spec/components/oneboxer/github_commit_onebox_spec.rb
@@ -24,7 +24,7 @@ private
 <div class="onebox-result">
     <div class="source">
       <div class="info">
-        <a href="https://github.com/discourse/discourse/commit/ee76f1926defa8309b3a7ea64a25707519529a13" target="_blank">
+        <a href="https://github.com/discourse/discourse/commit/ee76f1926defa8309b3a7ea64a25707519529a13" class="track-link" target="_blank">
           <img class="favicon" src="/assets/favicons/github.png"> github.com
         </a>
       </div>
diff --git a/spec/components/oneboxer/wikipedia_onebox_spec.rb b/spec/components/oneboxer/wikipedia_onebox_spec.rb
index 443148dca12..e31d6e28c92 100644
--- a/spec/components/oneboxer/wikipedia_onebox_spec.rb
+++ b/spec/components/oneboxer/wikipedia_onebox_spec.rb
@@ -39,7 +39,7 @@ private
 <div class='onebox-result'>
     <div class='source'>
       <div class='info'>
-        <a href='http://en.wikipedia.org/wiki/Ruby' target="_blank">
+        <a href='http://en.wikipedia.org/wiki/Ruby' class="track-link" target="_blank">
           <img class='favicon' src="/assets/favicons/wikipedia.png"> en.wikipedia.org
         </a>
       </div>