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.

175 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. @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(depth, statuses)
  52. if depth < DESCENDANTS_DEPTH_LIMIT
  53. { statuses: statuses }
  54. else
  55. next_status = statuses.pop
  56. { statuses: statuses, next_status: next_status }
  57. end
  58. end
  59. def set_account
  60. @account = Account.find_local!(params[:account_username])
  61. end
  62. def set_ancestors
  63. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  64. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  65. end
  66. def set_descendants
  67. @max_descendant_thread_id = params[:max_descendant_thread_id]&.to_i
  68. @since_descendant_thread_id = params[:since_descendant_thread_id]&.to_i
  69. descendants = cache_collection(
  70. @status.descendants(
  71. DESCENDANTS_LIMIT,
  72. current_account,
  73. @max_descendant_thread_id,
  74. @since_descendant_thread_id,
  75. DESCENDANTS_DEPTH_LIMIT
  76. ),
  77. Status
  78. )
  79. @descendant_threads = []
  80. if descendants.present?
  81. statuses = [descendants.first]
  82. depth = 1
  83. descendants.drop(1).each_with_index do |descendant, index|
  84. if descendants[index].id == descendant.in_reply_to_id
  85. depth += 1
  86. statuses << descendant
  87. else
  88. @descendant_threads << create_descendant_thread(depth, statuses)
  89. @descendant_threads.reverse_each do |descendant_thread|
  90. statuses = descendant_thread[:statuses]
  91. index = statuses.find_index do |thread_status|
  92. thread_status.id == descendant.in_reply_to_id
  93. end
  94. if index.present?
  95. depth += index - statuses.size
  96. break
  97. end
  98. depth -= statuses.size
  99. end
  100. statuses = [descendant]
  101. end
  102. end
  103. @descendant_threads << create_descendant_thread(depth, statuses)
  104. end
  105. @max_descendant_thread_id = @descendant_threads.pop[:statuses].first.id if descendants.size >= DESCENDANTS_LIMIT
  106. end
  107. def set_link_headers
  108. response.headers['Link'] = LinkHeader.new(
  109. [
  110. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  111. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  112. ]
  113. )
  114. end
  115. def set_status
  116. @status = @account.statuses.find(params[:id])
  117. @stream_entry = @status.stream_entry
  118. @type = @stream_entry.activity_type.downcase
  119. authorize @status, :show?
  120. rescue Mastodon::NotPermittedError
  121. # Reraise in order to get a 404
  122. raise ActiveRecord::RecordNotFound
  123. end
  124. def set_instance_presenter
  125. @instance_presenter = InstancePresenter.new
  126. end
  127. def check_account_suspension
  128. gone if @account.suspended?
  129. end
  130. def redirect_to_original
  131. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  132. end
  133. def set_referrer_policy_header
  134. return if @status.public_visibility? || @status.unlisted_visibility?
  135. response.headers['Referrer-Policy'] = 'origin'
  136. end
  137. end