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.

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