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.

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