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.

225 lines
6.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. before_action :set_replies, only: [:replies]
  18. content_security_policy only: :embed do |p|
  19. p.frame_ancestors(false)
  20. end
  21. def show
  22. respond_to do |format|
  23. format.html do
  24. expires_in 10.seconds, public: true if current_account.nil?
  25. @body_classes = 'with-modals'
  26. set_ancestors
  27. set_descendants
  28. render 'stream_entries/show'
  29. end
  30. format.json do
  31. render_cached_json(['activitypub', 'note', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  32. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)
  33. end
  34. end
  35. end
  36. end
  37. def activity
  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. expires_in 180, public: true
  45. response.headers['X-Frame-Options'] = 'ALLOWALL'
  46. @autoplay = ActiveModel::Type::Boolean.new.cast(params[:autoplay])
  47. render 'stream_entries/embed', layout: 'embedded'
  48. end
  49. def replies
  50. render json: replies_collection_presenter,
  51. serializer: ActivityPub::CollectionSerializer,
  52. adapter: ActivityPub::Adapter,
  53. content_type: 'application/activity+json',
  54. skip_activities: true
  55. end
  56. private
  57. def replies_collection_presenter
  58. page = ActivityPub::CollectionPresenter.new(
  59. id: replies_account_status_url(@account, @status, page_params),
  60. type: :unordered,
  61. part_of: replies_account_status_url(@account, @status),
  62. next: next_page,
  63. items: @replies.map { |status| status.local ? status : status.id }
  64. )
  65. if page_requested?
  66. page
  67. else
  68. ActivityPub::CollectionPresenter.new(
  69. id: replies_account_status_url(@account, @status),
  70. type: :unordered,
  71. first: page
  72. )
  73. end
  74. end
  75. def create_descendant_thread(starting_depth, statuses)
  76. depth = starting_depth + statuses.size
  77. if depth < DESCENDANTS_DEPTH_LIMIT
  78. { statuses: statuses, starting_depth: starting_depth }
  79. else
  80. next_status = statuses.pop
  81. { statuses: statuses, starting_depth: starting_depth, next_status: next_status }
  82. end
  83. end
  84. def set_account
  85. @account = Account.find_local!(params[:account_username])
  86. end
  87. def set_ancestors
  88. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  89. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  90. end
  91. def set_descendants
  92. @max_descendant_thread_id = params[:max_descendant_thread_id]&.to_i
  93. @since_descendant_thread_id = params[:since_descendant_thread_id]&.to_i
  94. descendants = cache_collection(
  95. @status.descendants(
  96. DESCENDANTS_LIMIT,
  97. current_account,
  98. @max_descendant_thread_id,
  99. @since_descendant_thread_id,
  100. DESCENDANTS_DEPTH_LIMIT
  101. ),
  102. Status
  103. )
  104. @descendant_threads = []
  105. if descendants.present?
  106. statuses = [descendants.first]
  107. starting_depth = 0
  108. descendants.drop(1).each_with_index do |descendant, index|
  109. if descendants[index].id == descendant.in_reply_to_id
  110. statuses << descendant
  111. else
  112. @descendant_threads << create_descendant_thread(starting_depth, statuses)
  113. # The thread is broken, assume it's a reply to the root status
  114. starting_depth = 0
  115. # ... unless we can find its ancestor in one of the already-processed threads
  116. @descendant_threads.reverse_each do |descendant_thread|
  117. statuses = descendant_thread[:statuses]
  118. index = statuses.find_index do |thread_status|
  119. thread_status.id == descendant.in_reply_to_id
  120. end
  121. if index.present?
  122. starting_depth = descendant_thread[:starting_depth] + index + 1
  123. break
  124. end
  125. end
  126. statuses = [descendant]
  127. end
  128. end
  129. @descendant_threads << create_descendant_thread(starting_depth, statuses)
  130. end
  131. @max_descendant_thread_id = @descendant_threads.pop[:statuses].first.id if descendants.size >= DESCENDANTS_LIMIT
  132. end
  133. def set_link_headers
  134. response.headers['Link'] = LinkHeader.new(
  135. [
  136. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  137. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  138. ]
  139. )
  140. end
  141. def set_status
  142. @status = @account.statuses.find(params[:id])
  143. @stream_entry = @status.stream_entry
  144. @type = @stream_entry.activity_type.downcase
  145. authorize @status, :show?
  146. rescue Mastodon::NotPermittedError
  147. # Reraise in order to get a 404
  148. raise ActiveRecord::RecordNotFound
  149. end
  150. def set_instance_presenter
  151. @instance_presenter = InstancePresenter.new
  152. end
  153. def check_account_suspension
  154. gone if @account.suspended?
  155. end
  156. def redirect_to_original
  157. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  158. end
  159. def set_referrer_policy_header
  160. return if @status.public_visibility? || @status.unlisted_visibility?
  161. response.headers['Referrer-Policy'] = 'origin'
  162. end
  163. def page_requested?
  164. params[:page] == 'true'
  165. end
  166. def set_replies
  167. @replies = page_params[:other_accounts] ? Status.where.not(account_id: @account.id) : @account.statuses
  168. @replies = @replies.where(in_reply_to_id: @status.id, visibility: [:public, :unlisted])
  169. @replies = @replies.paginate_by_min_id(DESCENDANTS_LIMIT, params[:min_id])
  170. end
  171. def next_page
  172. last_reply = @replies.last
  173. return if last_reply.nil?
  174. same_account = last_reply.account_id == @account.id
  175. return unless same_account || @replies.size == DESCENDANTS_LIMIT
  176. same_account = false unless @replies.size == DESCENDANTS_LIMIT
  177. replies_account_status_url(@account, @status, page: true, min_id: last_reply.id, other_accounts: !same_account)
  178. end
  179. def page_params
  180. { page: true, other_accounts: params[:other_accounts], min_id: params[:min_id] }.compact
  181. end
  182. end