collections_controller.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_items
  7. before_action :set_size
  8. before_action :set_type
  9. before_action :set_cache_headers
  10. def show
  11. expires_in 3.minutes, public: public_fetch_mode?
  12. render_with_cache json: collection_presenter, content_type: 'application/activity+json', serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, skip_activities: true
  13. end
  14. private
  15. def set_items
  16. case params[:id]
  17. when 'featured'
  18. @items = begin
  19. # Because in public fetch mode we cache the response, there would be no
  20. # benefit from performing the check below, since a blocked account or domain
  21. # would likely be served the cache from the reverse proxy anyway
  22. 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)))
  23. []
  24. else
  25. cache_collection(@account.pinned_statuses, Status)
  26. end
  27. end
  28. when 'devices'
  29. @items = @account.devices
  30. else
  31. not_found
  32. end
  33. end
  34. def set_size
  35. case params[:id]
  36. when 'featured', 'devices'
  37. @size = @items.size
  38. else
  39. not_found
  40. end
  41. end
  42. def set_type
  43. case params[:id]
  44. when 'featured'
  45. @type = :ordered
  46. when 'devices'
  47. @type = :unordered
  48. else
  49. not_found
  50. end
  51. end
  52. def collection_presenter
  53. ActivityPub::CollectionPresenter.new(
  54. id: account_collection_url(@account, params[:id]),
  55. type: @type,
  56. size: @size,
  57. items: @items
  58. )
  59. end
  60. end