discourse/app/models/post_timing.rb

65 lines
2.1 KiB
Ruby
Raw Normal View History

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
# 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)
if rows == 0
Post.update_all 'reads = reads + 1', ['topic_id = :topic_id and post_number = :post_number', args]
exec_sql("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
SELECT :topic_id, :user_id, :post_number, :msecs
2013-02-07 10:45:24 -05:00
WHERE NOT EXISTS(SELECT 1 FROM post_timings
2013-02-05 14:16:51 -05:00
WHERE topic_id = :topic_id
AND user_id = :user_id
AND post_number = :post_number)",
args)
end
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
def self.destroy_for(user_id, topic_id)
PostTiming.transaction do
PostTiming.delete_all(['user_id = ? and topic_id = ?', user_id, topic_id])
TopicUser.delete_all(['user_id = ? and topic_id = ?', user_id, topic_id])
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
end
def self.process_timings(current_user, topic_id, highest_seen, topic_time, timings)
current_user.update_time_read!
2013-02-25 11:42:20 -05:00
timings.each do |post_number, time|
if post_number >= 0
PostTiming.record_timing(topic_id: topic_id,
post_number: post_number,
user_id: current_user.id,
msecs: time)
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
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