follower_accounts_controller.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. class FollowerAccountsController < ApplicationController
  3. include AccountControllerConcern
  4. before_action :set_cache_headers
  5. def index
  6. respond_to do |format|
  7. format.html do
  8. mark_cacheable! unless user_signed_in?
  9. next if @account.user_hides_network?
  10. follows
  11. @relationships = AccountRelationshipsPresenter.new(follows.map(&:account_id), current_user.account_id) if user_signed_in?
  12. end
  13. format.json do
  14. raise Mastodon::NotPermittedError if params[:page].present? && @account.user_hides_network?
  15. expires_in 3.minutes, public: true if params[:page].blank?
  16. render json: collection_presenter,
  17. serializer: ActivityPub::CollectionSerializer,
  18. adapter: ActivityPub::Adapter,
  19. content_type: 'application/activity+json'
  20. end
  21. end
  22. end
  23. private
  24. def follows
  25. @follows ||= Follow.where(target_account: @account).recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:account)
  26. end
  27. def page_url(page)
  28. account_followers_url(@account, page: page) unless page.nil?
  29. end
  30. def collection_presenter
  31. if params[:page].present?
  32. ActivityPub::CollectionPresenter.new(
  33. id: account_followers_url(@account, page: params.fetch(:page, 1)),
  34. type: :ordered,
  35. size: @account.followers_count,
  36. items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) },
  37. part_of: account_followers_url(@account),
  38. next: page_url(follows.next_page),
  39. prev: page_url(follows.prev_page)
  40. )
  41. else
  42. ActivityPub::CollectionPresenter.new(
  43. id: account_followers_url(@account),
  44. type: :ordered,
  45. size: @account.followers_count,
  46. first: page_url(1)
  47. )
  48. end
  49. end
  50. end