1
0

statuses_controller.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 :set_instance_presenter
  11. before_action :redirect_to_original, only: :show
  12. before_action :set_body_classes, only: :embed
  13. after_action :set_link_headers
  14. skip_around_action :set_locale, if: -> { request.format == :json }
  15. skip_before_action :require_functional!, only: [:show, :embed], unless: :limited_federation_mode?
  16. content_security_policy only: :embed do |policy|
  17. policy.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: true if @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), allow_other_host: true) if @status.reblog?
  58. end
  59. end