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
4.9 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(depth, statuses)
  54. if depth < DESCENDANTS_DEPTH_LIMIT
  55. { statuses: statuses }
  56. else
  57. next_status = statuses.pop
  58. { statuses: statuses, next_status: next_status }
  59. end
  60. end
  61. def set_account
  62. @account = Account.find_local!(params[:account_username])
  63. end
  64. def set_ancestors
  65. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  66. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  67. end
  68. def set_descendants
  69. @max_descendant_thread_id = params[:max_descendant_thread_id]&.to_i
  70. @since_descendant_thread_id = params[:since_descendant_thread_id]&.to_i
  71. descendants = cache_collection(
  72. @status.descendants(
  73. DESCENDANTS_LIMIT,
  74. current_account,
  75. @max_descendant_thread_id,
  76. @since_descendant_thread_id,
  77. DESCENDANTS_DEPTH_LIMIT
  78. ),
  79. Status
  80. )
  81. @descendant_threads = []
  82. if descendants.present?
  83. statuses = [descendants.first]
  84. depth = 1
  85. descendants.drop(1).each_with_index do |descendant, index|
  86. if descendants[index].id == descendant.in_reply_to_id
  87. depth += 1
  88. statuses << descendant
  89. else
  90. @descendant_threads << create_descendant_thread(depth, statuses)
  91. @descendant_threads.reverse_each do |descendant_thread|
  92. statuses = descendant_thread[:statuses]
  93. index = statuses.find_index do |thread_status|
  94. thread_status.id == descendant.in_reply_to_id
  95. end
  96. if index.present?
  97. depth += index - statuses.size
  98. break
  99. end
  100. depth -= statuses.size
  101. end
  102. statuses = [descendant]
  103. end
  104. end
  105. @descendant_threads << create_descendant_thread(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