2017-02-14 19:59:26 +00:00
|
|
|
# frozen_string_literal: true
|
2017-05-02 00:14:47 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reports
|
|
|
|
#
|
2018-04-23 09:29:17 +00:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# status_ids :bigint(8) default([]), not null, is an Array
|
2017-05-02 00:14:47 +00:00
|
|
|
# comment :text default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-04-23 09:29:17 +00:00
|
|
|
# account_id :bigint(8) not null
|
|
|
|
# action_taken_by_account_id :bigint(8)
|
|
|
|
# target_account_id :bigint(8) not null
|
|
|
|
# assigned_account_id :bigint(8)
|
2019-03-17 14:34:56 +00:00
|
|
|
# uri :string
|
2020-12-15 03:30:15 +00:00
|
|
|
# forwarded :boolean
|
2022-01-17 08:41:33 +00:00
|
|
|
# category :integer default("other"), not null
|
|
|
|
# action_taken_at :datetime
|
|
|
|
# rule_ids :bigint(8) is an Array
|
2017-05-02 00:14:47 +00:00
|
|
|
#
|
2017-02-14 19:59:26 +00:00
|
|
|
|
|
|
|
class Report < ApplicationRecord
|
2022-01-17 08:41:33 +00:00
|
|
|
self.ignored_columns = %w(action_taken)
|
|
|
|
|
2019-06-20 00:52:34 +00:00
|
|
|
include Paginable
|
2020-04-05 12:40:08 +00:00
|
|
|
include RateLimitable
|
|
|
|
|
|
|
|
rate_limit by: :account, family: :reports
|
2019-06-20 00:52:34 +00:00
|
|
|
|
2017-02-14 19:59:26 +00:00
|
|
|
belongs_to :account
|
|
|
|
belongs_to :target_account, class_name: 'Account'
|
2018-01-19 19:56:47 +00:00
|
|
|
belongs_to :action_taken_by_account, class_name: 'Account', optional: true
|
2018-04-02 20:04:14 +00:00
|
|
|
belongs_to :assigned_account, class_name: 'Account', optional: true
|
|
|
|
|
|
|
|
has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy
|
2017-02-14 19:59:26 +00:00
|
|
|
|
2022-01-17 08:41:33 +00:00
|
|
|
scope :unresolved, -> { where(action_taken_at: nil) }
|
|
|
|
scope :resolved, -> { where.not(action_taken_at: nil) }
|
2021-03-24 09:44:31 +00:00
|
|
|
scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with({ user: [:invite_request, :invite] })) }
|
2017-04-14 09:10:28 +00:00
|
|
|
|
2022-01-17 08:41:33 +00:00
|
|
|
validates :comment, length: { maximum: 1_000 }
|
2022-02-09 23:10:16 +00:00
|
|
|
validates :rule_ids, absence: true, unless: :violation?
|
|
|
|
|
|
|
|
validate :validate_rule_ids
|
2022-01-17 08:41:33 +00:00
|
|
|
|
|
|
|
enum category: {
|
|
|
|
other: 0,
|
|
|
|
spam: 1_000,
|
|
|
|
violation: 2_000,
|
|
|
|
}
|
2017-09-07 07:55:42 +00:00
|
|
|
|
2019-03-17 14:34:56 +00:00
|
|
|
def local?
|
|
|
|
false # Force uri_for to use uri attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
before_validation :set_uri, only: :create
|
|
|
|
|
2018-02-28 05:54:55 +00:00
|
|
|
def object_type
|
|
|
|
:flag
|
|
|
|
end
|
|
|
|
|
2017-04-14 09:10:28 +00:00
|
|
|
def statuses
|
2022-01-17 08:41:33 +00:00
|
|
|
Status.with_discarded.where(id: status_ids)
|
2017-04-14 09:10:28 +00:00
|
|
|
end
|
2017-04-23 22:44:37 +00:00
|
|
|
|
|
|
|
def media_attachments
|
2017-04-30 16:15:49 +00:00
|
|
|
MediaAttachment.where(status_id: status_ids)
|
2017-04-23 22:44:37 +00:00
|
|
|
end
|
2018-04-10 18:27:59 +00:00
|
|
|
|
2022-01-17 08:41:33 +00:00
|
|
|
def rules
|
|
|
|
Rule.with_discarded.where(id: rule_ids)
|
|
|
|
end
|
|
|
|
|
2018-04-10 18:27:59 +00:00
|
|
|
def assign_to_self!(current_account)
|
|
|
|
update!(assigned_account_id: current_account.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unassign!
|
|
|
|
update!(assigned_account_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve!(acting_account)
|
2022-01-17 08:41:33 +00:00
|
|
|
update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
|
2018-04-10 18:27:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unresolve!
|
2022-01-17 08:41:33 +00:00
|
|
|
update!(action_taken_at: nil, action_taken_by_account_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_taken?
|
|
|
|
action_taken_at.present?
|
2018-04-10 18:27:59 +00:00
|
|
|
end
|
|
|
|
|
2022-01-17 08:41:33 +00:00
|
|
|
alias action_taken action_taken?
|
|
|
|
|
2018-04-10 18:27:59 +00:00
|
|
|
def unresolved?
|
|
|
|
!action_taken?
|
|
|
|
end
|
|
|
|
|
2018-09-01 22:11:58 +00:00
|
|
|
def unresolved_siblings?
|
|
|
|
Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
|
|
|
|
end
|
|
|
|
|
2018-04-10 18:27:59 +00:00
|
|
|
def history
|
2022-01-17 08:41:33 +00:00
|
|
|
subquery = [
|
2018-04-10 18:27:59 +00:00
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Report',
|
2022-01-17 08:41:33 +00:00
|
|
|
target_id: id
|
|
|
|
).unscope(:order).arel,
|
2018-04-10 18:27:59 +00:00
|
|
|
|
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Account',
|
2022-01-17 08:41:33 +00:00
|
|
|
target_id: target_account_id
|
|
|
|
).unscope(:order).arel,
|
2018-04-10 18:27:59 +00:00
|
|
|
|
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Status',
|
2022-01-17 08:41:33 +00:00
|
|
|
target_id: status_ids
|
|
|
|
).unscope(:order).arel,
|
|
|
|
].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
|
2018-04-10 18:27:59 +00:00
|
|
|
|
2022-01-17 08:41:33 +00:00
|
|
|
Admin::ActionLog.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
|
2018-04-10 18:27:59 +00:00
|
|
|
end
|
2019-03-17 14:34:56 +00:00
|
|
|
|
|
|
|
def set_uri
|
|
|
|
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
|
|
|
|
end
|
2022-02-09 23:10:16 +00:00
|
|
|
|
|
|
|
def validate_rule_ids
|
|
|
|
return unless violation?
|
|
|
|
|
2022-03-07 10:21:27 +00:00
|
|
|
errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
|
2022-02-09 23:10:16 +00:00
|
|
|
end
|
2017-02-14 19:59:26 +00:00
|
|
|
end
|