report.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # action_taken :boolean default(FALSE), not null
  10. # created_at :datetime not null
  11. # updated_at :datetime not null
  12. # account_id :bigint(8) not null
  13. # action_taken_by_account_id :bigint(8)
  14. # target_account_id :bigint(8) not null
  15. # assigned_account_id :bigint(8)
  16. # uri :string
  17. # forwarded :boolean
  18. #
  19. class Report < ApplicationRecord
  20. include Paginable
  21. include RateLimitable
  22. rate_limit by: :account, family: :reports
  23. belongs_to :account
  24. belongs_to :target_account, class_name: 'Account'
  25. belongs_to :action_taken_by_account, class_name: 'Account', optional: true
  26. belongs_to :assigned_account, class_name: 'Account', optional: true
  27. has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy
  28. scope :unresolved, -> { where(action_taken: false) }
  29. scope :resolved, -> { where(action_taken: true) }
  30. scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with({ user: [:invite_request, :invite] })) }
  31. validates :comment, length: { maximum: 1000 }
  32. def local?
  33. false # Force uri_for to use uri attribute
  34. end
  35. before_validation :set_uri, only: :create
  36. def object_type
  37. :flag
  38. end
  39. def statuses
  40. Status.with_discarded.where(id: status_ids).includes(:account, :media_attachments, :mentions)
  41. end
  42. def media_attachments
  43. MediaAttachment.where(status_id: status_ids)
  44. end
  45. def assign_to_self!(current_account)
  46. update!(assigned_account_id: current_account.id)
  47. end
  48. def unassign!
  49. update!(assigned_account_id: nil)
  50. end
  51. def resolve!(acting_account)
  52. if account_id == -99 && target_account.trust_level == Account::TRUST_LEVELS[:untrusted]
  53. # This is an automated report and it is being dismissed, so it's
  54. # a false positive, in which case update the account's trust level
  55. # to prevent further spam checks
  56. target_account.update(trust_level: Account::TRUST_LEVELS[:trusted])
  57. end
  58. RemovalWorker.push_bulk(Status.with_discarded.discarded.where(id: status_ids).pluck(:id)) { |status_id| [status_id, { immediate: true }] }
  59. update!(action_taken: true, action_taken_by_account_id: acting_account.id)
  60. end
  61. def unresolve!
  62. update!(action_taken: false, action_taken_by_account_id: nil)
  63. end
  64. def unresolved?
  65. !action_taken?
  66. end
  67. def unresolved_siblings?
  68. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  69. end
  70. def history
  71. time_range = created_at..updated_at
  72. sql = [
  73. Admin::ActionLog.where(
  74. target_type: 'Report',
  75. target_id: id,
  76. created_at: time_range
  77. ).unscope(:order),
  78. Admin::ActionLog.where(
  79. target_type: 'Account',
  80. target_id: target_account_id,
  81. created_at: time_range
  82. ).unscope(:order),
  83. Admin::ActionLog.where(
  84. target_type: 'Status',
  85. target_id: status_ids,
  86. created_at: time_range
  87. ).unscope(:order),
  88. ].map { |query| "(#{query.to_sql})" }.join(' UNION ALL ')
  89. Admin::ActionLog.from("(#{sql}) AS admin_action_logs")
  90. end
  91. def set_uri
  92. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  93. end
  94. end