From 861023f0d64d1ba26c07d42d24ad55315e50ed1b Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 8 May 2019 11:46:35 +0300 Subject: [PATCH] FIX: Skip attachments in click track. --- .../discourse/lib/click-track.js.es6 | 20 +++++++++++-------- test/javascripts/lib/click-track-test.js.es6 | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/click-track.js.es6 b/app/assets/javascripts/discourse/lib/click-track.js.es6 index d744929fcb6..438915c13ff 100644 --- a/app/assets/javascripts/discourse/lib/click-track.js.es6 +++ b/app/assets/javascripts/discourse/lib/click-track.js.es6 @@ -52,6 +52,11 @@ export default { return true; } + let href = ($link.attr("href") || $link.data("href") || "").trim(); + if (!href || href.indexOf("mailto:") === 0) { + return true; + } + if ($link.hasClass("attachment")) { // Warn the user if they cannot download the file. if ( @@ -59,15 +64,14 @@ export default { !Discourse.User.current() ) { bootbox.alert(I18n.t("post.errors.attachment_download_requires_login")); - return false; + } else if (wantsNewWindow(e)) { + const newWindow = window.open(href, "_blank"); + newWindow.opener = null; + newWindow.focus(); + } else { + DiscourseURL.redirectTo(href); } - - return true; - } - - let href = ($link.attr("href") || $link.data("href") || "").trim(); - if (!href || href.indexOf("mailto:") === 0) { - return true; + return false; } const $article = $link.closest( diff --git a/test/javascripts/lib/click-track-test.js.es6 b/test/javascripts/lib/click-track-test.js.es6 index d69b2332cca..20e41e560fd 100644 --- a/test/javascripts/lib/click-track-test.js.es6 +++ b/test/javascripts/lib/click-track-test.js.es6 @@ -70,13 +70,13 @@ QUnit.test("does not track elements with no href", async assert => { }); QUnit.test("does not track attachments", async assert => { - assert.expect(1); sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com"); /* global server */ server.post("/clicks/track", () => assert.ok(false)); - assert.ok(track(generateClickEventOn(".attachment"))); + assert.notOk(track(generateClickEventOn(".attachment"))); + assert.ok(DiscourseURL.redirectTo.calledWith("http://discuss.domain.com/uploads/default/1234/1532357280.txt")); }); QUnit.test("tracks external URLs", async assert => {