2017-02-14 11:59:26 -08:00
|
|
|
# frozen_string_literal: true
|
2023-02-19 21:58:28 -08:00
|
|
|
|
2017-05-01 17:14:47 -07:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reports
|
|
|
|
#
|
2018-04-23 02:29:17 -07:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# status_ids :bigint(8) default([]), not null, is an Array
|
2017-05-01 17:14:47 -07:00
|
|
|
# comment :text default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-04-23 02:29:17 -07: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 07:34:56 -07:00
|
|
|
# uri :string
|
2020-12-14 19:30:15 -08:00
|
|
|
# forwarded :boolean
|
2022-01-17 00:41:33 -08:00
|
|
|
# category :integer default("other"), not null
|
|
|
|
# action_taken_at :datetime
|
|
|
|
# rule_ids :bigint(8) is an Array
|
2017-05-01 17:14:47 -07:00
|
|
|
#
|
2017-02-14 11:59:26 -08:00
|
|
|
|
|
|
|
class Report < ApplicationRecord
|
2023-03-31 06:07:22 -07:00
|
|
|
self.ignored_columns += %w(action_taken)
|
2022-01-17 00:41:33 -08:00
|
|
|
|
2019-06-19 17:52:34 -07:00
|
|
|
include Paginable
|
2020-04-05 05:40:08 -07:00
|
|
|
include RateLimitable
|
|
|
|
|
2024-04-24 01:56:28 -07:00
|
|
|
COMMENT_SIZE_LIMIT = 1_000
|
|
|
|
|
2020-04-05 05:40:08 -07:00
|
|
|
rate_limit by: :account, family: :reports
|
2019-06-19 17:52:34 -07:00
|
|
|
|
2017-02-14 11:59:26 -08:00
|
|
|
belongs_to :account
|
2024-01-18 04:29:41 -08:00
|
|
|
|
|
|
|
with_options class_name: 'Account' do
|
|
|
|
belongs_to :target_account
|
|
|
|
belongs_to :action_taken_by_account, optional: true
|
|
|
|
belongs_to :assigned_account, optional: true
|
|
|
|
end
|
2018-04-02 13:04:14 -07:00
|
|
|
|
2023-02-19 17:19:40 -08:00
|
|
|
has_many :notes, class_name: 'ReportNote', inverse_of: :report, dependent: :destroy
|
2022-10-26 17:10:54 -07:00
|
|
|
has_many :notifications, as: :activity, dependent: :destroy
|
2017-02-14 11:59:26 -08:00
|
|
|
|
2022-01-17 00:41:33 -08:00
|
|
|
scope :unresolved, -> { where(action_taken_at: nil) }
|
|
|
|
scope :resolved, -> { where.not(action_taken_at: nil) }
|
2024-01-23 03:41:54 -08:00
|
|
|
scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with([:account_stat, { user: [:invite_request, :invite, :ips] }])) }
|
2017-04-14 02:10:28 -07:00
|
|
|
|
2023-05-22 04:15:21 -07:00
|
|
|
# A report is considered local if the reporter is local
|
|
|
|
delegate :local?, to: :account
|
|
|
|
|
2024-04-24 01:56:28 -07:00
|
|
|
validates :comment, length: { maximum: COMMENT_SIZE_LIMIT }, if: :local?
|
2024-02-06 01:35:27 -08:00
|
|
|
validates :rule_ids, absence: true, if: -> { (category_changed? || rule_ids_changed?) && !violation? }
|
2022-02-09 15:10:16 -08:00
|
|
|
|
2024-02-06 01:35:27 -08:00
|
|
|
validate :validate_rule_ids, if: -> { (category_changed? || rule_ids_changed?) && violation? }
|
2022-01-17 00:41:33 -08:00
|
|
|
|
2023-08-16 07:38:33 -07:00
|
|
|
# entries here need to be kept in sync with the front-end:
|
|
|
|
# - app/javascript/mastodon/features/notifications/components/report.jsx
|
|
|
|
# - app/javascript/mastodon/features/report/category.jsx
|
|
|
|
# - app/javascript/mastodon/components/admin/ReportReasonSelector.jsx
|
2024-02-16 06:54:23 -08:00
|
|
|
enum :category, {
|
2022-01-17 00:41:33 -08:00
|
|
|
other: 0,
|
|
|
|
spam: 1_000,
|
2023-06-06 14:41:59 -07:00
|
|
|
legal: 1_500,
|
2022-01-17 00:41:33 -08:00
|
|
|
violation: 2_000,
|
|
|
|
}
|
2017-09-07 00:55:42 -07:00
|
|
|
|
2019-03-17 07:34:56 -07:00
|
|
|
before_validation :set_uri, only: :create
|
|
|
|
|
2023-07-24 08:37:38 -07:00
|
|
|
after_create_commit :trigger_create_webhooks
|
|
|
|
after_update_commit :trigger_update_webhooks
|
2022-06-09 12:57:36 -07:00
|
|
|
|
2018-02-27 21:54:55 -08:00
|
|
|
def object_type
|
|
|
|
:flag
|
|
|
|
end
|
|
|
|
|
2017-04-14 02:10:28 -07:00
|
|
|
def statuses
|
2022-01-17 00:41:33 -08:00
|
|
|
Status.with_discarded.where(id: status_ids)
|
2017-04-14 02:10:28 -07:00
|
|
|
end
|
2017-04-23 15:44:37 -07:00
|
|
|
|
2022-03-09 00:06:17 -08:00
|
|
|
def media_attachments_count
|
|
|
|
statuses_to_query = []
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
statuses.pluck(:id, :ordered_media_attachment_ids).each do |id, ordered_ids|
|
|
|
|
if ordered_ids.nil?
|
|
|
|
statuses_to_query << id
|
|
|
|
else
|
|
|
|
count += ordered_ids.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
count += MediaAttachment.where(status_id: statuses_to_query).count unless statuses_to_query.empty?
|
|
|
|
count
|
2017-04-23 15:44:37 -07:00
|
|
|
end
|
2018-04-10 11:27:59 -07:00
|
|
|
|
2022-01-17 00:41:33 -08:00
|
|
|
def rules
|
|
|
|
Rule.with_discarded.where(id: rule_ids)
|
|
|
|
end
|
|
|
|
|
2018-04-10 11:27:59 -07: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 00:41:33 -08:00
|
|
|
update!(action_taken_at: Time.now.utc, action_taken_by_account_id: acting_account.id)
|
2018-04-10 11:27:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def unresolve!
|
2022-01-17 00:41:33 -08:00
|
|
|
update!(action_taken_at: nil, action_taken_by_account_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_taken?
|
|
|
|
action_taken_at.present?
|
2018-04-10 11:27:59 -07:00
|
|
|
end
|
|
|
|
|
2022-01-17 00:41:33 -08:00
|
|
|
alias action_taken action_taken?
|
|
|
|
|
2018-04-10 11:27:59 -07:00
|
|
|
def unresolved?
|
|
|
|
!action_taken?
|
|
|
|
end
|
|
|
|
|
2018-09-01 15:11:58 -07:00
|
|
|
def unresolved_siblings?
|
|
|
|
Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
|
|
|
|
end
|
|
|
|
|
2022-08-25 11:39:40 -07:00
|
|
|
def to_log_human_identifier
|
|
|
|
id
|
|
|
|
end
|
|
|
|
|
2018-04-10 11:27:59 -07:00
|
|
|
def history
|
2022-01-17 00:41:33 -08:00
|
|
|
subquery = [
|
2018-04-10 11:27:59 -07:00
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Report',
|
2022-01-17 00:41:33 -08:00
|
|
|
target_id: id
|
2023-11-29 02:39:59 -08:00
|
|
|
).arel,
|
2018-04-10 11:27:59 -07:00
|
|
|
|
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Account',
|
2022-01-17 00:41:33 -08:00
|
|
|
target_id: target_account_id
|
2023-11-29 02:39:59 -08:00
|
|
|
).arel,
|
2018-04-10 11:27:59 -07:00
|
|
|
|
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'Status',
|
2022-01-17 00:41:33 -08:00
|
|
|
target_id: status_ids
|
2023-11-29 02:39:59 -08:00
|
|
|
).arel,
|
2023-10-23 05:30:46 -07:00
|
|
|
|
|
|
|
Admin::ActionLog.where(
|
|
|
|
target_type: 'AccountWarning',
|
|
|
|
target_id: AccountWarning.where(report_id: id).select(:id)
|
2023-11-29 02:39:59 -08:00
|
|
|
).arel,
|
2022-01-17 00:41:33 -08:00
|
|
|
].reduce { |union, query| Arel::Nodes::UnionAll.new(union, query) }
|
2018-04-10 11:27:59 -07:00
|
|
|
|
2023-11-29 02:39:59 -08:00
|
|
|
Admin::ActionLog.latest.from(Arel::Nodes::As.new(subquery, Admin::ActionLog.arel_table))
|
2018-04-10 11:27:59 -07:00
|
|
|
end
|
2019-03-17 07:34:56 -07:00
|
|
|
|
2022-08-25 11:39:40 -07:00
|
|
|
private
|
|
|
|
|
2019-03-17 07:34:56 -07:00
|
|
|
def set_uri
|
|
|
|
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
|
|
|
|
end
|
2022-02-09 15:10:16 -08:00
|
|
|
|
|
|
|
def validate_rule_ids
|
2022-03-07 02:21:27 -08:00
|
|
|
errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size
|
2022-02-09 15:10:16 -08:00
|
|
|
end
|
2022-06-09 12:57:36 -07:00
|
|
|
|
2023-07-24 08:37:38 -07:00
|
|
|
def trigger_create_webhooks
|
2022-06-09 12:57:36 -07:00
|
|
|
TriggerWebhookWorker.perform_async('report.created', 'Report', id)
|
|
|
|
end
|
2023-07-24 08:37:38 -07:00
|
|
|
|
|
|
|
def trigger_update_webhooks
|
|
|
|
TriggerWebhookWorker.perform_async('report.updated', 'Report', id)
|
|
|
|
end
|
2017-02-14 11:59:26 -08:00
|
|
|
end
|