]> cat aescling's git repositories - mastodon.git/blob - app/controllers/api/v1/accounts_controller.rb
Add REST API for creating an account (#9572)
[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 respond_to :json
16
17 def show
18 render json: @account, serializer: REST::AccountSerializer
19 end
20
21 def create
22 token = AppSignUpService.new.call(doorkeeper_token.application, account_params)
23 response = Doorkeeper::OAuth::TokenResponse.new(token)
24
25 headers.merge!(response.headers)
26
27 self.response_body = Oj.dump(response.body)
28 self.status = response.status
29 end
30
31 def follow
32 FollowService.new.call(current_user.account, @account, reblogs: truthy_param?(:reblogs))
33
34 options = @account.locked? ? {} : { following_map: { @account.id => { reblogs: truthy_param?(:reblogs) } }, requested_map: { @account.id => false } }
35
36 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(options)
37 end
38
39 def block
40 BlockService.new.call(current_user.account, @account)
41 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
42 end
43
44 def mute
45 MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications))
46 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
47 end
48
49 def unfollow
50 UnfollowService.new.call(current_user.account, @account)
51 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
52 end
53
54 def unblock
55 UnblockService.new.call(current_user.account, @account)
56 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
57 end
58
59 def unmute
60 UnmuteService.new.call(current_user.account, @account)
61 render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
62 end
63
64 private
65
66 def set_account
67 @account = Account.find(params[:id])
68 end
69
70 def relationships(**options)
71 AccountRelationshipsPresenter.new([@account.id], current_user.account_id, options)
72 end
73
74 def check_account_suspension
75 gone if @account.suspended?
76 end
77
78 def account_params
79 params.permit(:username, :email, :password, :agreement)
80 end
81
82 def check_enabled_registrations
83 forbidden if single_user_mode? || !Setting.open_registrations
84 end
85 end
This page took 0.108062 seconds and 5 git commands to generate.