about summary refs log tree commit diff
path: root/db/migrate/20230215074424_move_glitch_user_settings.rb
blob: 76fafdd761fd5417de47fd0ec4767dc587ab6834 (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
# frozen_string_literal: true

class MoveGlitchUserSettings < ActiveRecord::Migration[6.1]
  class User < ApplicationRecord; end

  MAPPING = {
    favourite_modal: 'web.favourite_modal',
    system_emoji_font: 'web.use_system_emoji_font',
    hide_followers_count: 'hide_followers_count',
    default_content_type: 'default_content_type',
    flavour: 'flavour',
    skin: 'skin',
    notification_emails: {
      trending_link: 'notification_emails.link_trends',
      trending_status: 'notification_emails.status_trends',
    }.freeze,
  }.freeze

  class LegacySetting < ApplicationRecord
    self.table_name = 'settings'

    def var
      self[:var]&.to_sym
    end

    def value
      YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
    end
  end

  def up
    User.find_each do |user|
      previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var)

      user_settings = Oj.load(user.settings || '{}')
      user_settings.delete('theme')

      MAPPING.each do |legacy_key, new_key|
        value = previous_settings[legacy_key]&.value

        next if value.blank?

        if value.is_a?(Hash)
          value.each do |nested_key, nested_value|
            user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
          end
        else
          user_settings[new_key] = value
        end
      end

      user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
    end
  end

  def down; end
end