follower_accounts_controller.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. class FollowerAccountsController < ApplicationController
  3. include AccountControllerConcern
  4. include SignatureVerification
  5. include WebAppControllerConcern
  6. vary_by -> { public_fetch_mode? ? 'Accept, Accept-Language, Cookie' : 'Accept, Accept-Language, Cookie, Signature' }
  7. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  8. skip_around_action :set_locale, if: -> { request.format == :json }
  9. skip_before_action :require_functional!, unless: :limited_federation_mode?
  10. def index
  11. respond_to do |format|
  12. format.html do
  13. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  14. end
  15. format.json do
  16. raise Mastodon::NotPermittedError if page_requested? && @account.hide_collections?
  17. expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode?)
  18. render json: collection_presenter,
  19. serializer: ActivityPub::CollectionSerializer,
  20. adapter: ActivityPub::Adapter,
  21. content_type: 'application/activity+json',
  22. fields: restrict_fields_to
  23. end
  24. end
  25. end
  26. private
  27. def follows
  28. return @follows if defined?(@follows)
  29. scope = Follow.where(target_account: @account)
  30. scope = scope.where.not(account_id: current_account.excluded_from_timeline_account_ids) if user_signed_in?
  31. @follows = scope.recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:account)
  32. end
  33. def page_requested?
  34. params[:page].present?
  35. end
  36. def page_url(page)
  37. account_followers_url(@account, page: page) unless page.nil?
  38. end
  39. def next_page_url
  40. page_url(follows.next_page) if follows.respond_to?(:next_page)
  41. end
  42. def prev_page_url
  43. page_url(follows.prev_page) if follows.respond_to?(:prev_page)
  44. end
  45. def collection_presenter
  46. if page_requested?
  47. ActivityPub::CollectionPresenter.new(
  48. id: account_followers_url(@account, page: params.fetch(:page, 1)),
  49. type: :ordered,
  50. size: @account.followers_count,
  51. items: follows.map { |follow| ActivityPub::TagManager.instance.uri_for(follow.account) },
  52. part_of: account_followers_url(@account),
  53. next: next_page_url,
  54. prev: prev_page_url
  55. )
  56. else
  57. ActivityPub::CollectionPresenter.new(
  58. id: account_followers_url(@account),
  59. type: :ordered,
  60. size: @account.followers_count,
  61. first: page_url(1)
  62. )
  63. end
  64. end
  65. def restrict_fields_to
  66. if page_requested? || !@account.hide_collections?
  67. # Return all fields
  68. else
  69. %i(id type total_items)
  70. end
  71. end
  72. end