follower_accounts_controller.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # frozen_string_literal: true
  2. class FollowerAccountsController < ApplicationController
  3. include AccountControllerConcern
  4. include SignatureVerification
  5. include WebAppControllerConcern
  6. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  7. before_action :set_cache_headers
  8. skip_around_action :set_locale, if: -> { request.format == :json }
  9. skip_before_action :require_functional!, unless: :whitelist_mode?
  10. def index
  11. respond_to do |format|
  12. format.html do
  13. expires_in 0, public: true 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 { |f| ActivityPub::TagManager.instance.uri_for(f.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