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.

78 lines
2.0 KiB

7 years ago
7 years ago
7 years ago
  1. # frozen_string_literal: true
  2. class TagsController < ApplicationController
  3. include SignatureVerification
  4. PAGE_SIZE = 20
  5. PAGE_SIZE_MAX = 200
  6. layout 'public'
  7. before_action :require_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  8. before_action :authenticate_user!, if: :whitelist_mode?
  9. before_action :set_local
  10. before_action :set_tag
  11. before_action :set_statuses
  12. before_action :set_body_classes
  13. before_action :set_instance_presenter
  14. skip_before_action :require_functional!, unless: :whitelist_mode?
  15. def show
  16. respond_to do |format|
  17. format.html do
  18. expires_in 0, public: true
  19. end
  20. format.rss do
  21. expires_in 0, public: true
  22. render xml: RSS::TagSerializer.render(@tag, @statuses)
  23. end
  24. format.json do
  25. expires_in 3.minutes, public: public_fetch_mode?
  26. render json: collection_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json'
  27. end
  28. end
  29. end
  30. private
  31. def set_tag
  32. @tag = Tag.usable.find_normalized!(params[:id])
  33. end
  34. def set_local
  35. @local = truthy_param?(:local)
  36. end
  37. def set_statuses
  38. case request.format&.to_sym
  39. when :json
  40. @statuses = cache_collection(TagFeed.new(@tag, current_account, local: @local).get(PAGE_SIZE, params[:max_id], params[:since_id], params[:min_id]), Status)
  41. when :rss
  42. @statuses = cache_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status)
  43. end
  44. end
  45. def set_body_classes
  46. @body_classes = 'with-modals'
  47. end
  48. def set_instance_presenter
  49. @instance_presenter = InstancePresenter.new
  50. end
  51. def limit_param
  52. params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  53. end
  54. def collection_presenter
  55. ActivityPub::CollectionPresenter.new(
  56. id: tag_url(@tag),
  57. type: :ordered,
  58. size: @tag.statuses.count,
  59. items: @statuses.map { |s| ActivityPub::TagManager.instance.uri_for(s) }
  60. )
  61. end
  62. end