tags_controller.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. class TagsController < ApplicationController
  3. include SignatureVerification
  4. PAGE_SIZE = 20
  5. layout 'public'
  6. before_action :require_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  7. before_action :authenticate_user!, if: :whitelist_mode?
  8. before_action :set_tag
  9. before_action :set_body_classes
  10. before_action :set_instance_presenter
  11. def show
  12. respond_to do |format|
  13. format.html do
  14. expires_in 0, public: true
  15. @initial_state_json = ActiveModelSerializers::SerializableResource.new(
  16. InitialStatePresenter.new(settings: {}, token: current_session&.token),
  17. serializer: InitialStateSerializer
  18. ).to_json
  19. end
  20. format.rss do
  21. expires_in 0, public: true
  22. @statuses = HashtagQueryService.new.call(@tag, params.slice(:any, :all, :none)).limit(PAGE_SIZE)
  23. @statuses = cache_collection(@statuses, Status)
  24. render xml: RSS::TagSerializer.render(@tag, @statuses)
  25. end
  26. format.json do
  27. expires_in 3.minutes, public: public_fetch_mode?
  28. @statuses = HashtagQueryService.new.call(@tag, params.slice(:any, :all, :none), current_account, params[:local]).paginate_by_max_id(PAGE_SIZE, params[:max_id])
  29. @statuses = cache_collection(@statuses, Status)
  30. render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  31. end
  32. end
  33. end
  34. private
  35. def set_tag
  36. @tag = Tag.find_normalized!(params[:id])
  37. end
  38. def set_body_classes
  39. @body_classes = 'with-modals'
  40. end
  41. def set_instance_presenter
  42. @instance_presenter = InstancePresenter.new
  43. end
  44. def collection_presenter
  45. ActivityPub::CollectionPresenter.new(
  46. id: tag_url(@tag, params.slice(:any, :all, :none)),
  47. type: :ordered,
  48. size: @tag.statuses.count,
  49. items: @statuses.map { |s| ActivityPub::TagManager.instance.uri_for(s) }
  50. )
  51. end
  52. end