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.

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