Skip to content

Commit

Permalink
Change user settings to be stored in a more optimal way (mastodon#23630)
Browse files Browse the repository at this point in the history
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
  • Loading branch information
2 people authored and skerit committed Jul 7, 2023
1 parent 925d8ee commit d7c909a
Show file tree
Hide file tree
Showing 36 changed files with 818 additions and 526 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Expand Up @@ -185,3 +185,11 @@ Style/TrailingCommaInHashLiteral:

Style/SymbolArray:
Enabled: false

# Reason: Prefer less intendation in conditional assignments
# https://docs.rubocop.org/rubocop/cops_style.html#styleredundantbegin
Style/RedundantBegin:
Enabled: false

RSpec/NamedSubject:
EnforcedStyle: named_only
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Expand Up @@ -259,6 +259,7 @@ Metrics/ModuleLength:
- 'app/helpers/jsonld_helper.rb'
- 'app/helpers/statuses_helper.rb'
- 'app/models/concerns/account_interactions.rb'
- 'app/models/concerns/has_user_settings.rb'

# Configuration parameters: Max, CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Expand Down
12 changes: 7 additions & 5 deletions app/controllers/api/v1/accounts/credentials_controller.rb
Expand Up @@ -13,7 +13,7 @@ def show
def update
@account = current_account
UpdateAccountService.new.call(@account, account_params, raise_error: true)
UserSettingsDecorator.new(current_user).update(user_settings_params) if user_settings_params
current_user.update(user_params) if user_params
ActivityPub::UpdateDistributionWorker.perform_async(@account.id)
render json: @account, serializer: REST::CredentialAccountSerializer
end
Expand All @@ -34,15 +34,17 @@ def account_params
)
end

def user_settings_params
def user_params
return nil if params[:source].blank?

source_params = params.require(:source)

{
'setting_default_privacy' => source_params.fetch(:privacy, @account.user.setting_default_privacy),
'setting_default_sensitive' => source_params.fetch(:sensitive, @account.user.setting_default_sensitive),
'setting_default_language' => source_params.fetch(:language, @account.user.setting_default_language),
settings_attributes: {
default_privacy: source_params.fetch(:privacy, @account.user.setting_default_privacy),
default_sensitive: source_params.fetch(:sensitive, @account.user.setting_default_sensitive),
default_language: source_params.fetch(:language, @account.user.setting_default_language),
},
}
end
end
40 changes: 1 addition & 39 deletions app/controllers/settings/preferences_controller.rb
Expand Up @@ -4,8 +4,6 @@ class Settings::PreferencesController < Settings::BaseController
def show; end

def update
user_settings.update(user_settings_params.to_h)

if current_user.update(user_params)
I18n.locale = current_user.locale
redirect_to after_update_redirect_path, notice: I18n.t('generic.changes_saved_msg')
Expand All @@ -20,43 +18,7 @@ def after_update_redirect_path
settings_preferences_path
end

def user_settings
UserSettingsDecorator.new(current_user)
end

def user_params
params.require(:user).permit(
:locale,
chosen_languages: []
)
end

def user_settings_params
params.require(:user).permit(
:setting_default_privacy,
:setting_default_sensitive,
:setting_default_language,
:setting_unfollow_modal,
:setting_boost_modal,
:setting_delete_modal,
:setting_auto_play_gif,
:setting_display_media,
:setting_expand_spoilers,
:setting_reduce_motion,
:setting_disable_swiping,
:setting_system_font_ui,
:setting_noindex,
:setting_theme,
:setting_aggregate_reblogs,
:setting_show_application,
:setting_advanced_layout,
:setting_use_blurhash,
:setting_use_pending_items,
:setting_trends,
:setting_crop_images,
:setting_always_send_emails,
notification_emails: %i(follow follow_request reblog favourite mention report pending_account trending_tag appeal),
interactions: %i(must_be_follower must_be_following must_be_following_dm)
)
params.require(:user).permit(:locale, chosen_languages: [], settings_attributes: UserSettings.keys)
end
end
155 changes: 0 additions & 155 deletions app/lib/user_settings_decorator.rb

This file was deleted.

19 changes: 19 additions & 0 deletions app/lib/user_settings_serializer.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class UserSettingsSerializer
def self.load(value)
json = begin
if value.blank?
{}
else
Oj.load(value, symbol_keys: true)
end
end

UserSettings.new(json)
end

def self.dump(value)
Oj.dump(value.as_json)
end
end

0 comments on commit d7c909a

Please sign in to comment.