1
0

following_accounts_controller.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. class FollowingAccountsController < ApplicationController
  3. include AccountControllerConcern
  4. include SignatureVerification
  5. vary_by -> { public_fetch_mode? ? 'Accept, Accept-Language, Cookie' : 'Accept, Accept-Language, Cookie, Signature' }
  6. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  7. skip_around_action :set_locale, if: -> { request.format == :json }
  8. skip_before_action :require_functional!, unless: :limited_federation_mode?
  9. def index
  10. respond_to do |format|
  11. format.html do
  12. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  13. end
  14. format.json do
  15. if page_requested? && @account.hide_collections?
  16. forbidden
  17. next
  18. end
  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. fields: restrict_fields_to
  25. end
  26. end
  27. end
  28. private
  29. def follows
  30. return @follows if defined?(@follows)
  31. scope = Follow.where(account: @account)
  32. scope = scope.where.not(target_account_id: current_account.excluded_from_timeline_account_ids) if user_signed_in?
  33. @follows = scope.recent.page(params[:page]).per(FOLLOW_PER_PAGE).preload(:target_account)
  34. end
  35. def page_requested?
  36. params[:page].present?
  37. end
  38. def page_url(page)
  39. account_following_index_url(@account, page: page) unless page.nil?
  40. end
  41. def next_page_url
  42. page_url(follows.next_page) if follows.respond_to?(:next_page)
  43. end
  44. def prev_page_url
  45. page_url(follows.prev_page) if follows.respond_to?(:prev_page)
  46. end
  47. def collection_presenter
  48. if page_requested?
  49. ActivityPub::CollectionPresenter.new(
  50. id: account_following_index_url(@account, page: params.fetch(:page, 1)),
  51. type: :ordered,
  52. size: @account.following_count,
  53. items: follows.map { |follow| ActivityPub::TagManager.instance.uri_for(follow.target_account) },
  54. part_of: account_following_index_url(@account),
  55. next: next_page_url,
  56. prev: prev_page_url
  57. )
  58. else
  59. ActivityPub::CollectionPresenter.new(
  60. id: account_following_index_url(@account),
  61. type: :ordered,
  62. size: @account.following_count,
  63. first: page_url(1)
  64. )
  65. end
  66. end
  67. def restrict_fields_to
  68. if page_requested? || !@account.hide_collections?
  69. # Return all fields
  70. else
  71. %i(id type total_items)
  72. end
  73. end
  74. end