]> cat aescling's git repositories - mastodon.git/blob - app/controllers/api/v1/accounts_controller.rb
Make public timelines API not require user context/app credentials (#1291)
[mastodon.git] / app / controllers / api / v1 / accounts_controller.rb
1 # frozen_string_literal: true
2
3 class Api::V1::AccountsController < ApiController
4 before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock, :mute, :unmute]
5 before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock, :mute, :unmute]
6 before_action :require_user!, except: [:show, :following, :followers, :statuses]
7 before_action :set_account, except: [:verify_credentials, :suggestions, :search]
8
9 respond_to :json
10
11 def show; end
12
13 def verify_credentials
14 @account = current_user.account
15 render action: :show
16 end
17
18 def following
19 results = Follow.where(account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
20 accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
21 @accounts = results.map { |f| accounts[f.target_account_id] }
22
23 next_path = following_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
24 prev_path = following_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
25
26 set_pagination_headers(next_path, prev_path)
27
28 render action: :index
29 end
30
31 def followers
32 results = Follow.where(target_account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
33 accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
34 @accounts = results.map { |f| accounts[f.account_id] }
35
36 next_path = followers_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
37 prev_path = followers_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
38
39 set_pagination_headers(next_path, prev_path)
40
41 render action: :index
42 end
43
44 def statuses
45 @statuses = @account.statuses.permitted_for(@account, current_account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
46 @statuses = @statuses.where(id: MediaAttachment.where(account: @account).where.not(status_id: nil).reorder('').select('distinct status_id')) if params[:only_media]
47 @statuses = @statuses.without_replies if params[:exclude_replies]
48 @statuses = cache_collection(@statuses, Status)
49
50 set_maps(@statuses)
51
52 next_path = statuses_api_v1_account_url(statuses_pagination_params(max_id: @statuses.last.id)) unless @statuses.empty?
53 prev_path = statuses_api_v1_account_url(statuses_pagination_params(since_id: @statuses.first.id)) unless @statuses.empty?
54
55 set_pagination_headers(next_path, prev_path)
56 end
57
58 def follow
59 FollowService.new.call(current_user.account, @account.acct)
60 set_relationship
61 render action: :relationship
62 end
63
64 def block
65 BlockService.new.call(current_user.account, @account)
66
67 @following = { @account.id => false }
68 @followed_by = { @account.id => false }
69 @blocking = { @account.id => true }
70 @requested = { @account.id => false }
71 @muting = { @account.id => current_user.account.muting?(@account.id) }
72
73 render action: :relationship
74 end
75
76 def mute
77 MuteService.new.call(current_user.account, @account)
78 set_relationship
79 render action: :relationship
80 end
81
82 def unfollow
83 UnfollowService.new.call(current_user.account, @account)
84 set_relationship
85 render action: :relationship
86 end
87
88 def unblock
89 UnblockService.new.call(current_user.account, @account)
90 set_relationship
91 render action: :relationship
92 end
93
94 def unmute
95 UnmuteService.new.call(current_user.account, @account)
96 set_relationship
97 render action: :relationship
98 end
99
100 def relationships
101 ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
102
103 @accounts = Account.where(id: ids).select('id')
104 @following = Account.following_map(ids, current_user.account_id)
105 @followed_by = Account.followed_by_map(ids, current_user.account_id)
106 @blocking = Account.blocking_map(ids, current_user.account_id)
107 @muting = Account.muting_map(ids, current_user.account_id)
108 @requested = Account.requested_map(ids, current_user.account_id)
109 end
110
111 def search
112 @accounts = AccountSearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true', current_account)
113
114 render action: :index
115 end
116
117 private
118
119 def set_account
120 @account = Account.find(params[:id])
121 end
122
123 def set_relationship
124 @following = Account.following_map([@account.id], current_user.account_id)
125 @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
126 @blocking = Account.blocking_map([@account.id], current_user.account_id)
127 @muting = Account.muting_map([@account.id], current_user.account_id)
128 @requested = Account.requested_map([@account.id], current_user.account_id)
129 end
130
131 def pagination_params(core_params)
132 params.permit(:limit).merge(core_params)
133 end
134
135 def statuses_pagination_params(core_params)
136 params.permit(:limit, :only_media, :exclude_replies).merge(core_params)
137 end
138 end
This page took 0.105299 seconds and 5 git commands to generate.