following_accounts_controller.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. end
  16. format.json do
  17. raise Mastodon::NotPermittedError if page_requested? && @account.user_hides_network?
  18. expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode?)
  19. render json: collection_presenter,
  20. serializer: ActivityPub::CollectionSerializer,
  21. adapter: ActivityPub::Adapter,
  22. content_type: 'application/activity+json',
  23. fields: restrict_fields_to
  24. end
  25. end
  26. end
  27. private
  28. def follows
  29. return @follows if defined?(@follows)
  30. scope = Follow.where(account: @account)
  31. scope = scope.where.not(target_account_id: current_account.excluded_from_timeline_account_ids) if user_signed_in?
  32. @follows = scope.recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:target_account)
  33. end
  34. def page_requested?
  35. params[:page].present?
  36. end
  37. def page_url(page)
  38. account_following_index_url(@account, page: page) unless page.nil?
  39. end
  40. def collection_presenter
  41. if page_requested?
  42. ActivityPub::CollectionPresenter.new(
  43. id: account_following_index_url(@account, page: params.fetch(:page, 1)),
  44. type: :ordered,
  45. size: @account.following_count,
  46. items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) },
  47. part_of: account_following_index_url(@account),
  48. next: page_url(follows.next_page),
  49. prev: page_url(follows.prev_page)
  50. )
  51. else
  52. ActivityPub::CollectionPresenter.new(
  53. id: account_following_index_url(@account),
  54. type: :ordered,
  55. size: @account.following_count,
  56. first: page_url(1)
  57. )
  58. end
  59. end
  60. def restrict_fields_to
  61. if page_requested? || !@account.user_hides_network?
  62. # Return all fields
  63. else
  64. %i(id type totalItems)
  65. end
  66. end
  67. end