Browse Source

Enable updating additional account information from user preferences via rest api (#6789)

* Enable updating additional account information from user preferences via rest api
Resolves #6553

* Pacify rubocop

* Decoerce incoming settings in UserSettingsDecorator

* Create user preferences hash directly from incoming credentials instead of going through ActionController::Parameters

* Clean up user preferences update

* Use ActiveModel::Type::Boolean instead of manually checking stringified number equivalence
pull/4/head
Levi Bard 6 years ago
committed by Eugen Rochko
parent
commit
cd0eaa349c
4 changed files with 31 additions and 2 deletions
  1. +12
    -0
      app/controllers/api/v1/accounts/credentials_controller.rb
  2. +2
    -2
      app/lib/user_settings_decorator.rb
  3. +6
    -0
      spec/controllers/api/v1/accounts/credentials_controller_spec.rb
  4. +11
    -0
      spec/lib/user_settings_decorator_spec.rb

+ 12
- 0
app/controllers/api/v1/accounts/credentials_controller.rb View File

@ -13,6 +13,7 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController
def update def update
@account = current_account @account = current_account
UpdateAccountService.new.call(@account, account_params, raise_error: true) UpdateAccountService.new.call(@account, account_params, raise_error: true)
UserSettingsDecorator.new(current_user).update(user_settings_params) if user_settings_params
ActivityPub::UpdateDistributionWorker.perform_async(@account.id) ActivityPub::UpdateDistributionWorker.perform_async(@account.id)
render json: @account, serializer: REST::CredentialAccountSerializer render json: @account, serializer: REST::CredentialAccountSerializer
end end
@ -22,4 +23,15 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController
def account_params def account_params
params.permit(:display_name, :note, :avatar, :header, :locked) params.permit(:display_name, :note, :avatar, :header, :locked)
end end
def user_settings_params
return nil unless params.key?(:source)
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),
}
end
end end

+ 2
- 2
app/lib/user_settings_decorator.rb View File

@ -83,7 +83,7 @@ class UserSettingsDecorator
end end
def boolean_cast_setting(key) def boolean_cast_setting(key)
settings[key] == '1'
ActiveModel::Type::Boolean.new.cast(settings[key])
end end
def coerced_settings(key) def coerced_settings(key)
@ -91,7 +91,7 @@ class UserSettingsDecorator
end end
def coerce_values(params_hash) def coerce_values(params_hash)
params_hash.transform_values { |x| x == '1' }
params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) }
end end
def change?(key) def change?(key)

+ 6
- 0
spec/controllers/api/v1/accounts/credentials_controller_spec.rb View File

@ -28,6 +28,10 @@ describe Api::V1::Accounts::CredentialsController do
note: "Hi!\n\nToot toot!", note: "Hi!\n\nToot toot!",
avatar: fixture_file_upload('files/avatar.gif', 'image/gif'), avatar: fixture_file_upload('files/avatar.gif', 'image/gif'),
header: fixture_file_upload('files/attachment.jpg', 'image/jpeg'), header: fixture_file_upload('files/attachment.jpg', 'image/jpeg'),
source: {
privacy: 'unlisted',
sensitive: true,
}
} }
end end
@ -42,6 +46,8 @@ describe Api::V1::Accounts::CredentialsController do
expect(user.account.note).to eq("Hi!\n\nToot toot!") expect(user.account.note).to eq("Hi!\n\nToot toot!")
expect(user.account.avatar).to exist expect(user.account.avatar).to exist
expect(user.account.header).to exist expect(user.account.header).to exist
expect(user.setting_default_privacy).to eq('unlisted')
expect(user.setting_default_sensitive).to eq(true)
end end
it 'queues up an account update distribution' do it 'queues up an account update distribution' do

+ 11
- 0
spec/lib/user_settings_decorator_spec.rb View File

@ -69,5 +69,16 @@ describe UserSettingsDecorator do
settings.update(values) settings.update(values)
expect(user.settings['system_font_ui']).to eq false expect(user.settings['system_font_ui']).to eq false
end end
it 'decoerces setting values before applying' do
values = {
'setting_delete_modal' => 'false',
'setting_boost_modal' => 'true',
}
settings.update(values)
expect(user.settings['delete_modal']).to eq false
expect(user.settings['boost_modal']).to eq true
end
end end
end end

Loading…
Cancel
Save