2013-02-05 14:16:51 -05:00
|
|
|
class PostTiming < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates_presence_of :post_number
|
|
|
|
validates_presence_of :msecs
|
|
|
|
|
|
|
|
|
2013-07-03 21:47:12 -04:00
|
|
|
def self.pretend_read(topic_id, actual_read_post_number, pretend_read_post_number)
|
|
|
|
# This is done in SQL cause the logic is quite tricky and we want to do this in one db hit
|
|
|
|
#
|
|
|
|
exec_sql("INSERT INTO post_timings(topic_id, user_id, post_number, msecs)
|
|
|
|
SELECT :topic_id, user_id, :pretend_read_post_number, 1
|
|
|
|
FROM post_timings pt
|
|
|
|
WHERE topic_id = :topic_id AND
|
|
|
|
post_number = :actual_read_post_number AND
|
|
|
|
NOT EXISTS (
|
|
|
|
SELECT 1 FROM post_timings pt1
|
|
|
|
WHERE pt1.topic_id = pt.topic_id AND
|
|
|
|
pt1.post_number = :pretend_read_post_number AND
|
|
|
|
pt1.user_id = pt.user_id
|
|
|
|
)
|
|
|
|
",
|
|
|
|
pretend_read_post_number: pretend_read_post_number,
|
|
|
|
topic_id: topic_id,
|
|
|
|
actual_read_post_number: actual_read_post_number
|
|
|
|
)
|
|
|
|
|
|
|
|
TopicUser.ensure_consistency!(topic_id)
|
|
|
|
end
|
|
|
|
|
2015-06-18 03:02:10 -04:00
|
|
|
def self.record_new_timing(args)
|
|
|
|
begin
|
|
|
|
exec_sql("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
|
|
|
|
SELECT :topic_id, :user_id, :post_number, :msecs
|
|
|
|
WHERE NOT EXISTS(SELECT 1 FROM post_timings
|
|
|
|
WHERE topic_id = :topic_id
|
|
|
|
AND user_id = :user_id
|
|
|
|
AND post_number = :post_number)",
|
|
|
|
args)
|
|
|
|
rescue PG::UniqueViolation
|
|
|
|
# concurrency is hard, we are not running serialized so this can possibly
|
|
|
|
# still happen, if it happens we just don't care, its an invalid record anyway
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Post.where(['topic_id = :topic_id and post_number = :post_number', args]).update_all 'reads = reads + 1'
|
|
|
|
UserStat.where(user_id: args[:user_id]).update_all 'posts_read_count = posts_read_count + 1'
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Increases a timer if a row exists, otherwise create it
|
|
|
|
def self.record_timing(args)
|
|
|
|
rows = exec_sql_row_count("UPDATE post_timings
|
|
|
|
SET msecs = msecs + :msecs
|
|
|
|
WHERE topic_id = :topic_id
|
|
|
|
AND user_id = :user_id
|
|
|
|
AND post_number = :post_number",
|
|
|
|
args)
|
|
|
|
|
2015-06-18 03:02:10 -04:00
|
|
|
record_new_timing(args) if rows == 0
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
|
2014-02-21 13:03:50 -05:00
|
|
|
def self.destroy_for(user_id, topic_ids)
|
2013-02-05 14:16:51 -05:00
|
|
|
PostTiming.transaction do
|
2014-02-21 13:03:50 -05:00
|
|
|
PostTiming.delete_all(['user_id = ? and topic_id in (?)', user_id, topic_ids])
|
|
|
|
TopicUser.delete_all(['user_id = ? and topic_id in (?)', user_id, topic_ids])
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-06-18 20:35:55 -04:00
|
|
|
MAX_READ_TIME_PER_BATCH = 60*1000.0
|
2013-02-25 02:42:42 -05:00
|
|
|
|
2013-04-07 21:12:52 -04:00
|
|
|
def self.process_timings(current_user, topic_id, topic_time, timings)
|
2013-10-03 23:28:49 -04:00
|
|
|
current_user.user_stat.update_time_read!
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2015-06-18 20:35:55 -04:00
|
|
|
max_time_per_post = ((Time.now - current_user.created_at) * 1000.0)
|
|
|
|
max_time_per_post = MAX_READ_TIME_PER_BATCH if max_time_per_post > MAX_READ_TIME_PER_BATCH
|
2015-04-14 11:49:44 -04:00
|
|
|
|
2013-04-07 21:12:52 -04:00
|
|
|
highest_seen = 1
|
2015-06-18 03:02:10 -04:00
|
|
|
|
|
|
|
join_table = []
|
|
|
|
|
2015-06-18 20:35:55 -04:00
|
|
|
i = timings.length
|
|
|
|
while i > 0
|
|
|
|
i -= 1
|
|
|
|
timings[i][1] = max_time_per_post if timings[i][1] > max_time_per_post
|
|
|
|
timings.delete_at(i) if timings[i][0] < 1
|
2015-06-18 03:02:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
timings.each_with_index do |(post_number, time), index|
|
2015-06-18 20:35:55 -04:00
|
|
|
|
2015-06-18 03:02:10 -04:00
|
|
|
join_table << "SELECT #{topic_id.to_i} topic_id, #{post_number.to_i} post_number,
|
|
|
|
#{current_user.id.to_i} user_id, #{time.to_i} msecs, #{index} idx"
|
|
|
|
|
2013-04-07 21:12:52 -04:00
|
|
|
|
|
|
|
highest_seen = post_number.to_i > highest_seen ?
|
|
|
|
post_number.to_i : highest_seen
|
2015-06-18 03:02:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if join_table.length > 0
|
|
|
|
sql = <<SQL
|
|
|
|
|
|
|
|
UPDATE post_timings t
|
|
|
|
SET msecs = t.msecs + x.msecs
|
|
|
|
FROM (#{join_table.join(" UNION ALL ")}) x
|
|
|
|
WHERE x.topic_id = t.topic_id AND
|
|
|
|
x.post_number = t.post_number AND
|
|
|
|
x.user_id = t.user_id
|
|
|
|
RETURNING x.idx
|
|
|
|
SQL
|
|
|
|
|
|
|
|
result = exec_sql(sql)
|
|
|
|
result.type_map = SqlBuilder.pg_type_map
|
|
|
|
existing = Set.new(result.column_values(0))
|
|
|
|
|
|
|
|
timings.each_with_index do |(post_number, time),index|
|
|
|
|
unless existing.include?(index)
|
|
|
|
PostTiming.record_new_timing(topic_id: topic_id,
|
|
|
|
post_number: post_number,
|
|
|
|
user_id: current_user.id,
|
|
|
|
msecs: time)
|
|
|
|
end
|
2013-02-25 02:42:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
total_changed = 0
|
|
|
|
if timings.length > 0
|
|
|
|
total_changed = Notification.mark_posts_read(current_user, topic_id, timings.map{|t| t[0]})
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2015-06-18 20:35:55 -04:00
|
|
|
topic_time = max_time_per_post if topic_time > max_time_per_post
|
2013-02-25 02:42:42 -05:00
|
|
|
TopicUser.update_last_read(current_user, topic_id, highest_seen, topic_time)
|
|
|
|
|
|
|
|
if total_changed > 0
|
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_timings
|
|
|
|
#
|
|
|
|
# topic_id :integer not null
|
|
|
|
# post_number :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# msecs :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-08-03 22:35:55 -04:00
|
|
|
# index_post_timings_on_user_id (user_id)
|
|
|
|
# post_timings_summary (topic_id,post_number)
|
|
|
|
# post_timings_unique (topic_id,post_number,user_id) UNIQUE
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|