about summary refs log tree commit diff
path: root/db/migrate
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2023-03-30 14:44:00 +0200
committerGitHub <noreply@github.com>2023-03-30 14:44:00 +0200
commita9b5598c97fc4d3302b61b260097ef41c2ebe377 (patch)
tree2568f87b80c64214f3d03bedb6e6f85a77e9ddb0 /db/migrate
parente7c3e5587489a9e248973fa0719869541d34ba9f (diff)
Change user settings to be stored in a more optimal way (#23630)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Diffstat (limited to 'db/migrate')
-rw-r--r--db/migrate/20230215074327_add_settings_to_users.rb7
-rw-r--r--db/migrate/20230215074423_move_user_settings.rb84
2 files changed, 91 insertions, 0 deletions
diff --git a/db/migrate/20230215074327_add_settings_to_users.rb b/db/migrate/20230215074327_add_settings_to_users.rb
new file mode 100644
index 000000000..ff5308f42
--- /dev/null
+++ b/db/migrate/20230215074327_add_settings_to_users.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddSettingsToUsers < ActiveRecord::Migration[6.1]
+  def change
+    add_column :users, :settings, :text
+  end
+end
diff --git a/db/migrate/20230215074423_move_user_settings.rb b/db/migrate/20230215074423_move_user_settings.rb
new file mode 100644
index 000000000..351a8b61d
--- /dev/null
+++ b/db/migrate/20230215074423_move_user_settings.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+class MoveUserSettings < ActiveRecord::Migration[6.1]
+  class User < ApplicationRecord; end
+
+  MAPPING = {
+    default_privacy: 'default_privacy',
+    default_sensitive: 'web.default_sensitive',
+    default_language: 'default_language',
+    noindex: 'noindex',
+    theme: 'theme',
+    trends: 'web.trends',
+    unfollow_modal: 'web.unfollow_modal',
+    boost_modal: 'web.reblog_modal',
+    delete_modal: 'web.delete_modal',
+    auto_play_gif: 'web.auto_play',
+    display_media: 'web.display_media',
+    expand_spoilers: 'web.expand_content_warnings',
+    reduce_motion: 'web.reduce_motion',
+    disable_swiping: 'web.disable_swiping',
+    show_application: 'show_application',
+    system_font_ui: 'web.use_system_font',
+    aggregate_reblogs: 'aggregate_reblogs',
+    advanced_layout: 'web.advanced_layout',
+    use_blurhash: 'web.use_blurhash',
+    use_pending_items: 'web.use_pending_items',
+    crop_images: 'web.crop_images',
+    notification_emails: {
+      follow: 'notification_emails.follow',
+      reblog: 'notification_emails.reblog',
+      favourite: 'notification_emails.favourite',
+      mention: 'notification_emails.mention',
+      follow_request: 'notification_emails.follow_request',
+      report: 'notification_emails.report',
+      pending_account: 'notification_emails.pending_account',
+      trending_tag: 'notification_emails.trends',
+      appeal: 'notification_emails.appeal',
+    }.freeze,
+    always_send_emails: 'always_send_emails',
+    interactions: {
+      must_be_follower: 'interactions.must_be_follower',
+      must_be_following: 'interactions.must_be_following',
+      must_be_following_dm: 'interactions.must_be_following_dm',
+    }.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]) 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 = {}
+
+      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