FIX: don't allow storage of post timings batch larger than 60 secs

This commit is contained in:
Sam 2015-06-19 10:35:55 +10:00
parent 8feaa5c613
commit 5eabf01c29
2 changed files with 14 additions and 5 deletions

View File

@ -68,21 +68,27 @@ class PostTiming < ActiveRecord::Base
end
end
MAX_READ_TIME_PER_BATCH = 60*1000.0
def self.process_timings(current_user, topic_id, topic_time, timings)
current_user.user_stat.update_time_read!
account_age_msecs = ((Time.now - current_user.created_at) * 1000.0)
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
highest_seen = 1
join_table = []
timings = timings.find_all do |post_number, time|
post_number >= 0 && time < account_age_msecs
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
end
timings.each_with_index do |(post_number, time), index|
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"
@ -122,6 +128,7 @@ SQL
total_changed = Notification.mark_posts_read(current_user, topic_id, timings.map{|t| t[0]})
end
topic_time = max_time_per_post if topic_time > max_time_per_post
TopicUser.update_last_read(current_user, topic_id, highest_seen, topic_time)
if total_changed > 0

View File

@ -71,7 +71,7 @@ describe PostTiming do
PostTiming.process_timings(user, post.topic_id, 1, [[post.post_number, 10.minutes.to_i * 1000]])
msecs = PostTiming.where(post_number: post.post_number, user_id: user.id).pluck(:msecs)[0]
expect(msecs).to eq(123)
expect(msecs).to eq(123 + PostTiming::MAX_READ_TIME_PER_BATCH)
end
end
@ -85,7 +85,7 @@ describe PostTiming do
ActiveRecord::Base.observers.enable :all
post = Fabricate(:post)
user2 = Fabricate(:coding_horror)
user2 = Fabricate(:coding_horror, created_at: 1.day.ago)
PostAction.act(user2, post, PostActionType.types[:like])
@ -96,6 +96,8 @@ describe PostTiming do
post.user.reload
expect(post.user.unread_notifications).to eq(0)
PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 1.day]])
end
end