diff --git a/app/controllers/admin/ng_words_controller.rb b/app/controllers/admin/ng_words_controller.rb index 1af50acb42..e26ca96b8c 100644 --- a/app/controllers/admin/ng_words_controller.rb +++ b/app/controllers/admin/ng_words_controller.rb @@ -11,18 +11,31 @@ module Admin def create authorize :ng_words, :create? + begin + test_words + rescue + flash[:alert] = I18n.t('admin.ng_words.test_error') + redirect_to after_update_redirect_path + return + end + @admin_settings = Form::AdminSettings.new(settings_params) if @admin_settings.save flash[:notice] = I18n.t('generic.changes_saved_msg') redirect_to after_update_redirect_path else - render :index + render :show end end private + def test_words + ng_words = settings_params['ng_words'].split(/\r\n|\r|\n/) + Admin::NgWord.reject_with_custom_words?('Sample text', ng_words) + end + def after_update_redirect_path admin_ng_words_path end diff --git a/app/models/admin/ng_word.rb b/app/models/admin/ng_word.rb index 42c84b0f8e..b99c52a714 100644 --- a/app/models/admin/ng_word.rb +++ b/app/models/admin/ng_word.rb @@ -3,7 +3,11 @@ class Admin::NgWord class << self def reject?(text) - ng_words.any? { |word| text.include?(word) } + ng_words.any? { |word| include?(text, word) } + end + + def reject_with_custom_words?(text, custom_ng_words) + custom_ng_words.any? { |word| include?(text, word) } end def hashtag_reject?(hashtag_count) @@ -16,6 +20,14 @@ class Admin::NgWord private + def include?(text, word) + if word.start_with?('?') && word.size >= 2 + text =~ /#{word[1..]}/i + else + text.include?(word) + end + end + def ng_words Setting.ng_words || [] end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5ee09dadbf..e89c236b0f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -600,6 +600,7 @@ en: hide_local_users_for_anonymous: Hide timeline local user posts from anonymous keywords: Reject keywords post_hash_tags_max: Hash tags max for posts + test_error: Testing is returned any errors title: NG words and against spams relationships: title: "%{acct}'s relationships" diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 1690c7f3dd..d27e475cb6 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -599,6 +599,7 @@ ja: hide_local_users_for_anonymous: ログインしていない状態でローカルユーザーの投稿をタイムラインから取得できないようにする keywords: 投稿できないキーワード post_hash_tags_max: 投稿に設定可能なハッシュタグの最大数 + test_error: NGワードのテストに失敗しました。正規表現のミスが含まれているかもしれません title: NGワードとスパム relationships: title: "%{acct} さんのフォロー・フォロワー"