Class: CleanUpSpammyUsersJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/clean_up_spammy_users_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(created_after: 1.month.ago) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/jobs/clean_up_spammy_users_job.rb', line 4

def perform(created_after: 1.month.ago)
  # Select potential spammers: users created within timeframe, who are not deleted, who have posted but all posts have
  # since been deleted (no live posts).
  possible_spammers = User.joins('inner join posts on users.id = posts.user_id')
                          .where('users.created_at >= ?', created_after)
                          .where(users: { deleted: false }).group('users.id').having('count(posts.id) > 0')
                          .having('count(distinct if(posts.deleted = true, null, posts.id)) = 0')
  possible_spammers.each do |spammer|
    all_posts_spam = spammer.posts.all? do |post|
      # A post is considered spam if there are any helpful spam flags on it.
      post.flags.any? { |flag| flag.post_flag_type.name == "it's spam" && flag.status == 'helpful' }
    end
    if all_posts_spam
      spammer.block('automatic block from spam cleanup job', length: 2.years)
      spammer.do_soft_delete(User.find(-1))
    end
  end
end