collections_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. class ActivityPub::CollectionsController < ActivityPub::BaseController
  3. include SignatureVerification
  4. include AccountOwnedConcern
  5. before_action :require_account_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
  13. end
  14. private
  15. def set_items
  16. case params[:id]
  17. when 'featured'
  18. @items = for_signed_account { cache_collection(@account.pinned_statuses, Status) }
  19. @items = @items.map { |item| item.distributable? ? item : ActivityPub::TagManager.instance.uri_for(item) }
  20. when 'tags'
  21. @items = for_signed_account { @account.featured_tags }
  22. when 'devices'
  23. @items = @account.devices
  24. else
  25. not_found
  26. end
  27. end
  28. def set_size
  29. case params[:id]
  30. when 'featured', 'devices', 'tags'
  31. @size = @items.size
  32. else
  33. not_found
  34. end
  35. end
  36. def set_type
  37. case params[:id]
  38. when 'featured'
  39. @type = :ordered
  40. when 'devices', 'tags'
  41. @type = :unordered
  42. else
  43. not_found
  44. end
  45. end
  46. def collection_presenter
  47. ActivityPub::CollectionPresenter.new(
  48. id: account_collection_url(@account, params[:id]),
  49. type: @type,
  50. size: @size,
  51. items: @items
  52. )
  53. end
  54. def for_signed_account
  55. # Because in public fetch mode we cache the response, there would be no
  56. # benefit from performing the check below, since a blocked account or domain
  57. # would likely be served the cache from the reverse proxy anyway
  58. 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)))
  59. []
  60. else
  61. yield
  62. end
  63. end
  64. end