follower_accounts_controller.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if params[:page].blank?
  16. skip_session!
  17. expires_in 3.minutes, public: true
  18. end
  19. render json: collection_presenter,
  20. serializer: ActivityPub::CollectionSerializer,
  21. adapter: ActivityPub::Adapter,
  22. content_type: 'application/activity+json'
  23. end
  24. end
  25. end
  26. private
  27. def follows
  28. @follows ||= Follow.where(target_account: @account).recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:account)
  29. end
  30. def page_url(page)
  31. account_followers_url(@account, page: page) unless page.nil?
  32. end
  33. def collection_presenter
  34. if params[:page].present?
  35. ActivityPub::CollectionPresenter.new(
  36. id: account_followers_url(@account, page: params.fetch(:page, 1)),
  37. type: :ordered,
  38. size: @account.followers_count,
  39. items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) },
  40. part_of: account_followers_url(@account),
  41. next: page_url(follows.next_page),
  42. prev: page_url(follows.prev_page)
  43. )
  44. else
  45. ActivityPub::CollectionPresenter.new(
  46. id: account_followers_url(@account),
  47. type: :ordered,
  48. size: @account.followers_count,
  49. first: page_url(1)
  50. )
  51. end
  52. end
  53. end