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.

73 lines
1.9 KiB

  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
  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. when 'tags'
  20. @items = for_signed_account { @account.featured_tags }
  21. when 'devices'
  22. @items = @account.devices
  23. else
  24. not_found
  25. end
  26. end
  27. def set_size
  28. case params[:id]
  29. when 'featured', 'devices', 'tags'
  30. @size = @items.size
  31. else
  32. not_found
  33. end
  34. end
  35. def set_type
  36. case params[:id]
  37. when 'featured'
  38. @type = :ordered
  39. when 'devices', 'tags'
  40. @type = :unordered
  41. else
  42. not_found
  43. end
  44. end
  45. def collection_presenter
  46. ActivityPub::CollectionPresenter.new(
  47. id: account_collection_url(@account, params[:id]),
  48. type: @type,
  49. size: @size,
  50. items: @items
  51. )
  52. end
  53. def for_signed_account
  54. # Because in public fetch mode we cache the response, there would be no
  55. # benefit from performing the check below, since a blocked account or domain
  56. # would likely be served the cache from the reverse proxy anyway
  57. 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)))
  58. []
  59. else
  60. yield
  61. end
  62. end
  63. end