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.

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