[deploy] Auto-suspend multiple-spammers (#10679)

* Auto-ban multiple-spammers

* Fix xit
This commit is contained in:
Ben Halpern 2020-10-06 18:07:48 -04:00 committed by GitHub
parent f87e9eace1
commit c77a13c0d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 0 deletions

View file

@ -681,12 +681,24 @@ class Article < ApplicationRecord
def create_conditional_autovomits
return unless SiteConfig.spam_trigger_terms.any? { |term| title.downcase.include?(term.downcase) }
self.score = -25
Reaction.create(
user_id: SiteConfig.mascot_user_id,
reactable_id: id,
reactable_type: "Article",
category: "vomit",
)
return unless Reaction.article_vomits.where(reactable_id: user.articles.pluck(:id)).size > 2
user.add_role(:banned)
Note.create(
author_id: SiteConfig.mascot_user_id,
noteable_id: user_id,
noteable_type: "User",
reason: "automatic_ban",
content: "User banned for too many spammy articles, triggered by autovomit.",
)
end
def async_bust

View file

@ -257,6 +257,17 @@ class Comment < ApplicationRecord
reactable_type: "Comment",
category: "vomit",
)
return unless Reaction.comment_vomits.where(reactable_id: user.comments.pluck(:id)).size > 2
user.add_role(:banned)
Note.create(
author_id: SiteConfig.mascot_user_id,
noteable_id: user_id,
noteable_type: "User",
reason: "automatic_ban",
content: "User banned for too many spammy articles, triggered by autovomit.",
)
end
def should_send_email_notification?

View file

@ -23,6 +23,8 @@ class Reaction < ApplicationRecord
scope :readinglist, -> { where(category: "readinglist") }
scope :for_articles, ->(ids) { where(reactable_type: "Article", reactable_id: ids) }
scope :eager_load_serialized_data, -> { includes(:reactable, :user) }
scope :article_vomits, -> { where(category: "vomit", reactable_type: "Article") }
scope :comment_vomits, -> { where(category: "vomit", reactable_type: "Comment") }
validates :category, inclusion: { in: CATEGORIES }
validates :reactable_type, inclusion: { in: REACTABLE_TYPES }

View file

@ -789,6 +789,26 @@ RSpec.describe Article, type: :model do
expect(Reaction.last.user_id).to eq(user.id)
end
it "does not ban user if only single vomit" do
article.body_markdown = article.body_markdown.gsub(article.title, "This post is about Yahoomagoo gogo")
article.save
expect(article.user.banned).to be false
end
it "bans user with 3 comment vomits" do
second_article = create(:article, user: article.user)
third_article = create(:article, user: article.user)
article.body_markdown = article.body_markdown.gsub(article.title, "This post is about Yahoomagoo gogo")
second_article.body_markdown = second_article.body_markdown.gsub(second_article.title, "testtestetest")
third_article.body_markdown = third_article.body_markdown.gsub(third_article.title, "yahoomagoo gogo")
article.save
second_article.save
third_article.save
expect(article.user.banned).to be true
expect(Note.last.reason).to eq "automatic_ban"
end
it "does not create vomit reaction if does not have matching title" do
article.save
expect(Reaction.last).to be nil

View file

@ -410,6 +410,24 @@ RSpec.describe Comment, type: :model do
expect(Reaction.last.user_id).to eq(user.id)
end
it "does not ban user if only single vomit" do
comment.body_markdown = "This post is about Yahoomagoo gogo"
comment.save
expect(comment.user.banned).to be false
end
it "bans user with 3 comment vomits" do
comment.body_markdown = "This post is about Yahoomagoo gogo"
second_comment = create(:comment, user: comment.user, body_markdown: "This post is about Yahoomagoo gogo")
third_comment = create(:comment, user: comment.user, body_markdown: "This post is about Yahoomagoo gogo")
comment.save
second_comment.save
third_comment.save
expect(comment.user.banned).to be true
expect(Note.last.reason).to eq "automatic_ban"
end
it "does not create vomit reaction if user is established in this context" do
user.update_column(:registered_at, 10.days.ago)
comment.body_markdown = "This post is about Yahoomagoo gogo"