collections_controller.rb 2.0 KB

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