accounts_controller.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. class AccountsController < ApplicationController
  3. PAGE_SIZE = 20
  4. PAGE_SIZE_MAX = 200
  5. include AccountControllerConcern
  6. include SignatureAuthentication
  7. vary_by -> { public_fetch_mode? ? 'Accept, Accept-Language, Cookie' : 'Accept, Accept-Language, Cookie, Signature' }
  8. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  9. skip_around_action :set_locale, if: -> { [:json, :rss].include?(request.format&.to_sym) }
  10. skip_before_action :require_functional!, unless: :limited_federation_mode?
  11. def show
  12. respond_to do |format|
  13. format.html do
  14. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  15. end
  16. format.rss do
  17. expires_in 1.minute, public: true
  18. limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  19. @statuses = filtered_statuses.without_reblogs.limit(limit)
  20. @statuses = preload_collection(@statuses, Status)
  21. end
  22. format.json do
  23. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  24. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
  25. end
  26. end
  27. end
  28. private
  29. def filtered_statuses
  30. default_statuses.tap do |statuses|
  31. statuses.merge!(hashtag_scope) if tag_requested?
  32. statuses.merge!(only_media_scope) if media_requested?
  33. statuses.merge!(no_replies_scope) unless replies_requested?
  34. end
  35. end
  36. def default_statuses
  37. @account.statuses.distributable_visibility
  38. end
  39. def only_media_scope
  40. Status.joins(:media_attachments).merge(@account.media_attachments).group(:id)
  41. end
  42. def no_replies_scope
  43. Status.without_replies
  44. end
  45. def hashtag_scope
  46. tag = Tag.find_normalized(params[:tag])
  47. if tag
  48. Status.tagged_with(tag.id)
  49. else
  50. Status.none
  51. end
  52. end
  53. def username_param
  54. params[:username]
  55. end
  56. def skip_temporary_suspension_response?
  57. request.format == :json
  58. end
  59. def rss_url
  60. if tag_requested?
  61. short_account_tag_url(@account, params[:tag], format: 'rss')
  62. else
  63. short_account_url(@account, format: 'rss')
  64. end
  65. end
  66. helper_method :rss_url
  67. def media_requested?
  68. path_without_format.end_with?('/media') && !tag_requested?
  69. end
  70. def replies_requested?
  71. path_without_format.end_with?('/with_replies') && !tag_requested?
  72. end
  73. def tag_requested?
  74. path_without_format.end_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  75. end
  76. def path_without_format
  77. request.path.split('.').first
  78. end
  79. end