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.
 
 
 
 

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