]> cat aescling's git repositories - mastodon.git/blob - app/controllers/api/v1/accounts_controller.rb
Merge branch 'master' into french-translation
[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]
5 before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock]
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
12 end
13
14 def verify_credentials
15 @account = current_user.account
16 render action: :show
17 end
18
19 def following
20 results = Follow.where(account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
21 accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
22 @accounts = results.map { |f| accounts[f.target_account_id] }
23
24 set_account_counters_maps(@accounts)
25
26 next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
27 prev_path = following_api_v1_account_url(since_id: results.first.id) unless results.empty?
28
29 set_pagination_headers(next_path, prev_path)
30
31 render action: :index
32 end
33
34 def followers
35 results = Follow.where(target_account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
36 accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
37 @accounts = results.map { |f| accounts[f.account_id] }
38
39 set_account_counters_maps(@accounts)
40
41 next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
42 prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty?
43
44 set_pagination_headers(next_path, prev_path)
45
46 render action: :index
47 end
48
49 def common_followers
50 @accounts = @account.common_followers_with(current_user.account)
51 render action: :index
52 end
53
54 def suggestions
55 @accounts = FollowSuggestion.get(current_user.account_id)
56 render action: :index
57 end
58
59 def statuses
60 @statuses = @account.statuses.with_includes.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a
61
62 set_maps(@statuses)
63 set_counters_maps(@statuses)
64
65 next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
66 prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
67
68 set_pagination_headers(next_path, prev_path)
69 end
70
71 def follow
72 FollowService.new.call(current_user.account, @account.acct)
73 set_relationship
74 render action: :relationship
75 end
76
77 def block
78 BlockService.new.call(current_user.account, @account)
79 set_relationship
80 render action: :relationship
81 end
82
83 def unfollow
84 UnfollowService.new.call(current_user.account, @account)
85 set_relationship
86 render action: :relationship
87 end
88
89 def unblock
90 UnblockService.new.call(current_user.account, @account)
91 set_relationship
92 render action: :relationship
93 end
94
95 def relationships
96 ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
97 @accounts = Account.where(id: ids).select('id')
98 @following = Account.following_map(ids, current_user.account_id)
99 @followed_by = Account.followed_by_map(ids, current_user.account_id)
100 @blocking = Account.blocking_map(ids, current_user.account_id)
101 end
102
103 def search
104 limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
105 @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
106
107 set_account_counters_maps(@accounts)
108
109 render action: :index
110 end
111
112 private
113
114 def set_account
115 @account = Account.find(params[:id])
116 end
117
118 def set_relationship
119 @following = Account.following_map([@account.id], current_user.account_id)
120 @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
121 @blocking = Account.blocking_map([@account.id], current_user.account_id)
122 end
123 end
This page took 0.118809 seconds and 5 git commands to generate.