report.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. 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. validates :comment, length: { maximum: 1_000 }
  36. validates :rule_ids, absence: true, unless: :violation?
  37. validate :validate_rule_ids
  38. enum category: {
  39. other: 0,
  40. spam: 1_000,
  41. violation: 2_000,
  42. }
  43. def local?
  44. false # Force uri_for to use uri attribute
  45. end
  46. before_validation :set_uri, only: :create
  47. after_create_commit :trigger_webhooks
  48. def object_type
  49. :flag
  50. end
  51. def statuses
  52. Status.with_discarded.where(id: status_ids)
  53. end
  54. def media_attachments_count
  55. statuses_to_query = []
  56. count = 0
  57. statuses.pluck(:id, :ordered_media_attachment_ids).each do |id, ordered_ids|
  58. if ordered_ids.nil?
  59. statuses_to_query << id
  60. else
  61. count += ordered_ids.size
  62. end
  63. end
  64. count += MediaAttachment.where(status_id: statuses_to_query).count unless statuses_to_query.empty?
  65. count
  66. end
  67. def rules
  68. Rule.with_discarded.where(id: rule_ids)
  69. end
  70. def assign_to_self!(current_account)
  71. update!(assigned_account_id: current_account.id)
  72. end
  73. def unassign!
  74. update!(assigned_account_id: nil)
  75. end
  76. def resolve!(acting_account)
  77. update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
  78. end
  79. def unresolve!
  80. update!(action_taken_at: nil, action_taken_by_account_id: nil)
  81. end
  82. def action_taken?
  83. action_taken_at.present?
  84. end
  85. alias action_taken action_taken?
  86. def unresolved?
  87. !action_taken?
  88. end
  89. def unresolved_siblings?
  90. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  91. end
  92. def to_log_human_identifier
  93. id
  94. end
  95. def history
  96. subquery = [
  97. Admin::ActionLog.where(
  98. target_type: 'Report',
  99. target_id: id
  100. ).unscope(:order).arel,
  101. Admin::ActionLog.where(
  102. target_type: 'Account',
  103. target_id: target_account_id
  104. ).unscope(:order).arel,
  105. Admin::ActionLog.where(
  106. target_type: 'Status',
  107. target_id: status_ids
  108. ).unscope(:order).arel,
  109. ].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
  110. Admin::ActionLog.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
  111. end
  112. private
  113. def set_uri
  114. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  115. end
  116. def validate_rule_ids
  117. return unless violation?
  118. errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
  119. end
  120. def trigger_webhooks
  121. TriggerWebhookWorker.perform_async('report.created', 'Report', id)
  122. end
  123. end