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.

168 lines
4.6 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 :require_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  8. before_action :set_cache_headers
  9. before_action :set_body_classes
  10. skip_around_action :set_locale, if: -> { [:json, :rss].include?(request.format&.to_sym) }
  11. skip_before_action :require_functional!, unless: :whitelist_mode?
  12. def show
  13. respond_to do |format|
  14. format.html do
  15. expires_in 0, public: true unless user_signed_in?
  16. @pinned_statuses = []
  17. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  18. @featured_hashtags = @account.featured_tags.order(statuses_count: :desc)
  19. if current_account && @account.blocking?(current_account)
  20. @statuses = []
  21. return
  22. end
  23. @pinned_statuses = cached_filtered_status_pins if show_pinned_statuses?
  24. @statuses = cached_filtered_status_page
  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
  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_pinned_statuses
  52. @account.pinned_statuses.where(visibility: [:public, :unlisted])
  53. end
  54. def filtered_statuses
  55. default_statuses.tap do |statuses|
  56. statuses.merge!(hashtag_scope) if tag_requested?
  57. statuses.merge!(only_media_scope) if media_requested?
  58. statuses.merge!(no_replies_scope) unless replies_requested?
  59. end
  60. end
  61. def default_statuses
  62. @account.statuses.where(visibility: [:public, :unlisted])
  63. end
  64. def only_media_scope
  65. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  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 skip_temporary_suspension_response?
  82. request.format == :json
  83. end
  84. def rss_url
  85. if tag_requested?
  86. short_account_tag_url(@account, params[:tag], format: 'rss')
  87. else
  88. short_account_url(@account, format: 'rss')
  89. end
  90. end
  91. def older_url
  92. pagination_url(max_id: @statuses.last.id)
  93. end
  94. def newer_url
  95. pagination_url(min_id: @statuses.first.id)
  96. end
  97. def pagination_url(max_id: nil, min_id: nil)
  98. if tag_requested?
  99. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  100. elsif media_requested?
  101. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  102. elsif replies_requested?
  103. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  104. else
  105. short_account_url(@account, max_id: max_id, min_id: min_id)
  106. end
  107. end
  108. def media_requested?
  109. request.path.split('.').first.end_with?('/media') && !tag_requested?
  110. end
  111. def replies_requested?
  112. request.path.split('.').first.end_with?('/with_replies') && !tag_requested?
  113. end
  114. def tag_requested?
  115. request.path.split('.').first.end_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  116. end
  117. def cached_filtered_status_pins
  118. cache_collection(
  119. filtered_pinned_statuses,
  120. Status
  121. )
  122. end
  123. def cached_filtered_status_page
  124. cache_collection_paginated_by_id(
  125. filtered_statuses,
  126. Status,
  127. PAGE_SIZE,
  128. params_slice(:max_id, :min_id, :since_id)
  129. )
  130. end
  131. def params_slice(*keys)
  132. params.slice(*keys).permit(*keys)
  133. end
  134. end