following_accounts_controller.rb 2.1 KB

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