collections_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # frozen_string_literal: true
  2. class ActivityPub::CollectionsController < ActivityPub::BaseController
  3. include SignatureVerification
  4. include AccountOwnedConcern
  5. before_action :require_signature!, if: :authorized_fetch_mode?
  6. before_action :set_size
  7. before_action :set_statuses
  8. before_action :set_cache_headers
  9. def show
  10. expires_in 3.minutes, public: public_fetch_mode?
  11. render_with_cache json: collection_presenter, content_type: 'application/activity+json', serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, skip_activities: true
  12. end
  13. private
  14. def set_statuses
  15. @statuses = scope_for_collection
  16. @statuses = cache_collection(@statuses, Status)
  17. end
  18. def set_size
  19. case params[:id]
  20. when 'featured'
  21. @size = @account.pinned_statuses.count
  22. else
  23. not_found
  24. end
  25. end
  26. def scope_for_collection
  27. case params[:id]
  28. when 'featured'
  29. # Because in public fetch mode we cache the response, there would be no
  30. # benefit from performing the check below, since a blocked account or domain
  31. # would likely be served the cache from the reverse proxy anyway
  32. if authorized_fetch_mode? && !signed_request_account.nil? && (@account.blocking?(signed_request_account) || (!signed_request_account.domain.nil? && @account.domain_blocking?(signed_request_account.domain)))
  33. Status.none
  34. else
  35. @account.pinned_statuses
  36. end
  37. end
  38. end
  39. def collection_presenter
  40. ActivityPub::CollectionPresenter.new(
  41. id: account_collection_url(@account, params[:id]),
  42. type: :ordered,
  43. size: @size,
  44. items: @statuses
  45. )
  46. end
  47. end