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.

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