mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: handle link tracking correctly for cdn based urls
(usually attachments)
This commit is contained in:
@@ -29,6 +29,15 @@ class TopicLinkClick < ActiveRecord::Base
|
|||||||
urls << uri.path if uri.try(:host) == Discourse.current_hostname
|
urls << uri.path if uri.try(:host) == Discourse.current_hostname
|
||||||
urls << url.sub(/\?.*$/, '') if url.include?('?')
|
urls << url.sub(/\?.*$/, '') if url.include?('?')
|
||||||
|
|
||||||
|
# add a cdn link
|
||||||
|
if uri && Discourse.asset_host.present?
|
||||||
|
cdn_uri = URI.parse(Discourse.asset_host) rescue nil
|
||||||
|
if cdn_uri && cdn_uri.hostname == uri.hostname && uri.path.starts_with?(cdn_uri.path)
|
||||||
|
is_cdn_link = true
|
||||||
|
urls << uri.path[(cdn_uri.path.length)..-1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
link = TopicLink.select([:id, :user_id])
|
link = TopicLink.select([:id, :user_id])
|
||||||
|
|
||||||
# test for all possible URLs
|
# test for all possible URLs
|
||||||
@@ -54,14 +63,7 @@ class TopicLinkClick < ActiveRecord::Base
|
|||||||
return nil unless uri
|
return nil unless uri
|
||||||
|
|
||||||
# Only redirect to whitelisted hostnames
|
# Only redirect to whitelisted hostnames
|
||||||
return url if WHITELISTED_REDIRECT_HOSTNAMES.include?(uri.hostname)
|
return url if WHITELISTED_REDIRECT_HOSTNAMES.include?(uri.hostname) || is_cdn_link
|
||||||
|
|
||||||
if Discourse.asset_host.present?
|
|
||||||
cdn_uri = URI.parse(Discourse.asset_host) rescue nil
|
|
||||||
if cdn_uri
|
|
||||||
return url if cdn_uri.hostname == uri.hostname && uri.path.starts_with?(cdn_uri.path)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -122,6 +122,24 @@ describe TopicLinkClick do
|
|||||||
ip: '127.0.0.3')
|
ip: '127.0.0.3')
|
||||||
|
|
||||||
expect(url).to eq(nil)
|
expect(url).to eq(nil)
|
||||||
|
|
||||||
|
# cdn better link track
|
||||||
|
path = "/uploads/site/29/5b585f848d8761d5.xls"
|
||||||
|
|
||||||
|
post = Fabricate(:post, topic: @topic, raw: "[test](#{path})")
|
||||||
|
TopicLink.extract_from(post)
|
||||||
|
|
||||||
|
url = TopicLinkClick.create_from(
|
||||||
|
url: "https://cdn.discourse.org/stuff#{path}",
|
||||||
|
topic_id: post.topic_id,
|
||||||
|
post_id: post.id,
|
||||||
|
ip: '127.0.0.3')
|
||||||
|
|
||||||
|
expect(url).to eq("https://cdn.discourse.org/stuff#{path}")
|
||||||
|
|
||||||
|
click = TopicLinkClick.order('id desc').first
|
||||||
|
|
||||||
|
expect(click.topic_link_id).to eq(TopicLink.order('id desc').first.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user