1
0

report.rb 5.0 KB

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