2018-09-03 21:28:22 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class IncomingLink < ActiveRecord::Base
|
2014-08-04 01:43:57 -05:00
|
|
|
belongs_to :post
|
2013-05-01 17:12:02 -05:00
|
|
|
belongs_to :user
|
2014-08-04 01:43:57 -05:00
|
|
|
belongs_to :incoming_referer
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-04-26 01:18:41 -05:00
|
|
|
validate :referer_valid
|
2014-08-18 08:41:34 -05:00
|
|
|
validates :post_id, presence: true
|
2013-04-26 01:18:41 -05:00
|
|
|
|
2013-03-02 03:33:29 -06:00
|
|
|
after_create :update_link_counts
|
|
|
|
|
2014-08-03 20:06:06 -05:00
|
|
|
attr_accessor :url
|
|
|
|
|
|
|
|
def self.add(opts)
|
2013-04-26 01:18:41 -05:00
|
|
|
user_id, host, referer = nil
|
2014-08-03 20:06:06 -05:00
|
|
|
current_user = opts[:current_user]
|
2013-04-26 01:18:41 -05:00
|
|
|
|
2015-05-06 20:06:42 -05:00
|
|
|
username = opts[:username]
|
2018-09-03 21:28:22 -05:00
|
|
|
username = nil if !(String === username)
|
|
|
|
username = nil if username&.include?("\0")
|
2015-05-06 20:06:42 -05:00
|
|
|
if username
|
2014-08-03 20:06:06 -05:00
|
|
|
u = User.select(:id).find_by(username_lower: username.downcase)
|
2013-04-26 01:18:41 -05:00
|
|
|
user_id = u.id if u
|
|
|
|
end
|
2018-05-14 16:29:44 -05:00
|
|
|
ip_address = opts[:ip_address]
|
2013-04-24 03:05:35 -05:00
|
|
|
|
2014-08-03 20:06:06 -05:00
|
|
|
if opts[:referer].present?
|
2013-12-29 21:05:44 -06:00
|
|
|
begin
|
2014-08-03 20:06:06 -05:00
|
|
|
host = URI.parse(opts[:referer]).host
|
|
|
|
referer = opts[:referer][0..999]
|
2018-08-14 05:23:32 -05:00
|
|
|
rescue URI::Error
|
2013-12-29 21:05:44 -06:00
|
|
|
# bad uri, skip
|
|
|
|
end
|
2013-04-26 01:18:41 -05:00
|
|
|
end
|
2013-04-24 03:05:35 -05:00
|
|
|
|
2014-08-03 20:06:06 -05: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)
|
2017-07-27 20:20:09 -05:00
|
|
|
.pluck(:id).first
|
2014-08-03 20:06:06 -05:00
|
|
|
|
2014-07-11 16:34:26 -05:00
|
|
|
cid = current_user ? (current_user.id) : (nil)
|
2018-05-14 16:29:44 -05:00
|
|
|
ip_address = nil if cid
|
2014-08-03 20:06:06 -05:00
|
|
|
|
2013-04-26 01:18:41 -05:00
|
|
|
unless cid && cid == user_id
|
2014-08-03 20:06:06 -05:00
|
|
|
|
2014-08-18 08:41:34 -05:00
|
|
|
create(referer: referer,
|
|
|
|
user_id: user_id,
|
|
|
|
post_id: post_id,
|
|
|
|
current_user_id: cid,
|
2018-05-14 16:29:44 -05:00
|
|
|
ip_address: ip_address) if post_id
|
2014-08-03 20:06:06 -05:00
|
|
|
|
2013-04-24 03:05:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-08-04 01:43:57 -05:00
|
|
|
def referer=(referer)
|
|
|
|
self.incoming_referer_id = nil
|
|
|
|
|
|
|
|
# will set incoming_referer_id
|
|
|
|
unless referer.present?
|
|
|
|
return
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-08-04 01:43:57 -05:00
|
|
|
|
|
|
|
parsed = URI.parse(referer)
|
|
|
|
|
|
|
|
if parsed.scheme == "http" || parsed.scheme == "https"
|
|
|
|
domain = IncomingDomain.add!(parsed)
|
|
|
|
|
2017-12-29 12:01:53 -06:00
|
|
|
referer_record = IncomingReferer.add!(path: parsed.path, incoming_domain: domain) if domain
|
|
|
|
self.incoming_referer_id = referer_record.id if referer_record
|
2014-08-04 01:43:57 -05:00
|
|
|
end
|
|
|
|
|
2018-08-14 05:23:32 -05:00
|
|
|
rescue URI::Error
|
2014-08-04 01:43:57 -05:00
|
|
|
# ignore
|
2013-03-02 03:33:29 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-08-04 01:43:57 -05:00
|
|
|
def referer
|
|
|
|
if self.incoming_referer
|
2019-05-02 17:17:27 -05:00
|
|
|
self.incoming_referer.incoming_domain.to_url + self.incoming_referer.path
|
2014-08-04 01:43:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain
|
|
|
|
if incoming_referer
|
|
|
|
incoming_referer.incoming_domain.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:33:29 -06:00
|
|
|
# Internal: Update appropriate link counts.
|
|
|
|
def update_link_counts
|
2018-06-19 01:13:14 -05:00
|
|
|
DB.exec("UPDATE topics
|
2014-08-03 20:06:06 -05:00
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE id = (SELECT topic_id FROM posts where id = ?)", post_id)
|
2018-06-19 01:13:14 -05:00
|
|
|
DB.exec("UPDATE posts
|
2014-08-03 20:06:06 -05:00
|
|
|
SET incoming_link_count = incoming_link_count + 1
|
|
|
|
WHERE id = ?", post_id)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-04-26 01:18:41 -05: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 13:16:51 -06:00
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_links
|
|
|
|
#
|
2014-08-03 21:35:55 -05:00
|
|
|
# id :integer not null, primary key
|
2014-08-27 00:19:25 -05:00
|
|
|
# created_at :datetime not null
|
2014-08-03 21:35:55 -05:00
|
|
|
# user_id :integer
|
|
|
|
# ip_address :inet
|
|
|
|
# current_user_id :integer
|
|
|
|
# post_id :integer not null
|
|
|
|
# incoming_referer_id :integer
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2013-08-13 15:09:27 -05:00
|
|
|
# index_incoming_links_on_created_at_and_user_id (created_at,user_id)
|
2014-08-03 20:06:33 -05:00
|
|
|
# index_incoming_links_on_post_id (post_id)
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|