2014-08-04 05:07:55 -04:00
|
|
|
require 'ipaddr'
|
|
|
|
|
|
|
|
# awkward TopicView is taken
|
|
|
|
class TopicViewItem < ActiveRecord::Base
|
|
|
|
self.table_name = 'topic_views'
|
|
|
|
belongs_to :user
|
|
|
|
validates_presence_of :topic_id, :ip_address, :viewed_at
|
|
|
|
|
2014-08-05 02:14:10 -04:00
|
|
|
def self.add(topic_id, ip, user_id=nil, at=nil, skip_redis=false)
|
2014-08-04 05:07:55 -04:00
|
|
|
# Only store a view once per day per thing per user per ip
|
2014-08-14 14:20:52 -04:00
|
|
|
redis_key = "view:#{topic_id}:#{Date.today}"
|
2014-08-04 05:07:55 -04:00
|
|
|
if user_id
|
|
|
|
redis_key << ":user-#{user_id}"
|
|
|
|
else
|
|
|
|
redis_key << ":ip-#{ip}"
|
|
|
|
end
|
|
|
|
|
2014-08-05 02:14:10 -04:00
|
|
|
if skip_redis || $redis.setnx(redis_key, "1")
|
2015-03-04 23:25:05 -05:00
|
|
|
skip_redis || $redis.expire(redis_key, SiteSetting.topic_view_duration_hours.hours)
|
2014-08-04 05:07:55 -04:00
|
|
|
|
|
|
|
TopicViewItem.transaction do
|
|
|
|
at ||= Date.today
|
2014-08-05 02:14:10 -04:00
|
|
|
|
2014-08-06 23:14:39 -04:00
|
|
|
# this is called real frequently, working hard to avoid exceptions
|
|
|
|
sql = "INSERT INTO topic_views (topic_id, ip_address, viewed_at, user_id)
|
|
|
|
SELECT :topic_id, :ip_address, :viewed_at, :user_id
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT 1 FROM topic_views
|
|
|
|
/*where*/
|
|
|
|
)"
|
|
|
|
|
|
|
|
|
|
|
|
builder = SqlBuilder.new(sql)
|
|
|
|
|
|
|
|
if !user_id
|
2015-05-14 03:21:02 -04:00
|
|
|
builder.where("ip_address = :ip_address AND topic_id = :topic_id AND user_id IS NULL")
|
2014-08-06 23:14:39 -04:00
|
|
|
else
|
|
|
|
builder.where("user_id = :user_id AND topic_id = :topic_id")
|
|
|
|
end
|
|
|
|
|
|
|
|
result = builder.exec(topic_id: topic_id, ip_address: ip, viewed_at: at, user_id: user_id)
|
|
|
|
|
2015-03-04 23:25:05 -05:00
|
|
|
Topic.where(id: topic_id).update_all 'views = views + 1'
|
|
|
|
|
2014-08-06 23:14:39 -04:00
|
|
|
if result.cmd_tuples > 0
|
2014-08-07 00:20:42 -04:00
|
|
|
UserStat.where(user_id: user_id).update_all 'topics_entered = topics_entered + 1' if user_id
|
2014-08-06 23:14:39 -04:00
|
|
|
end
|
2014-08-04 05:07:55 -04:00
|
|
|
|
|
|
|
# Update the views count in the parent, if it exists.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_views
|
|
|
|
#
|
|
|
|
# topic_id :integer not null
|
|
|
|
# viewed_at :date not null
|
|
|
|
# user_id :integer
|
|
|
|
# ip_address :inet not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_topic_views_on_topic_id_and_viewed_at (topic_id,viewed_at)
|
|
|
|
# index_topic_views_on_viewed_at_and_topic_id (viewed_at,topic_id)
|
|
|
|
# ip_address_topic_id_topic_views (ip_address,topic_id) UNIQUE
|
|
|
|
# user_id_topic_id_topic_views (user_id,topic_id) UNIQUE
|
|
|
|
#
|