Skip to content

Commit

Permalink
Change ng rule timings
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Feb 22, 2024
1 parent 5d89371 commit 793bca2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ def process_status_params
end

def valid_status?
valid = !Admin::NgWord.reject?("#{@params[:spoiler_text]}\n#{@params[:text]}", uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?)
valid = true
valid = false if valid && !valid_status_for_ng_rule?
valid = !Admin::NgWord.reject?("#{@params[:spoiler_text]}\n#{@params[:text]}", uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?) if valid
valid = !Admin::NgWord.hashtag_reject?(@tags.size, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid
valid = !Admin::NgWord.mention_reject?(@raw_mention_uris.size, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid
valid = !Admin::NgWord.stranger_mention_reject_with_count?(@raw_mention_uris.size, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid && (mention_to_local_stranger? || reference_to_local_stranger?)
valid = !Admin::NgWord.stranger_mention_reject?("#{@params[:spoiler_text]}\n#{@params[:text]}", uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?) if valid && (mention_to_local_stranger? || reference_to_local_stranger?)
valid = false if valid && Setting.block_unfollow_account_mention && (mention_to_local_stranger? || reference_to_local_stranger?) && !local_following_sender?
valid = false if valid && !valid_status_for_ng_rule?

valid
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/activitypub/process_status_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ def valid_status?
end

def validate_status_mentions!
raise AbortError unless valid_status_for_ng_rule?
raise AbortError if (mention_to_local_stranger? || reference_to_local_stranger?) && Admin::NgWord.stranger_mention_reject?("#{@status_parser.spoiler_text}\n#{@status_parser.text}", uri: @status.uri, target_type: :status)
raise AbortError if Admin::NgWord.mention_reject?(@raw_mentions.size, uri: @status.uri, target_type: :status, text: "#{@status_parser.spoiler_text}\n#{@status_parser.text}")
raise AbortError if (mention_to_local_stranger? || reference_to_local_stranger?) && Admin::NgWord.stranger_mention_reject_with_count?(@raw_mentions.size, uri: @status.uri, target_type: :status, text: "#{@status_parser.spoiler_text}\n#{@status_parser.text}")
raise AbortError unless valid_status_for_ng_rule?
end

def valid_status_for_ng_rule?
Expand Down
2 changes: 1 addition & 1 deletion app/services/post_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ 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!
validate_status_ng_rules!
validate_status_mentions!

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

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/activitypub/activity/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ def activity_for_object(json)

context 'when rule hits' do
before do
Fabricate(:ng_rule, status_text: 'ipsum', status_action: :reject)
Fabricate(:ng_rule, status_text: 'ipsum', status_allow_follower_mention: false, status_action: :reject)
subject.perform
end

Expand All @@ -2076,7 +2076,7 @@ def activity_for_object(json)

context 'when rule does not hit' do
before do
Fabricate(:ng_rule, status_text: 'amely', status_action: :reject)
Fabricate(:ng_rule, status_text: 'amely', status_allow_follower_mention: false, status_action: :reject)
subject.perform
end

Expand Down
4 changes: 2 additions & 2 deletions spec/services/post_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@

describe 'ng rule is set' do
it 'creates a new status when no rule matches' do
Fabricate(:ng_rule, account_username: 'ohagi', status_action: :reject)
Fabricate(:ng_rule, account_username: 'ohagi', status_allow_follower_mention: false, status_action: :reject)
account = Fabricate(:account)
text = 'test status update'

Expand All @@ -833,7 +833,7 @@
end

it 'does not create a new status when a rule matches' do
Fabricate(:ng_rule, status_text: 'test', status_action: :reject)
Fabricate(:ng_rule, status_text: 'test', status_allow_follower_mention: false, status_action: :reject)
account = Fabricate(:account)
text = 'test status update'

Expand Down
4 changes: 2 additions & 2 deletions spec/services/update_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def match_update_request(req, type)

context 'when rule hits' do
before do
Fabricate(:ng_rule, status_text: 'Bar', status_action: :reject)
Fabricate(:ng_rule, status_text: 'Bar', status_allow_follower_mention: false, status_action: :reject)
end

it 'does not update text' do
Expand All @@ -419,7 +419,7 @@ def match_update_request(req, type)

context 'when rule does not hit' do
before do
Fabricate(:ng_rule, status_text: 'aar', status_action: :reject)
Fabricate(:ng_rule, status_text: 'aar', status_allow_follower_mention: false, status_action: :reject)
end

it 'does not update text' do
Expand Down

0 comments on commit 793bca2

Please sign in to comment.