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
signup-info-prompt
Levi Bard 2018-04-08 13:43:10 +02:00 committed by Eugen Rochko
parent 1364e9e4ae
commit cd0eaa349c
4 changed files with 31 additions and 2 deletions

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

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)

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

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