You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: reports
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_ids :bigint(8) default([]), not null, is an Array
  8. # comment :text default(""), not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # account_id :bigint(8) not null
  12. # action_taken_by_account_id :bigint(8)
  13. # target_account_id :bigint(8) not null
  14. # assigned_account_id :bigint(8)
  15. # uri :string
  16. # forwarded :boolean
  17. # category :integer default("other"), not null
  18. # action_taken_at :datetime
  19. # rule_ids :bigint(8) is an Array
  20. #
  21. class Report < ApplicationRecord
  22. self.ignored_columns = %w(action_taken)
  23. include Paginable
  24. include RateLimitable
  25. rate_limit by: :account, family: :reports
  26. belongs_to :account
  27. belongs_to :target_account, class_name: 'Account'
  28. belongs_to :action_taken_by_account, class_name: 'Account', optional: true
  29. belongs_to :assigned_account, class_name: 'Account', optional: true
  30. has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy
  31. scope :unresolved, -> { where(action_taken_at: nil) }
  32. scope :resolved, -> { where.not(action_taken_at: nil) }
  33. scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with({ user: [:invite_request, :invite] })) }
  34. validates :comment, length: { maximum: 1_000 }
  35. validates :rule_ids, absence: true, unless: :violation?
  36. validate :validate_rule_ids
  37. enum category: {
  38. other: 0,
  39. spam: 1_000,
  40. violation: 2_000,
  41. }
  42. def local?
  43. false # Force uri_for to use uri attribute
  44. end
  45. before_validation :set_uri, only: :create
  46. def object_type
  47. :flag
  48. end
  49. def statuses
  50. Status.with_discarded.where(id: status_ids)
  51. end
  52. def media_attachments
  53. MediaAttachment.where(status_id: status_ids)
  54. end
  55. def rules
  56. Rule.with_discarded.where(id: rule_ids)
  57. end
  58. def assign_to_self!(current_account)
  59. update!(assigned_account_id: current_account.id)
  60. end
  61. def unassign!
  62. update!(assigned_account_id: nil)
  63. end
  64. def resolve!(acting_account)
  65. update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
  66. end
  67. def unresolve!
  68. update!(action_taken_at: nil, action_taken_by_account_id: nil)
  69. end
  70. def action_taken?
  71. action_taken_at.present?
  72. end
  73. alias action_taken action_taken?
  74. def unresolved?
  75. !action_taken?
  76. end
  77. def unresolved_siblings?
  78. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  79. end
  80. def history
  81. subquery = [
  82. Admin::ActionLog.where(
  83. target_type: 'Report',
  84. target_id: id
  85. ).unscope(:order).arel,
  86. Admin::ActionLog.where(
  87. target_type: 'Account',
  88. target_id: target_account_id
  89. ).unscope(:order).arel,
  90. Admin::ActionLog.where(
  91. target_type: 'Status',
  92. target_id: status_ids
  93. ).unscope(:order).arel,
  94. ].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
  95. Admin::ActionLog.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
  96. end
  97. def set_uri
  98. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  99. end
  100. def validate_rule_ids
  101. return unless violation?
  102. errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
  103. end
  104. end