The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

169 lines
4.7 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. def show
  18. respond_to do |format|
  19. format.html do
  20. set_ancestors
  21. set_descendants
  22. render 'stream_entries/show'
  23. end
  24. format.json do
  25. skip_session! unless @stream_entry.hidden?
  26. render_cached_json(['activitypub', 'note', @status.cache_key], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  27. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)
  28. end
  29. end
  30. end
  31. end
  32. def activity
  33. skip_session!
  34. render_cached_json(['activitypub', 'activity', @status.cache_key], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
  35. ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::ActivitySerializer, adapter: ActivityPub::Adapter)
  36. end
  37. end
  38. def embed
  39. raise ActiveRecord::RecordNotFound if @status.hidden?
  40. skip_session!
  41. expires_in 180, public: true
  42. response.headers['X-Frame-Options'] = 'ALLOWALL'
  43. render 'stream_entries/embed', layout: 'embedded'
  44. end
  45. private
  46. def create_descendant_thread(depth, statuses)
  47. if depth < DESCENDANTS_DEPTH_LIMIT
  48. { statuses: statuses }
  49. else
  50. next_status = statuses.pop
  51. { statuses: statuses, next_status: next_status }
  52. end
  53. end
  54. def set_account
  55. @account = Account.find_local!(params[:account_username])
  56. end
  57. def set_ancestors
  58. @ancestors = @status.reply? ? cache_collection(@status.ancestors(ANCESTORS_LIMIT, current_account), Status) : []
  59. @next_ancestor = @ancestors.size < ANCESTORS_LIMIT ? nil : @ancestors.shift
  60. end
  61. def set_descendants
  62. @max_descendant_thread_id = params[:max_descendant_thread_id]&.to_i
  63. @since_descendant_thread_id = params[:since_descendant_thread_id]&.to_i
  64. descendants = cache_collection(
  65. @status.descendants(
  66. DESCENDANTS_LIMIT,
  67. current_account,
  68. @max_descendant_thread_id,
  69. @since_descendant_thread_id,
  70. DESCENDANTS_DEPTH_LIMIT
  71. ),
  72. Status
  73. )
  74. @descendant_threads = []
  75. if descendants.present?
  76. statuses = [descendants.first]
  77. depth = 1
  78. descendants.drop(1).each_with_index do |descendant, index|
  79. if descendants[index].id == descendant.in_reply_to_id
  80. depth += 1
  81. statuses << descendant
  82. else
  83. @descendant_threads << create_descendant_thread(depth, statuses)
  84. @descendant_threads.reverse_each do |descendant_thread|
  85. statuses = descendant_thread[:statuses]
  86. index = statuses.find_index do |thread_status|
  87. thread_status.id == descendant.in_reply_to_id
  88. end
  89. if index.present?
  90. depth += index - statuses.size
  91. break
  92. end
  93. depth -= statuses.size
  94. end
  95. statuses = [descendant]
  96. end
  97. end
  98. @descendant_threads << create_descendant_thread(depth, statuses)
  99. end
  100. @max_descendant_thread_id = @descendant_threads.pop[:statuses].first.id if descendants.size >= DESCENDANTS_LIMIT
  101. end
  102. def set_link_headers
  103. response.headers['Link'] = LinkHeader.new(
  104. [
  105. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  106. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  107. ]
  108. )
  109. end
  110. def set_status
  111. @status = @account.statuses.find(params[:id])
  112. @stream_entry = @status.stream_entry
  113. @type = @stream_entry.activity_type.downcase
  114. authorize @status, :show?
  115. rescue Mastodon::NotPermittedError
  116. # Reraise in order to get a 404
  117. raise ActiveRecord::RecordNotFound
  118. end
  119. def set_instance_presenter
  120. @instance_presenter = InstancePresenter.new
  121. end
  122. def check_account_suspension
  123. gone if @account.suspended?
  124. end
  125. def redirect_to_original
  126. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  127. end
  128. def set_referrer_policy_header
  129. return if @status.public_visibility? || @status.unlisted_visibility?
  130. response.headers['Referrer-Policy'] = 'origin'
  131. end
  132. end