about summary refs log tree commit diff
path: root/app/models/trends/status_batch.rb
blob: 78d93bed44524ba01dc43b24276837b49e151652 (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
# frozen_string_literal: true

class Trends::StatusBatch
  include ActiveModel::Model
  include Authorization

  attr_accessor :status_ids, :action, :current_account

  def save
    case action
    when 'approve'
      approve!
    when 'approve_accounts'
      approve_accounts!
    when 'reject'
      reject!
    when 'reject_accounts'
      reject_accounts!
    end
  end

  private

  def statuses
    @statuses ||= Status.where(id: status_ids)
  end

  def status_accounts
    @status_accounts ||= Account.where(id: statuses.map(&:account_id).uniq)
  end

  def approve!
    statuses.each { |status| authorize(status, :review?) }
    statuses.update_all(trendable: true)
  end

  def approve_accounts!
    status_accounts.each do |account|
      authorize(account, :review?)
      account.update(trendable: true, reviewed_at: action_time)
    end

    # Reset any individual overrides
    statuses.update_all(trendable: nil)
  end

  def reject!
    statuses.each { |status| authorize(status, :review?) }
    statuses.update_all(trendable: false)
  end

  def reject_accounts!
    status_accounts.each do |account|
      authorize(account, :review?)
      account.update(trendable: false, reviewed_at: action_time)
    end

    # Reset any individual overrides
    statuses.update_all(trendable: nil)
  end

  def action_time
    @action_time ||= Time.now.utc
  end
end