statuses_controller.rb 7.0 KB

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