- Notifications
You must be signed in to change notification settings - Fork 7.2k
/
Copy pathaccounts_controller.rb
132 lines (101 loc) · 4.98 KB
/
accounts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# frozen_string_literal: true
classApi::V1::AccountsController < Api::BaseController
includeRegistrationHelper
before_action->{authorize_if_got_token!:read,:'read:accounts'},except: [:create,:follow,:unfollow,:remove_from_followers,:block,:unblock,:mute,:unmute]
before_action->{doorkeeper_authorize!:follow,:write,:'write:follows'},only: [:follow,:unfollow,:remove_from_followers]
before_action->{doorkeeper_authorize!:follow,:write,:'write:mutes'},only: [:mute,:unmute]
before_action->{doorkeeper_authorize!:follow,:write,:'write:blocks'},only: [:block,:unblock]
before_action->{doorkeeper_authorize!:write,:'write:accounts'},only: [:create]
before_action:require_user!,except: [:index,:show,:create]
before_action:set_account,except: [:index,:create]
before_action:set_accounts,only: [:index]
before_action:check_account_approval,except: [:index,:create]
before_action:check_account_confirmation,except: [:index,:create]
before_action:check_enabled_registrations,only: [:create]
before_action:check_accounts_limit,only: [:index]
before_action:check_following_self,only: [:follow]
skip_before_action:require_authenticated_user!,only: :create
override_rate_limit_headers:follow,family: :follows
defindex
renderjson: @accounts,each_serializer: REST::AccountSerializer
end
defshow
cache_if_unauthenticated!
renderjson: @account,serializer: REST::AccountSerializer
end
defcreate
token=AppSignUpService.new.call(doorkeeper_token.application,request.remote_ip,account_params)
response=Doorkeeper::OAuth::TokenResponse.new(token)
headers.merge!(response.headers)
self.response_body=Oj.dump(response.body)
self.status=response.status
rescueActiveRecord::RecordInvalid=>e
renderjson: ValidationErrorFormatter.new(e,'account.username': :username,'invite_request.text': :reason).as_json,status: 422
end
deffollow
follow=FollowService.new.call(current_user.account,@account,reblogs: params.key?(:reblogs) ? truthy_param?(:reblogs) : nil,notify: params.key?(:notify) ? truthy_param?(:notify) : nil,languages: params.key?(:languages) ? params[:languages] : nil,with_rate_limit: true)
options=@account.locked? || current_user.account.silenced? ? {} : {following_map: {@account.id=>{reblogs: follow.show_reblogs?,notify: follow.notify?,languages: follow.languages}},requested_map: {@account.id=>false}}
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships(**options)
end
defblock
BlockService.new.call(current_user.account,@account)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
defmute
MuteService.new.call(current_user.account,@account,notifications: truthy_param?(:notifications),duration: params[:duration].to_i)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
defunfollow
UnfollowService.new.call(current_user.account,@account)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
defremove_from_followers
RemoveFromFollowersService.new.call(current_user.account,@account)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
defunblock
UnblockService.new.call(current_user.account,@account)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
defunmute
UnmuteService.new.call(current_user.account,@account)
renderjson: @account,serializer: REST::RelationshipSerializer,relationships: relationships
end
private
defset_account
@account=Account.find(params[:id])
end
defset_accounts
@accounts=Account.where(id: account_ids).without_unapproved
end
defcheck_account_approval
raise(ActiveRecord::RecordNotFound)if@account.local? && @account.user_pending?
end
defcheck_account_confirmation
raise(ActiveRecord::RecordNotFound)if@account.local? && !@account.user_confirmed?
end
defcheck_accounts_limit
raise(Mastodon::ValidationError)ifaccount_ids.size > DEFAULT_ACCOUNTS_LIMIT
end
defcheck_following_self
renderjson: {error: I18n.t('accounts.self_follow_error')},status: 403ifcurrent_user.account.id == @account.id
end
defrelationships(**)
AccountRelationshipsPresenter.new([@account],current_user.account_id, **)
end
defaccount_ids
Array(accounts_params[:id]).uniq.map(&:to_i)
end
defaccounts_params
params.permit(id: [])
end
defaccount_params
params.permit(:username,:email,:password,:agreement,:locale,:reason,:time_zone,:invite_code,:date_of_birth)
end
definvite
Invite.find_by(code: params[:invite_code])ifparams[:invite_code].present?
end
defcheck_enabled_registrations
forbiddenunlessallowed_registration?(request.remote_ip,invite)
end
end