]> cat aescling's git repositories - mastodon.git/blob - app/controllers/follower_accounts_controller.rb
Add more ActivityPub controller tests (#13590)
[mastodon.git] / app / controllers / follower_accounts_controller.rb
1 # frozen_string_literal: true
2
3 class FollowerAccountsController < ApplicationController
4 include AccountControllerConcern
5 include SignatureVerification
6
7 before_action :require_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
8 before_action :set_cache_headers
9
10 skip_around_action :set_locale, if: -> { request.format == :json }
11 skip_before_action :require_functional!
12
13 def index
14 respond_to do |format|
15 format.html do
16 expires_in 0, public: true unless user_signed_in?
17
18 next if @account.user_hides_network?
19
20 follows
21 end
22
23 format.json do
24 raise Mastodon::NotPermittedError if page_requested? && @account.user_hides_network?
25
26 expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode?)
27
28 render json: collection_presenter,
29 serializer: ActivityPub::CollectionSerializer,
30 adapter: ActivityPub::Adapter,
31 content_type: 'application/activity+json',
32 fields: restrict_fields_to
33 end
34 end
35 end
36
37 private
38
39 def follows
40 return @follows if defined?(@follows)
41
42 scope = Follow.where(target_account: @account)
43 scope = scope.where.not(account_id: current_account.excluded_from_timeline_account_ids) if user_signed_in?
44 @follows = scope.recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:account)
45 end
46
47 def page_requested?
48 params[:page].present?
49 end
50
51 def page_url(page)
52 account_followers_url(@account, page: page) unless page.nil?
53 end
54
55 def collection_presenter
56 if page_requested?
57 ActivityPub::CollectionPresenter.new(
58 id: account_followers_url(@account, page: params.fetch(:page, 1)),
59 type: :ordered,
60 size: @account.followers_count,
61 items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) },
62 part_of: account_followers_url(@account),
63 next: page_url(follows.next_page),
64 prev: page_url(follows.prev_page)
65 )
66 else
67 ActivityPub::CollectionPresenter.new(
68 id: account_followers_url(@account),
69 type: :ordered,
70 size: @account.followers_count,
71 first: page_url(1)
72 )
73 end
74 end
75
76 def restrict_fields_to
77 if page_requested? || !@account.user_hides_network?
78 # Return all fields
79 else
80 %i(id type totalItems)
81 end
82 end
83 end
This page took 0.076236 seconds and 4 git commands to generate.