2013-02-06 03:16:51 +08:00
|
|
|
class IncomingLink < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
2013-05-02 06:12:02 +08:00
|
|
|
belongs_to :user
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-26 14:18:41 +08:00
|
|
|
validate :referer_valid
|
2014-08-04 09:06:06 +08:00
|
|
|
validate :post_id, presence: true
|
2013-04-26 14:18:41 +08:00
|
|
|
|
2013-03-02 17:33:29 +08:00
|
|
|
before_validation :extract_domain
|
|
|
|
after_create :update_link_counts
|
|
|
|
|
2014-08-04 09:06:06 +08:00
|
|
|
attr_accessor :url
|
|
|
|
|
|
|
|
def self.add(opts)
|
2013-04-26 14:18:41 +08:00
|
|
|
user_id, host, referer = nil
|
2014-08-04 09:06:06 +08:00
|
|
|
current_user = opts[:current_user]
|
2013-04-26 14:18:41 +08:00
|
|
|
|
2014-08-04 09:06:06 +08:00
|
|
|
if username = opts[:username]
|
|
|
|
u = User.select(:id).find_by(username_lower: username.downcase)
|
2013-04-26 14:18:41 +08:00
|
|
|
user_id = u.id if u
|
|
|
|
end
|
2013-04-24 16:05:35 +08:00
|
|
|
|
2014-08-04 09:06:06 +08:00
|
|
|
if opts[:referer].present?
|
2013-12-30 11:05:44 +08:00
|
|
|
begin
|
2014-08-04 09:06:06 +08:00
|
|
|
host = URI.parse(opts[:referer]).host
|
|
|
|
referer = opts[:referer][0..999]
|
2013-12-30 11:05:44 +08:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# bad uri, skip
|
|
|
|
end
|
2013-04-26 14:18:41 +08:00
|
|
|
end
|
2013-04-24 16:05:35 +08:00
|
|
|
|
2014-08-04 09:06:06 +08:00
|
|
|
if host != opts[:host] && (user_id || referer)
|
|
|
|
|
|
|
|
post_id = opts[:post_id]
|
|
|
|
post_id ||= Post.where(topic_id: opts[:topic_id],
|
|
|
|
post_number: opts[:post_number] || 1)
|
|
|
|
.pluck(:id).first
|
|
|
|
|
2014-07-12 05:34:26 +08:00
|
|
|
cid = current_user ? (current_user.id) : (nil)
|
2014-08-04 09:06:06 +08:00
|
|
|
|
|
|
|
|
2013-04-26 14:18:41 +08:00
|
|
|
unless cid && cid == user_id
|
2014-08-04 09:06:06 +08:00
|
|
|
|
|
|
|
IncomingLink.create(referer: referer,
|
2013-04-26 14:18:41 +08:00
|
|
|
user_id: user_id,
|
2014-08-04 09:06:06 +08:00
|
|
|
post_id: post_id,
|
2013-04-26 14:18:41 +08:00
|
|
|
current_user_id: cid,
|
2014-08-04 09:06:06 +08:00
|
|
|
ip_address: opts[:ip_address]) if post_id
|
|
|
|
|
2013-04-24 16:05:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-26 14:18:41 +08:00
|
|
|
|
2013-03-02 17:33:29 +08:00
|
|
|
# Internal: Extract the domain from link.
|
|
|
|
def extract_domain
|
2013-02-06 03:16:51 +08:00
|
|
|
if referer.present?
|
2014-02-27 09:03:38 +08:00
|
|
|
# We may get a junk URI, just deal with it
|
|
|
|
self.domain = URI.parse(self.referer).host rescue nil
|
2013-07-05 12:07:08 +08:00
|
|
|
self.referer = nil unless self.domain
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-03-02 17:33:29 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-02 17:33:29 +08:00
|
|
|
# Internal: Update appropriate link counts.
|
|
|
|
def update_link_counts
|
2014-08-04 09:06:06 +08:00
|
|
|
exec_sql("UPDATE topics
|
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE id = (SELECT topic_id FROM posts where id = ?)", post_id)
|
|
|
|
exec_sql("UPDATE posts
|
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE id = ?", post_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-04-26 14:18:41 +08: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-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_links
|
|
|
|
#
|
2014-08-04 10:35:55 +08:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# topic_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# user_id :integer
|
|
|
|
# ip_address :inet
|
|
|
|
# current_user_id :integer
|
|
|
|
# post_id :integer not null
|
|
|
|
# incoming_referer_id :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2013-08-14 04:09:27 +08:00
|
|
|
# index_incoming_links_on_created_at_and_user_id (created_at,user_id)
|
2014-08-04 09:06:33 +08:00
|
|
|
# index_incoming_links_on_post_id (post_id)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|