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.

160 lines
4.5 KiB

  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. before_action :set_cache_headers
  8. before_action :set_body_classes
  9. skip_around_action :set_locale, if: -> { [:json, :rss].include?(request.format&.to_sym) }
  10. skip_before_action :require_functional!, unless: :whitelist_mode?
  11. def show
  12. respond_to do |format|
  13. format.html do
  14. expires_in 0, public: true unless user_signed_in?
  15. @pinned_statuses = []
  16. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  17. @featured_hashtags = @account.featured_tags.order(statuses_count: :desc)
  18. if current_account && @account.blocking?(current_account)
  19. @statuses = []
  20. return
  21. end
  22. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  23. @statuses = filtered_status_page
  24. @statuses = cache_collection(@statuses, Status)
  25. @rss_url = rss_url
  26. unless @statuses.empty?
  27. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  28. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  29. end
  30. end
  31. format.rss do
  32. expires_in 1.minute, public: true
  33. limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  34. @statuses = filtered_statuses.without_reblogs.limit(limit)
  35. @statuses = cache_collection(@statuses, Status)
  36. render xml: RSS::AccountSerializer.render(@account, @statuses, params[:tag])
  37. end
  38. format.json do
  39. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  40. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter, fields: restrict_fields_to
  41. end
  42. end
  43. end
  44. private
  45. def set_body_classes
  46. @body_classes = 'with-modals'
  47. end
  48. def show_pinned_statuses?
  49. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  50. end
  51. def filtered_statuses
  52. default_statuses.tap do |statuses|
  53. statuses.merge!(hashtag_scope) if tag_requested?
  54. statuses.merge!(only_media_scope) if media_requested?
  55. statuses.merge!(no_replies_scope) unless replies_requested?
  56. end
  57. end
  58. def default_statuses
  59. @account.statuses.where(visibility: [:public, :unlisted])
  60. end
  61. def only_media_scope
  62. Status.where(id: account_media_status_ids)
  63. end
  64. def account_media_status_ids
  65. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  66. end
  67. def no_replies_scope
  68. Status.without_replies
  69. end
  70. def hashtag_scope
  71. tag = Tag.find_normalized(params[:tag])
  72. if tag
  73. Status.tagged_with(tag.id)
  74. else
  75. Status.none
  76. end
  77. end
  78. def username_param
  79. params[:username]
  80. end
  81. def rss_url
  82. if tag_requested?
  83. short_account_tag_url(@account, params[:tag], format: 'rss')
  84. else
  85. short_account_url(@account, format: 'rss')
  86. end
  87. end
  88. def older_url
  89. pagination_url(max_id: @statuses.last.id)
  90. end
  91. def newer_url
  92. pagination_url(min_id: @statuses.first.id)
  93. end
  94. def pagination_url(max_id: nil, min_id: nil)
  95. if tag_requested?
  96. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  97. elsif media_requested?
  98. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  99. elsif replies_requested?
  100. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  101. else
  102. short_account_url(@account, max_id: max_id, min_id: min_id)
  103. end
  104. end
  105. def media_requested?
  106. request.path.split('.').first.ends_with?('/media') && !tag_requested?
  107. end
  108. def replies_requested?
  109. request.path.split('.').first.ends_with?('/with_replies') && !tag_requested?
  110. end
  111. def tag_requested?
  112. request.path.split('.').first.ends_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  113. end
  114. def filtered_status_page
  115. filtered_statuses.paginate_by_id(PAGE_SIZE, params_slice(:max_id, :min_id, :since_id))
  116. end
  117. def params_slice(*keys)
  118. params.slice(*keys).permit(*keys)
  119. end
  120. def restrict_fields_to
  121. if signed_request_account.present? || public_fetch_mode?
  122. # Return all fields
  123. else
  124. %i(id type preferred_username inbox public_key endpoints)
  125. end
  126. end
  127. end