report.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_count
  53. statuses_to_query = []
  54. count = 0
  55. statuses.pluck(:id, :ordered_media_attachment_ids).each do |id, ordered_ids|
  56. if ordered_ids.nil?
  57. statuses_to_query << id
  58. else
  59. count += ordered_ids.size
  60. end
  61. end
  62. count += MediaAttachment.where(status_id: statuses_to_query).count unless statuses_to_query.empty?
  63. count
  64. end
  65. def rules
  66. Rule.with_discarded.where(id: rule_ids)
  67. end
  68. def assign_to_self!(current_account)
  69. update!(assigned_account_id: current_account.id)
  70. end
  71. def unassign!
  72. update!(assigned_account_id: nil)
  73. end
  74. def resolve!(acting_account)
  75. update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
  76. end
  77. def unresolve!
  78. update!(action_taken_at: nil, action_taken_by_account_id: nil)
  79. end
  80. def action_taken?
  81. action_taken_at.present?
  82. end
  83. alias action_taken action_taken?
  84. def unresolved?
  85. !action_taken?
  86. end
  87. def unresolved_siblings?
  88. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  89. end
  90. def history
  91. subquery = [
  92. Admin::ActionLog.where(
  93. target_type: 'Report',
  94. target_id: id
  95. ).unscope(:order).arel,
  96. Admin::ActionLog.where(
  97. target_type: 'Account',
  98. target_id: target_account_id
  99. ).unscope(:order).arel,
  100. Admin::ActionLog.where(
  101. target_type: 'Status',
  102. target_id: status_ids
  103. ).unscope(:order).arel,
  104. ].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
  105. Admin::ActionLog.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
  106. end
  107. def set_uri
  108. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  109. end
  110. def validate_rule_ids
  111. return unless violation?
  112. errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
  113. end
  114. end