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,14 +23,15 @@ class Jobs::CreateUserReviewable < Jobs::Base
email: user.email
}
)
return if @reviewable.score > 0
@reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:needs_approval],
reason: reason,
force_review: true
)
if @reviewable.created_new
@reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:needs_approval],
reason: reason,
force_review: true
)
end
end
end
end

View File

@ -15,6 +15,7 @@ class Reviewable < ActiveRecord::Base
end
end
attr_accessor :created_new
validates_presence_of :type, :status, :created_by_id
belongs_to :target, polymorphic: true
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)
category_id = topic.category_id if topic.present?
create!(
reviewable = create!(
target: target,
target_created_by_id: target_created_by_id,
topic: topic,
@ -95,6 +96,9 @@ class Reviewable < ActiveRecord::Base
payload: payload,
potential_spam: potential_spam
)
reviewable.created_new = true
reviewable
rescue ActiveRecord::RecordNotUnique
row_count = DB.exec(<<~SQL, status: statuses[:pending], id: target.id, type: target.class.name)