report.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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', inverse_of: :report, dependent: :destroy
  31. has_many :notifications, as: :activity, dependent: :destroy
  32. scope :unresolved, -> { where(action_taken_at: nil) }
  33. scope :resolved, -> { where.not(action_taken_at: nil) }
  34. scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with({ user: [:invite_request, :invite] })) }
  35. # A report is considered local if the reporter is local
  36. delegate :local?, to: :account
  37. validates :comment, length: { maximum: 1_000 }, if: :local?
  38. validates :rule_ids, absence: true, unless: :violation?
  39. validate :validate_rule_ids
  40. # entries here need to be kept in sync with the front-end:
  41. # - app/javascript/mastodon/features/notifications/components/report.jsx
  42. # - app/javascript/mastodon/features/report/category.jsx
  43. # - app/javascript/mastodon/components/admin/ReportReasonSelector.jsx
  44. enum category: {
  45. other: 0,
  46. spam: 1_000,
  47. legal: 1_500,
  48. violation: 2_000,
  49. }
  50. before_validation :set_uri, only: :create
  51. after_create_commit :trigger_create_webhooks
  52. after_update_commit :trigger_update_webhooks
  53. def object_type
  54. :flag
  55. end
  56. def statuses
  57. Status.with_discarded.where(id: status_ids)
  58. end
  59. def media_attachments_count
  60. statuses_to_query = []
  61. count = 0
  62. statuses.pluck(:id, :ordered_media_attachment_ids).each do |id, ordered_ids|
  63. if ordered_ids.nil?
  64. statuses_to_query << id
  65. else
  66. count += ordered_ids.size
  67. end
  68. end
  69. count += MediaAttachment.where(status_id: statuses_to_query).count unless statuses_to_query.empty?
  70. count
  71. end
  72. def rules
  73. Rule.with_discarded.where(id: rule_ids)
  74. end
  75. def assign_to_self!(current_account)
  76. update!(assigned_account_id: current_account.id)
  77. end
  78. def unassign!
  79. update!(assigned_account_id: nil)
  80. end
  81. def resolve!(acting_account)
  82. update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
  83. end
  84. def unresolve!
  85. update!(action_taken_at: nil, action_taken_by_account_id: nil)
  86. end
  87. def action_taken?
  88. action_taken_at.present?
  89. end
  90. alias action_taken action_taken?
  91. def unresolved?
  92. !action_taken?
  93. end
  94. def unresolved_siblings?
  95. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  96. end
  97. def to_log_human_identifier
  98. id
  99. end
  100. def history
  101. subquery = [
  102. Admin::ActionLog.where(
  103. target_type: 'Report',
  104. target_id: id
  105. ).arel,
  106. Admin::ActionLog.where(
  107. target_type: 'Account',
  108. target_id: target_account_id
  109. ).arel,
  110. Admin::ActionLog.where(
  111. target_type: 'Status',
  112. target_id: status_ids
  113. ).arel,
  114. Admin::ActionLog.where(
  115. target_type: 'AccountWarning',
  116. target_id: AccountWarning.where(report_id: id).select(:id)
  117. ).arel,
  118. ].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
  119. Admin::ActionLog.latest.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
  120. end
  121. private
  122. def set_uri
  123. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  124. end
  125. def validate_rule_ids
  126. return unless violation?
  127. errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
  128. end
  129. def trigger_create_webhooks
  130. TriggerWebhookWorker.perform_async('report.created', 'Report', id)
  131. end
  132. def trigger_update_webhooks
  133. TriggerWebhookWorker.perform_async('report.updated', 'Report', id)
  134. end
  135. end