The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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