mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FIX: avoid exception in path that is very exception prone
This commit is contained in:
parent
ae2d80501a
commit
022109bc76
1 changed files with 22 additions and 5 deletions
|
@ -21,15 +21,32 @@ class TopicViewItem < ActiveRecord::Base
|
|||
TopicViewItem.transaction do
|
||||
at ||= Date.today
|
||||
|
||||
# AR likes logging failures here, we don't need that
|
||||
TopicViewItem.create!(topic_id: topic_id, ip_address: ip, viewed_at: at, user_id: user_id)
|
||||
# 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
|
||||
builder.where("ip_address = :ip_address AND topic_id = :topic_id")
|
||||
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)
|
||||
|
||||
if result.cmd_tuples > 0
|
||||
Topic.where(id: topic_id).update_all 'views = views + 1'
|
||||
end
|
||||
|
||||
# Update the views count in the parent, if it exists.
|
||||
Topic.where(id: topic_id).update_all 'views = views + 1'
|
||||
end
|
||||
end
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
# don't care, skip
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue