report.rb 3.6 KB

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