about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEmelia Smith <ThisIsMissEm@users.noreply.github.com>2018-04-23 23:52:58 +0200
committerEugen Rochko <eugen@zeonfederated.com>2018-04-23 23:52:58 +0200
commit60b871d56c554ddb22b7890d803e4ba6650ed286 (patch)
tree7011103a98c5961650cc748f513e80e89350ce9b /app
parent495303d9b86919c72bf1948a714bf8d00b41fa0f (diff)
Implement the ability for instances to define a list of disallowed hashtags (#7176)
The goal here isn't to prevent these hashtags from existing, but just to strongly curtail their usage; The hashtags may still exist in the database via federated status, or from being created prior to this feature.
Diffstat (limited to 'app')
-rw-r--r--app/models/status.rb1
-rw-r--r--app/validators/disallowed_hashtags_validator.rb22
2 files changed, 23 insertions, 0 deletions
diff --git a/app/models/status.rb b/app/models/status.rb
index ed4bcefca..37f2db562 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -59,6 +59,7 @@ class Status < ApplicationRecord
   validates :uri, uniqueness: true, presence: true, unless: :local?
   validates :text, presence: true, unless: -> { with_media? || reblog? }
   validates_with StatusLengthValidator
+  validates_with DisallowedHashtagsValidator
   validates :reblog, uniqueness: { scope: :account }, if: :reblog?
 
   default_scope { recent }
diff --git a/app/validators/disallowed_hashtags_validator.rb b/app/validators/disallowed_hashtags_validator.rb
new file mode 100644
index 000000000..22c027b0f
--- /dev/null
+++ b/app/validators/disallowed_hashtags_validator.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class DisallowedHashtagsValidator < ActiveModel::Validator
+  def validate(status)
+    return unless status.local? && !status.reblog?
+
+    tags = Extractor.extract_hashtags(status.text)
+    tags.keep_if { |tag| disallowed_hashtags.include? tag.downcase }
+
+    status.errors.add(:text, I18n.t('statuses.disallowed_hashtags', tags: tags.join(', '), count: tags.size)) unless tags.empty?
+  end
+
+  private
+
+  def disallowed_hashtags
+    return @disallowed_hashtags if @disallowed_hashtags
+
+    @disallowed_hashtags = Setting.disallowed_hashtags.nil? ? [] : Setting.disallowed_hashtags
+    @disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
+    @disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
+  end
+end