statuses_controller.rb 6.9 KB

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