You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

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