summaryrefslogtreecommitdiffstats
path: root/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb
blob: c080e77ecfd09ac3667352a1b9a74bb1360186da (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# frozen_string_literal: true

class BackfillAdminActionLogsAgain < ActiveRecord::Migration[6.1]
  disable_ddl_transaction!

  class Account < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    has_one :user, inverse_of: :account

    def local?
      domain.nil?
    end

    def acct
      local? ? username : "#{username}@#{domain}"
    end
  end

  class User < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account
  end

  class Status < ApplicationRecord
    include RoutingHelper

    # Dummy class, to make migration possible across version changes
    belongs_to :account

    def local?
      attributes['local'] || attributes['uri'].nil?
    end

    def uri
      local? ? activity_account_status_url(account, self) : attributes['uri']
    end
  end

  class DomainBlock < ApplicationRecord; end
  class DomainAllow < ApplicationRecord; end
  class EmailDomainBlock < ApplicationRecord; end
  class UnavailableDomain < ApplicationRecord; end

  class AccountWarning < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account
  end

  class Announcement < ApplicationRecord; end
  class IpBlock < ApplicationRecord; end
  class CustomEmoji < ApplicationRecord; end
  class CanonicalEmailBlock < ApplicationRecord; end

  class Appeal < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account
  end

  class AdminActionLog < ApplicationRecord
    # Dummy class, to make migration possible across version changes

    # Cannot use usual polymorphic support because of namespacing issues
    belongs_to :status, foreign_key: :target_id
    belongs_to :account, foreign_key: :target_id
    belongs_to :user
    belongs_to :domain_block, foreign_key: :target_id
    belongs_to :domain_allow, foreign_key: :target_id
    belongs_to :email_domain_block, foreign_key: :target_id
    belongs_to :unavailable_domain, foreign_key: :target_id
    belongs_to :account_warning, foreign_key: :target_id
    belongs_to :announcement, foreign_key: :target_id
    belongs_to :ip_block, foreign_key: :target_id
    belongs_to :custom_emoji, foreign_key: :target_id
    belongs_to :canonical_email_block, foreign_key: :target_id
    belongs_to :appeal, foreign_key: :target_id
  end

  def up
    safety_assured do
      process_logs_for_account
      process_logs_for_user
      process_logs_for_report
      process_logs_for_domain_block
      process_logs_for_domain_allow
      process_logs_for_email_domain_block
      process_logs_for_unavailable_domain
      process_logs_for_status
      process_logs_for_account_warning
      process_logs_for_announcement
      process_logs_for_ip_block
      process_logs_for_custom_emoji
      process_logs_for_canonical_email_block
      process_logs_for_appeal
    end
  end

  def down; end

  private

  def process_logs_for_account
    AdminActionLog.includes(:account).where(target_type: 'Account', human_identifier: nil).find_each do |log|
      next if log.account.nil?

      log.update_attribute('human_identifier', log.account.acct)
    end
  end

  def process_logs_for_user
    AdminActionLog.includes(user: :account).where(target_type: 'User', human_identifier: nil).find_each do |log|
      next if log.user.nil?

      log.update_attribute('human_identifier', log.user.account.acct)
      log.update_attribute('route_param', log.user.account_id)
    end
  end

  def process_logs_for_report
    AdminActionLog.where(target_type: 'Report', human_identifier: nil).in_batches.update_all('human_identifier = target_id::text')
  end

  def process_logs_for_domain_block
    AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log|
      next if log.domain_block.nil?

      log.update_attribute('human_identifier', log.domain_block.domain)
    end
  end

  def process_logs_for_domain_allow
    AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log|
      next if log.domain_allow.nil?

      log.update_attribute('human_identifier', log.domain_allow.domain)
    end
  end

  def process_logs_for_email_domain_block
    AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log|
      next if log.email_domain_block.nil?

      log.update_attribute('human_identifier', log.email_domain_block.domain)
    end
  end

  def process_logs_for_unavailable_domain
    AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log|
      next if log.unavailable_domain.nil?

      log.update_attribute('human_identifier', log.unavailable_domain.domain)
    end
  end

  def process_logs_for_status
    AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log|
      next if log.status.nil?

      log.update_attribute('human_identifier', log.status.account.acct)
      log.update_attribute('permalink', log.status.uri)
    end
  end

  def process_logs_for_account_warning
    AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log|
      next if log.account_warning.nil?

      log.update_attribute('human_identifier', log.account_warning.account.acct)
    end
  end

  def process_logs_for_announcement
    AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log|
      next if log.announcement.nil?

      log.update_attribute('human_identifier', log.announcement.text)
    end
  end

  def process_logs_for_ip_block
    AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log|
      next if log.ip_block.nil?

      log.update_attribute('human_identifier', "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}")
    end
  end

  def process_logs_for_custom_emoji
    AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log|
      next if log.custom_emoji.nil?

      log.update_attribute('human_identifier', log.custom_emoji.shortcode)
    end
  end

  def process_logs_for_canonical_email_block
    AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log|
      next if log.canonical_email_block.nil?

      log.update_attribute('human_identifier', log.canonical_email_block.canonical_email_hash)
    end
  end

  def process_logs_for_appeal
    AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal',