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.

84 lines
2.3 KiB

  1. # frozen_string_literal: true
  2. class StatusesController < ApplicationController
  3. include Authorization
  4. layout 'public'
  5. before_action :set_account
  6. before_action :set_status
  7. before_action :set_link_headers
  8. before_action :check_account_suspension
  9. before_action :redirect_to_original, only: [:show]
  10. before_action { response.headers['Vary'] = 'Accept' }
  11. def show
  12. respond_to do |format|
  13. format.html do
  14. @ancestors = @status.reply? ? cache_collection(@status.ancestors(current_account), Status) : []
  15. @descendants = cache_collection(@status.descendants(current_account), Status)
  16. render 'stream_entries/show'
  17. end
  18. format.json do
  19. render json: @status,
  20. serializer: ActivityPub::NoteSerializer,
  21. adapter: ActivityPub::Adapter,
  22. content_type: 'application/activity+json'
  23. # Allow HTTP caching for 3 minutes if the status is public
  24. unless @stream_entry.hidden?
  25. request.session_options[:skip] = true
  26. expires_in(3.minutes, public: true)
  27. end
  28. end
  29. end
  30. end
  31. def activity
  32. render json: @status,
  33. serializer: ActivityPub::ActivitySerializer,
  34. adapter: ActivityPub::Adapter,
  35. content_type: 'application/activity+json'
  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 set_account
  43. @account = Account.find_local!(params[:account_username])
  44. end
  45. def set_link_headers
  46. response.headers['Link'] = LinkHeader.new(
  47. [
  48. [account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]],
  49. [ActivityPub::TagManager.instance.uri_for(@status), [%w(rel alternate), %w(type application/activity+json)]],
  50. ]
  51. )
  52. end
  53. def set_status
  54. @status = @account.statuses.find(params[:id])
  55. @stream_entry = @status.stream_entry
  56. @type = @stream_entry.activity_type.downcase
  57. authorize @status, :show?
  58. rescue Mastodon::NotPermittedError
  59. # Reraise in order to get a 404
  60. raise ActiveRecord::RecordNotFound
  61. end
  62. def check_account_suspension
  63. gone if @account.suspended?
  64. end
  65. def redirect_to_original
  66. redirect_to ::TagManager.instance.url_for(@status.reblog) if @status.reblog?
  67. end
  68. end