You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.8 KiB

  1. # frozen_string_literal: true
  2. class StatusesController < ApplicationController
  3. include SignatureAuthentication
  4. include Authorization
  5. ANCESTORS_LIMIT = 20
  6. layout 'public'
  7. before_action :set_account
  8. before_action :set_status
  9. before_action :set_link_headers
  10. before_action :check_account_suspension
  11. before_action :redirect_to_original, only: [:show]
  12. before_action :set_referrer_policy_header, only: [:show]
  13. before_action :set_cache_headers
  14. def show
  15. respond_to do |format|
  16. format.html do
  17. use_pack 'public'
  18. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  19. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  20. @descendants = cache_collection(@status.descendants(current_account), Status)
  21. render 'stream_entries/show'
  22. end
  23. format.json do
  24. skip_session! unless @stream_entry.hidden?
  25. render_cached_json(['activitypub', 'note', @status.cache_key], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  26. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)
  27. end
  28. end
  29. end
  30. end
  31. def activity
  32. skip_session!
  33. render_cached_json(['activitypub', 'activity', @status.cache_key], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  34. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::ActivitySerializer, adapter: ActivityPub::Adapter)
  35. end
  36. end
  37. def embed
  38. use_pack 'embed'
  39. response.headers['X-Frame-Options'] = 'ALLOWALL'
  40. render 'stream_entries/embed', layout: 'embedded'
  41. end
  42. private
  43. def set_account
  44. @account = Account.find_local!(params[:account_username])
  45. end
  46. def set_link_headers
  47. response.headers['Link'] = LinkHeader.new(
  48. [
  49. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  50. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  51. ]
  52. )
  53. end
  54. def set_status
  55. @status = @account.statuses.find(params[:id])
  56. @stream_entry = @status.stream_entry
  57. @type = @stream_entry.activity_type.downcase
  58. authorize @status, :show?
  59. rescue Mastodon::NotPermittedError
  60. # Reraise in order to get a 404
  61. raise ActiveRecord::RecordNotFound
  62. end
  63. def check_account_suspension
  64. gone if @account.suspended?
  65. end
  66. def redirect_to_original
  67. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  68. end
  69. def set_referrer_policy_header
  70. return if @status.public_visibility? || @status.unlisted_visibility?
  71. response.headers['Referrer-Policy'] = 'origin'
  72. end
  73. end