report.rb 4.1 KB

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