tags_controller.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. class TagsController < ApplicationController
  3. include SignatureVerification
  4. include WebAppControllerConcern
  5. PAGE_SIZE = 20
  6. PAGE_SIZE_MAX = 200
  7. vary_by -> { public_fetch_mode? ? 'Accept, Accept-Language, Cookie' : 'Accept, Accept-Language, Cookie, Signature' }
  8. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  9. before_action :authenticate_user!, if: :limited_federation_mode?
  10. before_action :set_local
  11. before_action :set_tag
  12. before_action :set_statuses, if: -> { request.format == :rss }
  13. skip_before_action :require_functional!, unless: :limited_federation_mode?
  14. def show
  15. respond_to do |format|
  16. format.html do
  17. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  18. end
  19. format.rss do
  20. expires_in 0, public: true
  21. end
  22. format.json do
  23. expires_in 3.minutes, public: public_fetch_mode?
  24. render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  25. end
  26. end
  27. end
  28. private
  29. def set_tag
  30. @tag = Tag.usable.find_normalized!(params[:id])
  31. end
  32. def set_local
  33. @local = truthy_param?(:local)
  34. end
  35. def set_statuses
  36. @statuses = cache_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status)
  37. end
  38. def limit_param
  39. params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  40. end
  41. def collection_presenter
  42. ActivityPub::CollectionPresenter.new(
  43. id: tag_url(@tag),
  44. type: :ordered
  45. )
  46. end
  47. end