statuses_controller.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. class StatusesController < ApplicationController
  3. include StatusControllerConcern
  4. include SignatureAuthentication
  5. include Authorization
  6. include AccountOwnedConcern
  7. layout 'public'
  8. before_action :require_signature!, only: :show, if: -> { request.format == :json && authorized_fetch_mode? }
  9. before_action :set_status
  10. before_action :set_instance_presenter
  11. before_action :set_link_headers
  12. before_action :redirect_to_original, only: :show
  13. before_action :set_referrer_policy_header, only: :show
  14. before_action :set_cache_headers
  15. before_action :set_body_classes
  16. before_action :set_autoplay, only: :embed
  17. skip_around_action :set_locale, if: -> { request.format == :json }
  18. skip_before_action :require_functional!, only: [:show, :embed]
  19. content_security_policy only: :embed do |p|
  20. p.frame_ancestors(false)
  21. end
  22. def show
  23. respond_to do |format|
  24. format.html do
  25. expires_in 10.seconds, public: true if current_account.nil?
  26. set_ancestors
  27. set_descendants
  28. end
  29. format.json do
  30. expires_in 3.minutes, public: @status.distributable? && public_fetch_mode?
  31. render_with_cache json: @status, content_type: 'application/activity+json', serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter
  32. end
  33. end
  34. end
  35. def activity
  36. expires_in 3.minutes, public: @status.distributable? && public_fetch_mode?
  37. render_with_cache json: @status, content_type: 'application/activity+json', serializer: ActivityPub::ActivitySerializer, adapter: ActivityPub::Adapter
  38. end
  39. def embed
  40. raise ActiveRecord::RecordNotFound if @status.hidden?
  41. expires_in 180, public: true
  42. response.headers['X-Frame-Options'] = 'ALLOWALL'
  43. render layout: 'embedded'
  44. end
  45. private
  46. def set_body_classes
  47. @body_classes = 'with-modals'
  48. end
  49. def set_link_headers
  50. response.headers['Link'] = LinkHeader.new([[ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]]])
  51. end
  52. def set_status
  53. @status = @account.statuses.find(params[:id])
  54. authorize @status, :show?
  55. rescue Mastodon::NotPermittedError
  56. raise ActiveRecord::RecordNotFound
  57. end
  58. def set_instance_presenter
  59. @instance_presenter = InstancePresenter.new
  60. end
  61. def redirect_to_original
  62. redirect_to ActivityPub::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  63. end
  64. def set_referrer_policy_header
  65. response.headers['Referrer-Policy'] = 'origin' unless @status.distributable?
  66. end
  67. def set_autoplay
  68. @autoplay = truthy_param?(:autoplay)
  69. end
  70. end