report.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. RemovalWorker.push_bulk(Status.with_discarded.discarded.where(id: status_ids).pluck(:id)) { |status_id| [status_id, { immediate: true }] }
  50. update!(action_taken: true, action_taken_by_account_id: acting_account.id)
  51. end
  52. def unresolve!
  53. update!(action_taken: false, action_taken_by_account_id: nil)
  54. end
  55. def unresolved?
  56. !action_taken?
  57. end
  58. def unresolved_siblings?
  59. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  60. end
  61. def history
  62. time_range = created_at..updated_at
  63. sql = [
  64. Admin::ActionLog.where(
  65. target_type: 'Report',
  66. target_id: id,
  67. created_at: time_range
  68. ).unscope(:order),
  69. Admin::ActionLog.where(
  70. target_type: 'Account',
  71. target_id: target_account_id,
  72. created_at: time_range
  73. ).unscope(:order),
  74. Admin::ActionLog.where(
  75. target_type: 'Status',
  76. target_id: status_ids,
  77. created_at: time_range
  78. ).unscope(:order),
  79. ].map { |query| "(#{query.to_sql})" }.join(' UNION ALL ')
  80. Admin::ActionLog.from("(#{sql}) AS admin_action_logs")
  81. end
  82. def set_uri
  83. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  84. end
  85. end