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.

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