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.

71 lines
1.9 KiB

7 years ago
7 years ago
7 years ago
  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. before_action :require_account_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_instance_presenter
  13. skip_before_action :require_functional!, unless: :whitelist_mode?
  14. def show
  15. respond_to do |format|
  16. format.html do
  17. expires_in 0, public: true 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. case request.format&.to_sym
  37. when :json
  38. @statuses = cache_collection(TagFeed.new(@tag, current_account, local: @local).get(PAGE_SIZE, params[:max_id], params[:since_id], params[:min_id]), Status)
  39. when :rss
  40. @statuses = cache_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status)
  41. end
  42. end
  43. def set_instance_presenter
  44. @instance_presenter = InstancePresenter.new
  45. end
  46. def limit_param
  47. params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  48. end
  49. def collection_presenter
  50. ActivityPub::CollectionPresenter.new(
  51. id: tag_url(@tag),
  52. type: :ordered,
  53. size: @tag.statuses.count,
  54. items: @statuses.map { |status| ActivityPub::TagManager.instance.uri_for(status) }
  55. )
  56. end
  57. end