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.

158 lines
4.4 KiB

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