about summary refs log tree commit diff
path: root/app/models/admin/status_batch_action.rb
blob: 0f019b854d0741334ba906f973326904444d8f57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# frozen_string_literal: true

class Admin::StatusBatchAction
  include ActiveModel::Model
  include AccountableConcern
  include Authorization

  attr_accessor :current_account, :type,
                :status_ids, :report_id

  attr_reader :send_email_notification

  def send_email_notification=(value)
    @send_email_notification = ActiveModel::Type::Boolean.new.cast(value)
  end

  def save!
    process_action!
  end

  private

  def statuses
    Status.with_discarded.where(id: status_ids)
  end

  def process_action!
    return if status_ids.empty?

    case type
    when 'delete'
      handle_delete!
    when 'mark_as_sensitive'
      handle_mark_as_sensitive!
    when 'report'
      handle_report!
    when 'remove_from_report'
      handle_remove_from_report!
    end
  end

  def handle_delete!
    statuses.each { |status| authorize([:admin, status], :destroy?) }

    ApplicationRecord.transaction do
      statuses.each do |status|
        status.discard_with_reblogs
        log_action(:destroy, status)
      end

      if with_report?
        report.resolve!(current_account)
        log_action(:resolve, report)
      end

      @warning = target_account.strikes.create!(
        action: :delete_statuses,
        account: current_account,
        report: report,
        status_ids: status_ids
      )

      statuses.each { |status| Tombstone.find_or_create_by(uri: status.uri, account: status.account, by_moderator: true) } unless target_account.local?
    end

    UserMailer.warning(target_account.user, @warning).deliver_later! if warnable?
    RemovalWorker.push_bulk(status_ids) { |status_id| [status_id, { 'preserve' => target_account.local?, 'immediate' => !target_account.local? }] }
  end

  def handle_mark_as_sensitive!
    representative_account = Account.representative

    # Can't use a transaction here because UpdateStatusService queues
    # Sidekiq jobs
    statuses.includes(:media_attachments, :preview_cards).find_each do |status|
      next unless status.with_media? || status.with_preview_card?

      authorize([:admin, status], :update?)

      if target_account.local?
        UpdateStatusService.new.call(status, representative_account.id, sensitive: true)
      else
        status.update(sensitive: true)
      end

      log_action(:update, status)

      if with_report?
        report.resolve!(current_account)
        log_action(:resolve, report)
      end

      @warning = target_account.strikes.create!(
        action: :mark_statuses_as_sensitive,
        account: current_account,
        report: report,
        status_ids: status_ids
      )
    end

    UserMailer.warning(target_account.user, @warning).deliver_later! if warnable?
  end

  def handle_report!
    @report = Report.new(report_params) unless with_report?
    @report.status_ids = (@report.status_ids + allowed_status_ids).uniq
    @report.save!

    @report_id = @report.id
  end

  def handle_remove_from_report!
    return unless with_report?

    report.status_ids -= status_ids.map(&:to_i)
    report.save!
  end

  def report
    @report ||= Report.find(report_id) if report_id.present?
  end

  def with_report?
    !report.nil?
  end

  def warnable?
    send_email_notification && target_account.local?
  end

  def target_account
    @target_account ||= statuses.first.account
  end

  def report_params
    { account: current_account, target_account: target_account }
  end

  def allowed_status_ids
    AccountStatusesFilter.new(@report.target_account, current_account).results.with_discarded.where(id: status_ids).pluck(:id)
  end
end