0
0
Fork 0

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.
This commit is contained in:
Emelia Smith 2018-04-23 23:52:58 +02:00 committed by Eugen Rochko
parent 495303d9b8
commit 60b871d56c
4 changed files with 27 additions and 0 deletions

View file

@ -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