2013-02-05 14:16:51 -05:00
|
|
|
class IncomingLink < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
2013-05-01 18:12:02 -04:00
|
|
|
belongs_to :user
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-02 04:33:29 -05:00
|
|
|
validates :url, presence: true
|
2013-04-26 02:18:41 -04:00
|
|
|
validate :referer_valid
|
|
|
|
|
2013-03-02 04:33:29 -05:00
|
|
|
before_validation :extract_domain
|
|
|
|
before_validation :extract_topic_and_post
|
|
|
|
after_create :update_link_counts
|
|
|
|
|
2013-04-26 02:18:41 -04:00
|
|
|
def self.add(request,current_user=nil)
|
|
|
|
user_id, host, referer = nil
|
|
|
|
|
|
|
|
if request['u']
|
2014-05-06 09:41:59 -04:00
|
|
|
u = User.select(:id).find_by(username_lower: request["u"].downcase)
|
2013-04-26 02:18:41 -04:00
|
|
|
user_id = u.id if u
|
|
|
|
end
|
2013-04-24 04:05:35 -04:00
|
|
|
|
|
|
|
if request.referer.present?
|
2013-12-29 22:05:44 -05:00
|
|
|
begin
|
|
|
|
host = URI.parse(request.referer).host
|
|
|
|
referer = request.referer[0..999]
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# bad uri, skip
|
|
|
|
end
|
2013-04-26 02:18:41 -04:00
|
|
|
end
|
2013-04-24 04:05:35 -04:00
|
|
|
|
2013-04-26 02:18:41 -04:00
|
|
|
if host != request.host && (user_id || referer)
|
2014-07-11 17:34:26 -04:00
|
|
|
cid = current_user ? (current_user.id) : (nil)
|
2013-04-26 02:18:41 -04:00
|
|
|
unless cid && cid == user_id
|
|
|
|
IncomingLink.create(url: request.url,
|
|
|
|
referer: referer,
|
|
|
|
user_id: user_id,
|
|
|
|
current_user_id: cid,
|
|
|
|
ip_address: request.remote_ip)
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-26 02:18:41 -04:00
|
|
|
|
2013-03-02 04:33:29 -05:00
|
|
|
# Internal: Extract the domain from link.
|
|
|
|
def extract_domain
|
2013-02-05 14:16:51 -05:00
|
|
|
if referer.present?
|
2014-02-26 20:03:38 -05:00
|
|
|
# We may get a junk URI, just deal with it
|
|
|
|
self.domain = URI.parse(self.referer).host rescue nil
|
2013-07-05 00:07:08 -04:00
|
|
|
self.referer = nil unless self.domain
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-03-02 04:33:29 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-02 04:33:29 -05:00
|
|
|
# Internal: If link is internal and points to topic/post, extract their IDs.
|
|
|
|
def extract_topic_and_post
|
2013-02-05 14:16:51 -05:00
|
|
|
if url.present?
|
|
|
|
parsed = URI.parse(url)
|
|
|
|
|
|
|
|
begin
|
2013-07-05 00:23:29 -04:00
|
|
|
# TODO achieve same thing with no exception
|
2013-02-05 14:16:51 -05:00
|
|
|
params = Rails.application.routes.recognize_path(parsed.path)
|
2013-11-10 18:53:01 -05:00
|
|
|
if self.topic_id = params[:topic_id]
|
|
|
|
self.post_number = params[:post_number] || 1
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
# If we can't route to the url, that's OK. Don't save those two fields.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 04:33:29 -05:00
|
|
|
# Internal: Update appropriate link counts.
|
|
|
|
def update_link_counts
|
2013-02-05 14:16:51 -05:00
|
|
|
if topic_id.present?
|
|
|
|
exec_sql("UPDATE topics
|
2013-02-07 10:45:24 -05:00
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE id = ?", topic_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
if post_number.present?
|
2013-02-07 10:45:24 -05:00
|
|
|
exec_sql("UPDATE posts
|
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE topic_id = ? and post_number = ?", topic_id, post_number)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-04-26 02:18:41 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def referer_valid
|
|
|
|
return true unless referer
|
|
|
|
if (referer.length < 3 || referer.length > 100) || (domain.length < 1 || domain.length > 100)
|
|
|
|
# internal, no need to localize
|
|
|
|
errors.add(:referer, 'referer is invalid')
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_links
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# url :string(1000) not null
|
|
|
|
# referer :string(1000)
|
|
|
|
# domain :string(100)
|
|
|
|
# topic_id :integer
|
|
|
|
# post_number :integer
|
2014-05-27 21:49:50 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-05-23 22:48:32 -04:00
|
|
|
# user_id :integer
|
2013-12-05 01:40:35 -05:00
|
|
|
# ip_address :inet
|
2013-05-23 22:48:32 -04:00
|
|
|
# current_user_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2013-08-13 16:09:27 -04:00
|
|
|
# incoming_index (topic_id,post_number)
|
|
|
|
# index_incoming_links_on_created_at_and_domain (created_at,domain)
|
|
|
|
# index_incoming_links_on_created_at_and_user_id (created_at,user_id)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|