discourse/app/models/topic_link_click.rb
Dan Johnson 2e478d8537 TopicLinkClick: convert 'ip' (bigint) -> 'ip_address' (inet)
When accessed over IPv6, the ip address of the user is a 128-bit number,
too big for PostgreSQL's bigint data type. Since PostgresSQL has the
built-in inet type, which handles both IPv4 and IPv6 addresses, we
should use that instead. Where this is done elsewhere in the codebase,
the column is called ip_address, so we should follow that convention as
well.

This migration uses a SQL command to populate the new field from the old
one, so as not to rely on the TopicLinkClick model class, which should
keep the migration from failing if that class is modified in the future.
2013-06-25 19:41:19 -04:00

53 lines
1.5 KiB
Ruby

require_dependency 'discourse'
require 'ipaddr'
class TopicLinkClick < ActiveRecord::Base
belongs_to :topic_link, counter_cache: :clicks
belongs_to :user
validates_presence_of :topic_link_id
validates_presence_of :ip_address
# Create a click from a URL and post_id
def self.create_from(args={})
# Find the forum topic link
link = TopicLink.select(:id).where(url: args[:url])
link = link.where("user_id <> ?", args[:user_id]) if args[:user_id].present?
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
return unless link.present?
# 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)
create!(topic_link_id: link.id, user_id: args[:user_id], ip_address: args[:ip])
end
args[:url]
end
end
# == Schema Information
#
# Table name: topic_link_clicks
#
# id :integer not null, primary key
# topic_link_id :integer not null
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# ip_address :string not null
#
# Indexes
#
# index_forum_thread_link_clicks_on_forum_thread_link_id (topic_link_id)
#