discourse/app/models/view.rb
Dan Johnson 9f6b7889a8 views: convert 'ip' (bigint) -> 'ip_address' (inet)
This fixes all known issues when connecting to discourse over IPv6.

This table has no primary key, so the migration is done with update_all,
for each ip address in the views table. Since this table can potentially
grow quite large, this process might take a long time. I don't know any
way around this, though.

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

49 lines
1.2 KiB
Ruby

require 'ipaddr'
class View < ActiveRecord::Base
belongs_to :parent, polymorphic: true
belongs_to :user
validates_presence_of :parent_type, :parent_id, :ip_address, :viewed_at
# TODO: This could happen asyncronously
def self.create_for(parent, ip, user=nil)
# Only store a view once per day per thing per user per ip
redis_key = "view:#{parent.class.name}:#{parent.id}:#{Date.today.to_s}"
if user.present?
redis_key << ":user-#{user.id}"
else
redis_key << ":ip-#{ip}"
end
if $redis.setnx(redis_key, "1")
$redis.expire(redis_key, 1.day.to_i)
View.transaction do
View.create(parent: parent, ip_address: ip, viewed_at: Date.today, user: user)
# Update the views count in the parent, if it exists.
if parent.respond_to?(:views)
parent.class.update_all 'views = views + 1', id: parent.id
end
end
end
end
end
# == Schema Information
#
# Table name: views
#
# parent_id :integer not null
# parent_type :string(50) not null
# viewed_at :date not null
# user_id :integer
# ip_address :string not null
#
# Indexes
#
# index_views_on_parent_id_and_parent_type (parent_id,parent_type)
#