docbrown/app/models/settings/rate_limit.rb
Jeremy Friesen 91b951eb05
Refactoring and extending article spam behavior (#15399)
* Refactoring and extending article spam behavior

Prior to this refactor, we were checking an Article's title for
spaminess.  This change now checks the Article's title and
body_markdown.

In addition, the encapsulates the regular expression implementations in
favor of asking the Settings::RateLimit class to determine if the passed
in text is "spammy".

Furthermore, instead of having lots of inline logic within the article,
this refactor introduces a `Spam::ArticleHandler` class.  This helps
tidy up the already busy Article specs by delegating the business logic
of Spam handling to it's own class.

There are further refactors for Comments and Users that would follow
this pattern, but this change is more important to get out the door.

Closes #15396

* Adding comments to clarify behavior of spam handling

* Adding missing expectation to spec
2021-11-17 17:09:17 -05:00

38 lines
1.6 KiB
Ruby

module Settings
class RateLimit < Base
self.table_name = :settings_rate_limits
setting :article_update, type: :integer, default: 30
setting :comment_antispam_creation, type: :integer, default: 1
# Explicitly defaults to 7 to accommodate DEV Top 7 Posts
setting :mention_creation, type: :integer, default: 7
setting :comment_creation, type: :integer, default: 9
setting :email_recipient, type: :integer, default: 5
setting :feedback_message_creation, type: :integer, default: 5
setting :follow_count_daily, type: :integer, default: 500
setting :image_upload, type: :integer, default: 9
setting :listing_creation, type: :integer, default: 1
setting :organization_creation, type: :integer, default: 1
setting :published_article_antispam_creation, type: :integer, default: 1
setting :published_article_creation, type: :integer, default: 9
setting :reaction_creation, type: :integer, default: 10
setting :send_email_confirmation, type: :integer, default: 2
setting :spam_trigger_terms, type: :array, default: []
setting :user_considered_new_days, type: :integer, default: 3
setting :user_subscription_creation, type: :integer, default: 3
setting :user_update, type: :integer, default: 15
# A helper function to determine if text is spammy.
#
# @param text [String] text to check for "spamminess"
#
# @return [TrueClass] if this is spammy
# @return [FalseClass] if this isn't spammy
def self.trigger_spam_for?(text:)
return false if spam_trigger_terms.empty?
regexp = Regexp.new("(#{spam_trigger_terms.join('|')})", true)
regexp.match?(text)
end
end
end