Skip to content

Commit

Permalink
#150 ローカルユーザーの投稿にもメンションのNGワードを適用
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Oct 19, 2023
1 parent ceda0fd commit 4a26233
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/form/admin_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Form::AdminSettings
captcha_enabled
ng_words
ng_words_for_stranger_mention
stranger_mention_from_local_ng
hide_local_users_for_anonymous
post_hash_tags_max
sensitive_words
Expand Down Expand Up @@ -79,6 +80,7 @@ class Form::AdminSettings
check_lts_version_only
enable_public_unlisted_visibility
unlocked_friend
stranger_mention_from_local_ng
).freeze

UPLOAD_KEYS = %i(
Expand Down
10 changes: 10 additions & 0 deletions app/services/post_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def process_status!
@status = @account.statuses.new(status_attributes)
process_mentions_service.call(@status, limited_type: @status.limited_visibility? ? @limited_scope : '', circle: @circle, save_records: false)
safeguard_mentions!(@status)
validate_status_mentions!

@status.limited_scope = :personal if @status.limited_visibility? && !process_mentions_service.mentions?

Expand Down Expand Up @@ -208,6 +209,15 @@ def validate_status!
raise Mastodon::ValidationError, I18n.t('statuses.too_many_hashtags') if Admin::NgWord.hashtag_reject_with_extractor?(@options[:text])
end

def validate_status_mentions!
raise Mastodon::ValidationError, I18n.t('statuses.contains_ng_words') if mention_to_stranger? && Setting.stranger_mention_from_local_ng && Admin::NgWord.stranger_mention_reject?("#{@options[:spoiler_text]}\n#{@options[:text]}")
end

def mention_to_stranger?
@status.mentions.map(&:account).to_a.any? { |mentioned_account| mentioned_account.id != @account && !mentioned_account.following?(@account) } ||
(@in_reply_to && @in_reply_to.account.id != @account.id && !@in_reply_to.account.following?(@account))
end

def validate_media!
if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
@media = []
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/ng_words/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
.fields-group
= f.input :ng_words_for_stranger_mention, wrapper: :with_label, as: :text, input_html: { rows: 12 }, label: t('admin.ng_words.keywords_for_stranger_mention'), hint: t('admin.ng_words.keywords_for_stranger_mention_hint')

.fields-group
= f.input :stranger_mention_from_local_ng, wrapper: :with_label, as: :boolean, label: t('admin.ng_words.stranger_mention_from_local_ng'), hint: t('admin.ng_words.stranger_mention_from_local_ng_hint')

.fields-group
= f.input :ng_words, wrapper: :with_label, as: :text, input_html: { rows: 12 }, label: t('admin.ng_words.keywords'), hint: t('admin.ng_words.keywords_hint')

Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ en:
keywords_for_stranger_mention_hint: Currently this words are checked posts from other servers only.
keywords_hint: The first character of the line is "?". to use regular expressions
post_hash_tags_max: Hash tags max for posts
stranger_mention_from_local_ng: フォローしていないアカウントへのメンションのNGワードを、ローカルユーザーによる投稿にも適用する
stranger_mention_from_local_ng_hint: サーバーの登録が承認制でない場合、あなたのサーバーにもスパムが入り込む可能性があります
test_error: Testing is returned any errors
title: NG words and against spams
relationships:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ ja:
keywords_for_stranger_mention_hint: フォローしていないアカウントへのメンションにのみ適用されます。現状は外部サーバーから来た投稿のみに適用されます
keywords_hint: 行を「?」で始めると、正規表現が使えます
post_hash_tags_max: 投稿に設定可能なハッシュタグの最大数
stranger_mention_from_local_ng: フォローしていないアカウントへのメンションのNGワードを、ローカルユーザーによる投稿にも適用する
stranger_mention_from_local_ng_hint: サーバーの登録が承認制でない場合、あなたのサーバーにもスパムが入り込む可能性があります
test_error: NGワードのテストに失敗しました。正規表現のミスが含まれているかもしれません
title: NGワードとスパム
relationships:
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defaults: &defaults
check_lts_version_only: true
enable_public_unlisted_visibility: true
unlocked_friend: false
stranger_mention_from_local_ng: true

development:
<<: *defaults
Expand Down
76 changes: 76 additions & 0 deletions spec/services/post_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,82 @@
expect(status2.id).to eq status1.id
end

describe 'ng word is set' do
it 'hit ng words' do
account = Fabricate(:account)
text = 'ng word test'
Form::AdminSettings.new(ng_words: 'test').save

expect { subject.call(account, text: text) }.to raise_error(Mastodon::ValidationError)
end

it 'not hit ng words' do
account = Fabricate(:account)
text = 'ng word aiueo'
Form::AdminSettings.new(ng_words: 'test').save

status = subject.call(account, text: text)

expect(status).to be_persisted
expect(status.text).to eq text
end

it 'hit ng words for mention' do
account = Fabricate(:account)
mentioned = Fabricate(:account, username: 'ohagi', domain: nil)
text = 'ng word test @ohagi'
Form::AdminSettings.new(ng_words_for_stranger_mention: 'test', stranger_mention_from_local_ng: '1').save

expect { subject.call(account, text: text) }.to raise_error(Mastodon::ValidationError)
end

it 'hit ng words for mention but local posts are not checked' do
account = Fabricate(:account)
mentioned = Fabricate(:account, username: 'ohagi', domain: nil)
text = 'ng word test @ohagi'
Form::AdminSettings.new(ng_words_for_stranger_mention: 'test', stranger_mention_from_local_ng: '0').save

status = subject.call(account, text: text)

expect(status).to be_persisted
expect(status.text).to eq text
end

it 'hit ng words for mention to follower' do
account = Fabricate(:account)
mentioned = Fabricate(:account, username: 'ohagi', domain: nil)
mentioned.follow!(account)
text = 'ng word test @ohagi'
Form::AdminSettings.new(ng_words_for_stranger_mention: 'test', stranger_mention_from_local_ng: '1').save

status = subject.call(account, text: text)

expect(status).to be_persisted
expect(status.text).to eq text
end

it 'hit ng words for reply' do
account = Fabricate(:account)
text = 'ng word test'
Form::AdminSettings.new(ng_words_for_stranger_mention: 'test', stranger_mention_from_local_ng: '1').save

expect { subject.call(account, text: text, thread: Fabricate(:status)) }.to raise_error(Mastodon::ValidationError)
end

it 'hit ng words for reply to follower' do
account = Fabricate(:account)
mentioned = Fabricate(:account, username: 'ohagi', domain: nil)
mentioned.follow!(account)
text = 'ng word test'
Form::AdminSettings.new(ng_words_for_stranger_mention: 'test', stranger_mention_from_local_ng: '1').save

status = subject.call(account, text: text)

expect(status).to be_persisted
expect(status.text).to eq text
end
end

def create_status_with_options(**options)
subject.call(Fabricate(:account), options.merge(text: 'test'))
end
Expand Down

0 comments on commit 4a26233

Please sign in to comment.