Skip to content

Commit

Permalink
Fix already-invalid reports failing to resolve (#29027)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored Feb 6, 2024
1 parent 779d987 commit 66dda7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 2 additions & 4 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Report < ApplicationRecord
delegate :local?, to: :account

validates :comment, length: { maximum: 1_000 }, if: :local?
validates :rule_ids, absence: true, unless: :violation?
validates :rule_ids, absence: true, if: -> { (category_changed? || rule_ids_changed?) && !violation? }

validate :validate_rule_ids
validate :validate_rule_ids, if: -> { (category_changed? || rule_ids_changed?) && violation? }

# entries here need to be kept in sync with the front-end:
# - app/javascript/mastodon/features/notifications/components/report.jsx
Expand Down Expand Up @@ -162,8 +162,6 @@ def set_uri
end

def validate_rule_ids
return unless violation?

errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
end

Expand Down
13 changes: 13 additions & 0 deletions spec/models/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,18 @@
report = Fabricate.build(:report, account: remote_account, comment: Faker::Lorem.characters(number: 1001))
expect(report.valid?).to be true
end

it 'is invalid if it references invalid rules' do
report = Fabricate.build(:report, category: :violation, rule_ids: [-1])
expect(report.valid?).to be false
expect(report).to model_have_error_on_field(:rule_ids)
end

it 'is invalid if it references rules but category is not "violation"' do
rule = Fabricate(:rule)
report = Fabricate.build(:report, category: :spam, rule_ids: rule.id)
expect(report.valid?).to be false
expect(report).to model_have_error_on_field(:rule_ids)
end
end
end

0 comments on commit 66dda7c

Please sign in to comment.