]> cat aescling's git repositories - mastodon.git/blob - app/controllers/api/v1/accounts_controller.rb
Add whitelist mode (#11291)
[mastodon.git] / app / controllers / api / v1 / accounts_controller.rb
1 # frozen_string_literal: true
2
3 class Api::V1::AccountsController < Api::BaseController
4 before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :block, :unblock, :mute, :unmute]
5 before_action -> { doorkeeper_authorize! :follow, :'write:follows' }, only: [:follow, :unfollow]
6 before_action -> { doorkeeper_authorize! :follow, :'write:mutes' }, only: [:mute, :unmute]
7 before_action -> { doorkeeper_authorize! :follow, :'write:blocks' }, only: [:block, :unblock]
8 before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:create]
9
10 before_action :require_user!, except: [:show, :create]
11 before_action :set_account, except: [:create]
12 before_action :check_account_suspension, only: [:show]
13 before_action :check_enabled_registrations, only: [:create]
14
15 skip_before_action :require_authenticated_user!, only: :create
16
17 respond_to :json
18
19 def show
20 render json: @account, serializer: REST::AccountSerializer
21 end
22
23 def create
24 token = AppSignUpService.new.call(doorkeeper_token.application, account_params)
25 response = Doorkeeper::OAuth::TokenResponse.new(token)
26
27 headers.merge!(response.headers)
28
29 self.response_body = Oj.dump(response.body)
30 self.status = response.status
31 end
32
33 def follow
34 FollowService.new.call(current_user.account, @account, reblogs: truthy_param?(:reblogs))
35
36 options = @account.locked? ? {} : { following_map: { @account.id => { reblogs: truthy_param?(:reblogs) } }, requested_map: { @account.id => false } }
37
38 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(options)
39 end
40
41 def block
42 BlockService.new.call(current_user.account, @account)
43 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
44 end
45
46 def mute
47 MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications))
48 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
49 end
50
51 def unfollow
52 UnfollowService.new.call(current_user.account, @account)
53 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
54 end
55
56 def unblock
57 UnblockService.new.call(current_user.account, @account)
58 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
59 end
60
61 def unmute
62 UnmuteService.new.call(current_user.account, @account)
63 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
64 end
65
66 private
67
68 def set_account
69 @account = Account.find(params[:id])
70 end
71
72 def relationships(**options)
73 AccountRelationshipsPresenter.new([@account.id], current_user.account_id, options)
74 end
75
76 def check_account_suspension
77 gone if @account.suspended?
78 end
79
80 def account_params
81 params.permit(:username, :email, :password, :agreement, :locale)
82 end
83
84 def check_enabled_registrations
85 forbidden if single_user_mode? || !allowed_registrations?
86 end
87
88 def allowed_registrations?
89 Setting.registrations_mode != 'none'
90 end
91 end
This page took 0.14515 seconds and 4 git commands to generate.