report.rb 3.5 KB

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