credentials_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::CredentialsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:accounts' }, except: [:update]
  4. before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:update]
  5. before_action :require_user!
  6. def show
  7. @account = current_account
  8. render json: @account, serializer: REST::CredentialAccountSerializer
  9. end
  10. def update
  11. @account = current_account
  12. UpdateAccountService.new.call(@account, account_params, raise_error: true)
  13. current_user.update(user_params) if user_params
  14. ActivityPub::UpdateDistributionWorker.perform_async(@account.id)
  15. render json: @account, serializer: REST::CredentialAccountSerializer
  16. rescue ActiveRecord::RecordInvalid => e
  17. render json: ValidationErrorFormatter.new(e).as_json, status: 422
  18. end
  19. private
  20. def account_params
  21. params.permit(
  22. :display_name,
  23. :note,
  24. :avatar,
  25. :header,
  26. :locked,
  27. :bot,
  28. :discoverable,
  29. :hide_collections,
  30. :indexable,
  31. fields_attributes: [:name, :value]
  32. )
  33. end
  34. def user_params
  35. return nil if params[:source].blank?
  36. source_params = params.require(:source)
  37. {
  38. settings_attributes: {
  39. default_privacy: source_params.fetch(:privacy, @account.user.setting_default_privacy),
  40. default_sensitive: source_params.fetch(:sensitive, @account.user.setting_default_sensitive),
  41. default_language: source_params.fetch(:language, @account.user.setting_default_language),
  42. },
  43. }
  44. end
  45. end