tags_controller.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. before_action :set_instance_presenter
  14. skip_before_action :require_functional!, unless: :limited_federation_mode?
  15. def show
  16. respond_to do |format|
  17. format.html do
  18. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  19. end
  20. format.rss do
  21. expires_in 0, public: true
  22. end
  23. format.json do
  24. expires_in 3.minutes, public: public_fetch_mode?
  25. render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  26. end
  27. end
  28. end
  29. private
  30. def set_tag
  31. @tag = Tag.usable.find_normalized!(params[:id])
  32. end
  33. def set_local
  34. @local = truthy_param?(:local)
  35. end
  36. def set_statuses
  37. @statuses = cache_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status)
  38. end
  39. def set_instance_presenter
  40. @instance_presenter = InstancePresenter.new
  41. end
  42. def limit_param
  43. params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  44. end
  45. def collection_presenter
  46. ActivityPub::CollectionPresenter.new(
  47. id: tag_url(@tag),
  48. type: :ordered
  49. )
  50. end
  51. end