statuses_controller.rb 2.3 KB

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