about summary refs log tree commit diff
path: root/app/models/form/custom_emoji_batch.rb
blob: 54a15dc183588d257d0feddd7111e1cfd63cc4bd (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
# frozen_string_literal: true

class Form::CustomEmojiBatch
  include ActiveModel::Model
  include Authorization
  include AccountableConcern

  attr_accessor :custom_emoji_ids, :action, :current_account,
                :category_id, :category_name, :visible_in_picker

  def save
    case action
    when 'update'
      update!
    when 'list'
      list!
    when 'unlist'
      unlist!
    when 'enable'
      enable!
    when 'disable'
      disable!
    when 'copy'
      copy!
    when 'delete'
      delete!
    when 'claim'
      claim!
    when 'unclaim'
      unclaim!
    end
  end

  private

  def custom_emojis(include_all = false)
    @custom_emojis ||= (include_all || current_account&.user&.staff? ? CustomEmoji.where(id: custom_emoji_ids) : CustomEmoji.local.where(id: custom_emoji_ids, account: current_account))
  end

  def update!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

    category = begin
      if category_id.present?
        CustomEmojiCategory.find(category_id)
      elsif category_name.present?
        CustomEmojiCategory.find_or_create_by!(name: current_account&.user&.staff? ? category_name.strip : "(@#{current_account.username}) #{category_name}".rstrip)
      end
    end

    return if category.name.start_with?('(@') && !category.name.start_with?("(@#{current_account.username}) ")

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(category_id: category&.id)
      log_action :update, custom_emoji
    end
  end

  def list!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(visible_in_picker: true)
      log_action :update, custom_emoji
    end
  end

  def unlist!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(visible_in_picker: false)
      log_action :update, custom_emoji
    end
  end

  def enable!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :enable?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(disabled: false)
      log_action :enable, custom_emoji
    end
  end

  def disable!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :disable?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(disabled: true)
      log_action :disable, custom_emoji
    end
  end

  def copy!
    custom_emojis(true).each { |custom_emoji| authorize(custom_emoji, :copy?) }

    custom_emojis.each do |custom_emoji|
      copied_custom_emoji = custom_emoji.copy!(current_account)
      log_action :create, copied_custom_emoji
    end
  end

  def delete!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :destroy?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.destroy
      log_action :destroy, custom_emoji
    end
  end

  def claim!
    custom_emojis(true).each { |custom_emoji| authorize(custom_emoji, :claim?) }

    custom_emojis.each do |custom_emoji|
      if custom_emoji.local?
        custom_emoji.update(account: current_account)
        log_action :update, custom_emoji
      else
        copied_custom_emoji = custom_emoji.copy!(current_account)
        log_action :create, copied_custom_emoji
      end
    end
  end

  def unclaim!
    custom_emojis.each { |custom_emoji| authorize(custom_emoji, :unclaim?) }

    custom_emojis.each do |custom_emoji|
      custom_emoji.update(account: nil)
      log_action :update, custom_emoji
    end
  end
end