Skip to content

Commit

Permalink
Slight restructure of threading model in autoflagging
Browse files Browse the repository at this point in the history
Waiting for a weekday to test this on.
  • Loading branch information
Undo1 committed May 14, 2017
1 parent 204c494 commit 6537097
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class Post < ApplicationRecord
ActionCable.server.broadcast "topbar", { review: Post.without_feedback.count }
end

after_create :autoflag
after_create do
post = self
Thread.new do
post.autoflag
end
end

def autoflag
return unless Post.where(:link => link).count == 1
Expand All @@ -33,55 +38,53 @@ def autoflag
dry_run = FlagSetting['dry_run'] == '1'
post = self

Thread.new do
begin
conditions = post.site.flag_conditions.where(:flags_enabled => true)
available_user_ids = {}
conditions.each do |condition|
if condition.validate!(post)
available_user_ids[condition.user.id] = condition
end
begin
conditions = post.site.flag_conditions.where(:flags_enabled => true)
available_user_ids = {}
conditions.each do |condition|
if condition.validate!(post)
available_user_ids[condition.user.id] = condition
end
end

uids = post.site.user_site_settings.where(:user_id => available_user_ids.keys).map(&:user_id)
users = User.where(:id => uids, :flags_enabled => true).where.not(:encrypted_api_token => nil)
unless users.present?
post.send_not_autoflagged
Thread.exit
end
uids = post.site.user_site_settings.where(:user_id => available_user_ids.keys).map(&:user_id)
users = User.where(:id => uids, :flags_enabled => true).where.not(:encrypted_api_token => nil)
unless users.present?
post.send_not_autoflagged
Thread.exit
end

post.fetch_revision_count
unless post.revision_count == 1
post.send_not_autoflagged
Thread.exit
end
post.fetch_revision_count
unless post.revision_count == 1
post.send_not_autoflagged
Thread.exit
end

max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i].min
core_count = (max_flags / 2.0).ceil
other_count = max_flags - core_count
max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i].min
core_count = (max_flags / 2.0).ceil
other_count = max_flags - core_count

users.with_role(:core).shuffle.each do |user|
if core_count <= 0
break
end
core_count -= post.send_autoflag(user, dry_run, available_user_ids[user.id])
users.with_role(:core).shuffle.each do |user|
if core_count <= 0
break
end
core_count -= post.send_autoflag(user, dry_run, available_user_ids[user.id])
end

# Go through all non-core users first; then add core users at the end. See #146
(users.without_role(:core).shuffle + users.with_role(:core).shuffle).each do |user|
if other_count <= 0
break
end
other_count -= post.send_autoflag(user, dry_run, available_user_ids[user.id])
# Go through all non-core users first; then add core users at the end. See #146
(users.without_role(:core).shuffle + users.with_role(:core).shuffle).each do |user|
if other_count <= 0
break
end
rescue => e
FlagLog.create(:success => false, :error_message => "#{e}: #{e.message} | #{e.backtrace.join("\n")}",
:is_dry_run => dry_run, :flag_condition => nil, :post => post,
:site_id => post.site_id)
other_count -= post.send_autoflag(user, dry_run, available_user_ids[user.id])
end

post.send_not_autoflagged if post.flag_logs.where(:success => true).empty?
rescue => e
FlagLog.create(:success => false, :error_message => "#{e}: #{e.message} | #{e.backtrace.join("\n")}",
:is_dry_run => dry_run, :flag_condition => nil, :post => post,
:site_id => post.site_id)
end

post.send_not_autoflagged if post.flag_logs.where(:success => true).empty?
end

def send_autoflag(user, dry_run, condition)
Expand Down

0 comments on commit 6537097

Please sign in to comment.