statuses_controller.rb 2.3 KB

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