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.

177 lines
5.2 KiB

  1. # frozen_string_literal: true
  2. class StatusesController < ApplicationController
  3. include SignatureAuthentication
  4. include Authorization
  5. ANCESTORS_LIMIT = 40
  6. DESCENDANTS_LIMIT = 60
  7. DESCENDANTS_DEPTH_LIMIT = 20
  8. layout 'public'
  9. before_action :set_account
  10. before_action :set_status
  11. before_action :set_instance_presenter
  12. before_action :set_link_headers
  13. before_action :check_account_suspension
  14. before_action :redirect_to_original, only: [:show]
  15. before_action :set_referrer_policy_header, only: [:show]
  16. before_action :set_cache_headers
  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. @body_classes = 'with-modals'
  24. set_ancestors
  25. set_descendants
  26. render 'stream_entries/show'
  27. end
  28. format.json do
  29. skip_session! unless @stream_entry.hidden?
  30. render_cached_json(['activitypub', 'note', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  31. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)
  32. end
  33. end
  34. end
  35. end
  36. def activity
  37. skip_session!
  38. render_cached_json(['activitypub', 'activity', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  39. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::ActivitySerializer, adapter: ActivityPub::Adapter)
  40. end
  41. end
  42. def embed
  43. raise ActiveRecord::RecordNotFound if @status.hidden?
  44. skip_session!
  45. expires_in 180, public: true
  46. response.headers['X-Frame-Options'] = 'ALLOWALL'
  47. @autoplay = ActiveModel::Type::Boolean.new.cast(params[:autoplay])
  48. render 'stream_entries/embed', layout: 'embedded'
  49. end
  50. private
  51. def create_descendant_thread(starting_depth, statuses)
  52. depth = starting_depth + statuses.size
  53. if depth < DESCENDANTS_DEPTH_LIMIT
  54. { statuses: statuses, starting_depth: starting_depth }
  55. else
  56. next_status = statuses.pop
  57. { statuses: statuses, starting_depth: starting_depth, next_status: next_status }
  58. end
  59. end
  60. def set_account
  61. @account = Account.find_local!(params[:account_username])
  62. end
  63. def set_ancestors
  64. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  65. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  66. end
  67. def set_descendants
  68. @max_descendant_thread_id = params[:max_descendant_thread_id]&.to_i
  69. @since_descendant_thread_id = params[:since_descendant_thread_id]&.to_i
  70. descendants = cache_collection(
  71. @status.descendants(
  72. DESCENDANTS_LIMIT,
  73. current_account,
  74. @max_descendant_thread_id,
  75. @since_descendant_thread_id,
  76. DESCENDANTS_DEPTH_LIMIT
  77. ),
  78. Status
  79. )
  80. @descendant_threads = []
  81. if descendants.present?
  82. statuses = [descendants.first]
  83. starting_depth = 0
  84. descendants.drop(1).each_with_index do |descendant, index|
  85. if descendants[index].id == descendant.in_reply_to_id
  86. statuses << descendant
  87. else
  88. @descendant_threads << create_descendant_thread(starting_depth, statuses)
  89. # The thread is broken, assume it's a reply to the root status
  90. starting_depth = 0
  91. # ... unless we can find its ancestor in one of the already-processed threads
  92. @descendant_threads.reverse_each do |descendant_thread|
  93. statuses = descendant_thread[:statuses]
  94. index = statuses.find_index do |thread_status|
  95. thread_status.id == descendant.in_reply_to_id
  96. end
  97. if index.present?
  98. starting_depth = descendant_thread[:starting_depth] + index + 1
  99. break
  100. end
  101. end
  102. statuses = [descendant]
  103. end
  104. end
  105. @descendant_threads << create_descendant_thread(starting_depth, statuses)
  106. end
  107. @max_descendant_thread_id = @descendant_threads.pop[:statuses].first.id if descendants.size >= DESCENDANTS_LIMIT
  108. end
  109. def set_link_headers
  110. response.headers['Link'] = LinkHeader.new(
  111. [
  112. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  113. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  114. ]
  115. )
  116. end
  117. def set_status
  118. @status = @account.statuses.find(params[:id])
  119. @stream_entry = @status.stream_entry
  120. @type = @stream_entry.activity_type.downcase
  121. authorize @status, :show?
  122. rescue Mastodon::NotPermittedError
  123. # Reraise in order to get a 404
  124. raise ActiveRecord::RecordNotFound
  125. end
  126. def set_instance_presenter
  127. @instance_presenter = InstancePresenter.new
  128. end
  129. def check_account_suspension
  130. gone if @account.suspended?
  131. end
  132. def redirect_to_original
  133. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  134. end
  135. def set_referrer_policy_header
  136. return if @status.public_visibility? || @status.unlisted_visibility?
  137. response.headers['Referrer-Policy'] = 'origin'
  138. end
  139. end