2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'discourse'
|
|
|
|
require 'ipaddr'
|
2015-02-22 13:47:18 -06:00
|
|
|
require 'url_helper'
|
|
|
|
|
|
|
|
class TopicLinkClickHelper
|
|
|
|
include UrlHelper
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
class TopicLinkClick < ActiveRecord::Base
|
|
|
|
belongs_to :topic_link, counter_cache: :clicks
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates_presence_of :topic_link_id
|
2013-06-24 17:30:32 -05:00
|
|
|
validates_presence_of :ip_address
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-04-01 15:59:25 -05:00
|
|
|
WHITELISTED_REDIRECT_HOSTNAMES = Set.new(%W{www.youtube.com youtu.be})
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Create a click from a URL and post_id
|
|
|
|
def self.create_from(args={})
|
2015-02-22 13:47:18 -06:00
|
|
|
url = args[:url]
|
|
|
|
return nil if url.blank?
|
|
|
|
|
|
|
|
helper = TopicLinkClickHelper.new
|
|
|
|
uri = URI.parse(url) rescue nil
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2015-02-22 13:47:18 -06:00
|
|
|
urls = Set.new
|
|
|
|
urls << url
|
|
|
|
if url =~ /^http/
|
|
|
|
urls << url.sub(/^https/, 'http')
|
|
|
|
urls << url.sub(/^http:/, 'https:')
|
|
|
|
urls << helper.schemaless(url)
|
2014-01-14 13:59:51 -06:00
|
|
|
end
|
2015-02-22 13:47:18 -06:00
|
|
|
urls << helper.absolute_without_cdn(url)
|
|
|
|
urls << uri.path if uri.try(:host) == Discourse.current_hostname
|
|
|
|
urls << url.sub(/\?.*$/, '') if url.include?('?')
|
|
|
|
|
|
|
|
link = TopicLink.select([:id, :user_id])
|
|
|
|
|
|
|
|
# test for all possible URLs
|
|
|
|
link = link.where(Array.new(urls.count, "url = ?").join(" OR "), *urls)
|
2014-01-14 13:59:51 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Find the forum topic link
|
|
|
|
link = link.where(post_id: args[:post_id]) if args[:post_id].present?
|
|
|
|
|
|
|
|
# If we don't have a post, just find the first occurance of the link
|
|
|
|
link = link.where(topic_id: args[:topic_id]) if args[:topic_id].present?
|
|
|
|
link = link.first
|
|
|
|
|
2015-02-22 13:47:18 -06:00
|
|
|
# If no link is found...
|
2013-07-27 12:18:37 -05:00
|
|
|
unless link.present?
|
2015-02-22 13:47:18 -06:00
|
|
|
# ... return the url for relative links or when using the same host
|
|
|
|
return url if url =~ /^\// || uri.try(:host) == Discourse.current_hostname
|
2014-11-20 13:01:48 -06:00
|
|
|
|
2015-02-22 13:47:18 -06:00
|
|
|
# If we have it somewhere else on the site, just allow the redirect.
|
|
|
|
# This is likely due to a onebox of another topic.
|
|
|
|
link = TopicLink.find_by(url: url)
|
2015-04-01 15:59:25 -05:00
|
|
|
return link.url if link.present?
|
|
|
|
|
|
|
|
# Only redirect to whitelisted hostnames
|
|
|
|
return WHITELISTED_REDIRECT_HOSTNAMES.include?(uri.hostname) ? url : nil
|
2013-07-27 12:18:37 -05:00
|
|
|
end
|
|
|
|
|
2015-02-22 13:47:18 -06:00
|
|
|
return url if args[:user_id] && link.user_id == args[:user_id]
|
2013-07-26 16:29:43 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Rate limit the click counts to once in 24 hours
|
|
|
|
rate_key = "link-clicks:#{link.id}:#{args[:user_id] || args[:ip]}"
|
|
|
|
if $redis.setnx(rate_key, "1")
|
|
|
|
$redis.expire(rate_key, 1.day.to_i)
|
2013-06-24 17:30:32 -05:00
|
|
|
create!(topic_link_id: link.id, user_id: args[:user_id], ip_address: args[:ip])
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2015-02-22 13:47:18 -06:00
|
|
|
url
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_link_clicks
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# topic_link_id :integer not null
|
|
|
|
# user_id :integer
|
2014-08-27 00:19:25 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-12-05 00:40:35 -06:00
|
|
|
# ip_address :inet not null
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-05-27 20:49:50 -05:00
|
|
|
# by_link (topic_link_id)
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|