You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0 KiB

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