following_accounts_controller.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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!, unless: :whitelist_mode?
  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 next_page_url
  41. page_url(follows.next_page) if follows.respond_to?(:next_page)
  42. end
  43. def prev_page_url
  44. page_url(follows.prev_page) if follows.respond_to?(:prev_page)
  45. end
  46. def collection_presenter
  47. if page_requested?
  48. ActivityPub::CollectionPresenter.new(
  49. id: account_following_index_url(@account, page: params.fetch(:page, 1)),
  50. type: :ordered,
  51. size: @account.following_count,
  52. items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) },
  53. part_of: account_following_index_url(@account),
  54. next: next_page_url,
  55. prev: prev_page_url
  56. )
  57. else
  58. ActivityPub::CollectionPresenter.new(
  59. id: account_following_index_url(@account),
  60. type: :ordered,
  61. size: @account.following_count,
  62. first: page_url(1)
  63. )
  64. end
  65. end
  66. def restrict_fields_to
  67. if page_requested? || !@account.user_hides_network?
  68. # Return all fields
  69. else
  70. %i(id type total_items)
  71. end
  72. end
  73. end