statuses_controller.rb 2.3 KB

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