credentials_controller.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. end
  17. private
  18. def account_params
  19. params.permit(
  20. :display_name,
  21. :note,
  22. :avatar,
  23. :header,
  24. :locked,
  25. :bot,
  26. :discoverable,
  27. :hide_collections,
  28. :indexable,
  29. fields_attributes: [:name, :value]
  30. )
  31. end
  32. def user_params
  33. return nil if params[:source].blank?
  34. source_params = params.require(:source)
  35. {
  36. settings_attributes: {
  37. default_privacy: source_params.fetch(:privacy, @account.user.setting_default_privacy),
  38. default_sensitive: source_params.fetch(:sensitive, @account.user.setting_default_sensitive),
  39. default_language: source_params.fetch(:language, @account.user.setting_default_language),
  40. },
  41. }
  42. end
  43. end