FIX: Safer scoring with concurrency

This commit is contained in:
Robin Ward 2019-04-23 15:53:37 -04:00
parent 70097966ed
commit fca3f53e9c
2 changed files with 13 additions and 8 deletions

View File

@ -23,8 +23,8 @@ class Jobs::CreateUserReviewable < Jobs::Base
email: user.email email: user.email
} }
) )
return if @reviewable.score > 0
if @reviewable.created_new
@reviewable.add_score( @reviewable.add_score(
Discourse.system_user, Discourse.system_user,
ReviewableScore.types[:needs_approval], ReviewableScore.types[:needs_approval],
@ -34,3 +34,4 @@ class Jobs::CreateUserReviewable < Jobs::Base
end end
end end
end end
end

View File

@ -15,6 +15,7 @@ class Reviewable < ActiveRecord::Base
end end
end end
attr_accessor :created_new
validates_presence_of :type, :status, :created_by_id validates_presence_of :type, :status, :created_by_id
belongs_to :target, polymorphic: true belongs_to :target, polymorphic: true
belongs_to :created_by, class_name: 'User' belongs_to :created_by, class_name: 'User'
@ -85,7 +86,7 @@ class Reviewable < ActiveRecord::Base
topic = target.topic if topic.blank? && target.is_a?(Post) topic = target.topic if topic.blank? && target.is_a?(Post)
category_id = topic.category_id if topic.present? category_id = topic.category_id if topic.present?
create!( reviewable = create!(
target: target, target: target,
target_created_by_id: target_created_by_id, target_created_by_id: target_created_by_id,
topic: topic, topic: topic,
@ -95,6 +96,9 @@ class Reviewable < ActiveRecord::Base
payload: payload, payload: payload,
potential_spam: potential_spam potential_spam: potential_spam
) )
reviewable.created_new = true
reviewable
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
row_count = DB.exec(<<~SQL, status: statuses[:pending], id: target.id, type: target.class.name) row_count = DB.exec(<<~SQL, status: statuses[:pending], id: target.id, type: target.class.name)