following_accounts_controller.rb 2.5 KB

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