Change user settings to be stored in a more optimal way (#23630)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
e7c3e55874
commit
a9b5598c97
36 changed files with 817 additions and 525 deletions
7
db/migrate/20230215074327_add_settings_to_users.rb
Normal file
7
db/migrate/20230215074327_add_settings_to_users.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddSettingsToUsers < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :users, :settings, :text
|
||||
end
|
||||
end
|
84
db/migrate/20230215074423_move_user_settings.rb
Normal file
84
db/migrate/20230215074423_move_user_settings.rb
Normal file
|
@ -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
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2022_12_06_114142) do
|
||||
ActiveRecord::Schema.define(version: 2023_02_15_074423) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -1060,6 +1060,7 @@ ActiveRecord::Schema.define(version: 2022_12_06_114142) do
|
|||
t.inet "sign_up_ip"
|
||||
t.boolean "skip_sign_in_token"
|
||||
t.bigint "role_id"
|
||||
t.text "settings"
|
||||
t.index ["account_id"], name: "index_users_on_account_id"
|
||||
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
|
||||
t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id", where: "(created_by_application_id IS NOT NULL)"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue