Spam: Retroactively get AI to review recent posts and then delete the ones marked as spam

,

I helped out a site that got hit about about 30 spam posts per hour. We turned on AI Spam, but that didn’t solve the problem of the existing posts. Here’s what I did. This might work for you. It’s not tested on your site, so if you do any of these things you should make a backup and also make sure that it looks like this wasn’t written by an AI that is trying to take down your site.

This assumes that you know what rails is and how to run it. If you don’t, you probably shouldn’t be doing this.

First, you should put your site in Read Only Modes in Discourse so that people don’t create stuff while you make some terrible mistake. Then make a backup. That will mean that until you turn off read-only mode, you can safely return to your backup.

Get Recent posts

The attack started just over a day ago, so I went back two days. You could also do stuff like 36.hours.ago if you want to be more explicit. If I were more clever, I might have done something to skip posts in PMs, but I decided not to worry about that.

posts = Post.where("created_at >= ?", 2.days.ago)
          .where(deleted_at: nil)
          .where("user_id > ?", 1)

Call the SpamScanner on those posts

I’m not quite sure what happened to make some of those fail, but if you get a few “oops” it probably isn’t cause for concern

posts.each do |post|
  begin
    puts  "https://talk.commonmark.org/p/#{post.id}"
    DiscourseAi::AiModeration::SpamScanner.perform_scan(post)
  rescue
    puts "oops";
  end;
  sleep 0.2;
end

Get the posts that now need to be reviewed

This gets reviewable posts that were just marked as reviewable (presumably by the above actions). We don’t want to blindly apply this to stuff that’s not part of this dangerous adventure.

reviewables = Reviewable.where(status: "pending")
  .where(potential_spam: true)
  .where("created_at >= ?", 1.hour.ago)
  .where("updated_at >= ?", 1.hour.ago)

Blindly Trust the AI

Of course you carefully looked through the topics that got hidden and they all look like they were really spam, so you’re going to just go for it. The heading is a joke.You’re not really blindly trusting the AI, are you?!?!?

This is going to delete the posts and delete and block the users. It’s really, really dangerous.


reviewables.each do |reviewable|
  begin
    puts "deleting https://talk.commonmark.org/t/#{reviewable.topic_id}"
    if reviewable.topic.nil?
      puts "already gone"
      next
    end
    reviewable.perform(user, :delete_user_block)
  rescue
    puts "oops"
  end
end

8 Likes