follower_accounts_controller.rb 2.6 KB

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