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.

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