report.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. belongs_to :account
  20. belongs_to :target_account, class_name: 'Account'
  21. belongs_to :action_taken_by_account, class_name: 'Account', optional: true
  22. belongs_to :assigned_account, class_name: 'Account', optional: true
  23. has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy
  24. scope :unresolved, -> { where(action_taken: false) }
  25. scope :resolved, -> { where(action_taken: true) }
  26. validates :comment, length: { maximum: 1000 }
  27. def local?
  28. false # Force uri_for to use uri attribute
  29. end
  30. before_validation :set_uri, only: :create
  31. def object_type
  32. :flag
  33. end
  34. def statuses
  35. Status.where(id: status_ids).includes(:account, :media_attachments, :mentions)
  36. end
  37. def media_attachments
  38. MediaAttachment.where(status_id: status_ids)
  39. end
  40. def assign_to_self!(current_account)
  41. update!(assigned_account_id: current_account.id)
  42. end
  43. def unassign!
  44. update!(assigned_account_id: nil)
  45. end
  46. def resolve!(acting_account)
  47. update!(action_taken: true, action_taken_by_account_id: acting_account.id)
  48. end
  49. def unresolve!
  50. update!(action_taken: false, action_taken_by_account_id: nil)
  51. end
  52. def unresolved?
  53. !action_taken?
  54. end
  55. def unresolved_siblings?
  56. Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
  57. end
  58. def history
  59. time_range = created_at..updated_at
  60. sql = [
  61. Admin::ActionLog.where(
  62. target_type: 'Report',
  63. target_id: id,
  64. created_at: time_range
  65. ).unscope(:order),
  66. Admin::ActionLog.where(
  67. target_type: 'Account',
  68. target_id: target_account_id,
  69. created_at: time_range
  70. ).unscope(:order),
  71. Admin::ActionLog.where(
  72. target_type: 'Status',
  73. target_id: status_ids,
  74. created_at: time_range
  75. ).unscope(:order),
  76. ].map { |query| "(#{query.to_sql})" }.join(' UNION ALL ')
  77. Admin::ActionLog.from("(#{sql}) AS admin_action_logs")
  78. end
  79. def set_uri
  80. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
  81. end
  82. end