credentials_controller.rb 1.62 KB
Newer Older
1
2
# frozen_string_literal: true

3
class Api::V1::Accounts::CredentialsController < Api::BaseController
4
5
  before_action -> { doorkeeper_authorize! :read, :'read:accounts' }, except: [:update]
  before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:update]
6
7
8
9
  before_action :require_user!

  def show
    @account = current_account
10
    render json: @account, serializer: REST::CredentialAccountSerializer
11
12
13
14
  end

  def update
    @account = current_account
15
    UpdateAccountService.new.call(@account, account_params, raise_error: true)
16
    UserSettingsDecorator.new(current_user).update(user_settings_params) if user_settings_params
Eugen Rochko's avatar
Eugen Rochko committed
17
    ActivityPub::UpdateDistributionWorker.perform_async(@account.id)
18
    render json: @account, serializer: REST::CredentialAccountSerializer
19
20
21
22
23
  end

  private

  def account_params
Eugen Rochko's avatar
Eugen Rochko committed
24
    params.permit(:display_name, :note, :avatar, :header, :locked, :bot, :discoverable, fields_attributes: [:name, :value])
25
  end
26
27

  def user_settings_params
28
    return nil if params[:source].blank?
29
30
31
32
33
34

    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),
35
      'setting_default_language' => source_params.fetch(:language, @account.user.setting_default_language),
36
      'setting_default_federation' => source_params.fetch(:federation, @account.user.setting_default_federation),
37
      'setting_default_content_type' => source_params.fetch(:content_type, @account.user.setting_default_content_type),
38
39
    }
  end
40
end