report.rb 2.6 KB

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