]> cat aescling's git repositories - mastodon.git/blob - app/models/report.rb
Update Mastodon to Rails 6.1 (#15910)
[mastodon.git] / app / models / report.rb
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 # forwarded :boolean
18 #
19
20 class Report < ApplicationRecord
21 include Paginable
22 include RateLimitable
23
24 rate_limit by: :account, family: :reports
25
26 belongs_to :account
27 belongs_to :target_account, class_name: 'Account'
28 belongs_to :action_taken_by_account, class_name: 'Account', optional: true
29 belongs_to :assigned_account, class_name: 'Account', optional: true
30
31 has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy
32
33 scope :unresolved, -> { where(action_taken: false) }
34 scope :resolved, -> { where(action_taken: true) }
35 scope :with_accounts, -> { includes([:account, :target_account, :action_taken_by_account, :assigned_account].index_with({ user: [:invite_request, :invite] })) }
36
37 validates :comment, length: { maximum: 1000 }
38
39 def local?
40 false # Force uri_for to use uri attribute
41 end
42
43 before_validation :set_uri, only: :create
44
45 def object_type
46 :flag
47 end
48
49 def statuses
50 Status.with_discarded.where(id: status_ids).includes(:account, :media_attachments, :mentions)
51 end
52
53 def media_attachments
54 MediaAttachment.where(status_id: status_ids)
55 end
56
57 def assign_to_self!(current_account)
58 update!(assigned_account_id: current_account.id)
59 end
60
61 def unassign!
62 update!(assigned_account_id: nil)
63 end
64
65 def resolve!(acting_account)
66 if account_id == -99 && target_account.trust_level == Account::TRUST_LEVELS[:untrusted]
67 # This is an automated report and it is being dismissed, so it's
68 # a false positive, in which case update the account's trust level
69 # to prevent further spam checks
70
71 target_account.update(trust_level: Account::TRUST_LEVELS[:trusted])
72 end
73
74 RemovalWorker.push_bulk(Status.with_discarded.discarded.where(id: status_ids).pluck(:id)) { |status_id| [status_id, { immediate: true }] }
75 update!(action_taken: true, action_taken_by_account_id: acting_account.id)
76 end
77
78 def unresolve!
79 update!(action_taken: false, action_taken_by_account_id: nil)
80 end
81
82 def unresolved?
83 !action_taken?
84 end
85
86 def unresolved_siblings?
87 Report.where.not(id: id).where(target_account_id: target_account_id).unresolved.exists?
88 end
89
90 def history
91 time_range = created_at..updated_at
92
93 sql = [
94 Admin::ActionLog.where(
95 target_type: 'Report',
96 target_id: id,
97 created_at: time_range
98 ).unscope(:order),
99
100 Admin::ActionLog.where(
101 target_type: 'Account',
102 target_id: target_account_id,
103 created_at: time_range
104 ).unscope(:order),
105
106 Admin::ActionLog.where(
107 target_type: 'Status',
108 target_id: status_ids,
109 created_at: time_range
110 ).unscope(:order),
111 ].map { |query| "(#{query.to_sql})" }.join(' UNION ALL ')
112
113 Admin::ActionLog.from("(#{sql}) AS admin_action_logs")
114 end
115
116 def set_uri
117 self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
118 end
119 end
This page took 0.106915 seconds and 4 git commands to generate.