From 3dcba94e68889f3d6603fa5416d54edd08dcf4bd Mon Sep 17 00:00:00 2001 From: Claire Date: Fri, 31 Mar 2023 21:37:57 +0200 Subject: [PATCH] Migrate glitch-soc's exclusive user settings --- .../settings/flavours_controller.rb | 14 +---- app/helpers/accounts_helper.rb | 2 +- app/models/concerns/has_user_settings.rb | 36 +++++++++++- app/models/user_settings.rb | 9 ++- ...0230215074424_move_glitch_user_settings.rb | 57 +++++++++++++++++++ db/schema.rb | 2 +- 6 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20230215074424_move_glitch_user_settings.rb diff --git a/app/controllers/settings/flavours_controller.rb b/app/controllers/settings/flavours_controller.rb index 62c52eee97..c1172598b4 100644 --- a/app/controllers/settings/flavours_controller.rb +++ b/app/controllers/settings/flavours_controller.rb @@ -21,18 +21,8 @@ class Settings::FlavoursController < Settings::BaseController end def update - user_settings.update(user_settings_params) + current_user.settings.update(flavour: params.require(:flavour), skin: params.dig(:user, :setting_skin)) + current_user.save redirect_to action: 'show', flavour: params[:flavour] end - - private - - def user_settings - UserSettingsDecorator.new(current_user) - end - - def user_settings_params - { setting_flavour: params.require(:flavour), - setting_skin: params.dig(:user, :setting_skin) }.with_indifferent_access - end end diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb index e15aee6df1..91c3a116b7 100644 --- a/app/helpers/accounts_helper.rb +++ b/app/helpers/accounts_helper.rb @@ -28,7 +28,7 @@ module AccountsHelper end def hide_followers_count?(account) - Setting.hide_followers_count || account.user&.setting_hide_followers_count + Setting.hide_followers_count || account.user&.settings['hide_followers_count'] end def account_description(account) diff --git a/app/models/concerns/has_user_settings.rb b/app/models/concerns/has_user_settings.rb index b3fa1f683b..0e9d4e1cd4 100644 --- a/app/models/concerns/has_user_settings.rb +++ b/app/models/concerns/has_user_settings.rb @@ -39,6 +39,10 @@ module HasUserSettings settings['web.delete_modal'] end + def setting_favourite_modal + settings['web.favourite_modal'] + end + def setting_reduce_motion settings['web.reduce_motion'] end @@ -47,12 +51,20 @@ module HasUserSettings settings['web.use_system_font'] end + def setting_system_emoji_font + settings['web.use_system_emoji_font'] + end + def setting_noindex settings['noindex'] end - def setting_theme - settings['theme'] + def setting_flavour + settings['flavour'] + end + + def setting_skin + settings['skin'] end def setting_display_media @@ -107,6 +119,14 @@ module HasUserSettings settings['default_privacy'] || (account.locked? ? 'private' : 'public') end + def setting_default_content_type + settings['default_content_type'] + end + + def setting_hide_followers_count + settings['hide_followers_count'] + end + def allows_report_emails? settings['notification_emails.report'] end @@ -123,6 +143,18 @@ module HasUserSettings settings['notification_emails.trends'] end + def allows_trending_tags_review_emails? + settings['notification_emails.trends'] + end + + def allows_trending_links_review_emails? + settings['notification_emails.link_trends'] + end + + def allows_trending_statuses_review_emails? + settings['notification_emails.status_trends'] + end + def aggregates_reblogs? settings['aggregate_reblogs'] end diff --git a/app/models/user_settings.rb b/app/models/user_settings.rb index 2c025d6c56..0be8c5fbce 100644 --- a/app/models/user_settings.rb +++ b/app/models/user_settings.rb @@ -9,12 +9,15 @@ class UserSettings setting :always_send_emails, default: false setting :aggregate_reblogs, default: true - setting :theme, default: -> { ::Setting.theme } + setting :flavour, default: -> { ::Setting.flavour } + setting :skin, default: -> { ::Setting.skin } setting :noindex, default: -> { ::Setting.noindex } setting :show_application, default: true setting :default_language, default: nil setting :default_sensitive, default: false setting :default_privacy, default: nil + setting :default_content_type, default: 'text/plain' + setting :hide_followers_count, default: false namespace :web do setting :crop_images, default: true @@ -27,10 +30,12 @@ class UserSettings setting :delete_modal, default: true setting :reblog_modal, default: false setting :unfollow_modal, default: true + setting :favourite_modal, default: false setting :reduce_motion, default: false setting :expand_content_warnings, default: false setting :display_media, default: 'default', in: %w(default show_all hide_all) setting :auto_play, default: false + setting :use_system_emoji_font, default: false end namespace :notification_emails do @@ -42,6 +47,8 @@ class UserSettings setting :report, default: true setting :pending_account, default: true setting :trends, default: true + setting :link_trends, default: false + setting :status_trends, default: false setting :appeal, default: true end diff --git a/db/migrate/20230215074424_move_glitch_user_settings.rb b/db/migrate/20230215074424_move_glitch_user_settings.rb new file mode 100644 index 0000000000..6b5a259259 --- /dev/null +++ b/db/migrate/20230215074424_move_glitch_user_settings.rb @@ -0,0 +1,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]) 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 diff --git a/db/schema.rb b/db/schema.rb index 5b03e24f54..7d894b1aa1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_02_15_074423) do +ActiveRecord::Schema.define(version: 2023_02_15_074424) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"