闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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