accounts_controller.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_account_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. end
  37. format.json do
  38. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  39. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
  40. end
  41. end
  42. end
  43. private
  44. def set_body_classes
  45. @body_classes = 'with-modals'
  46. end
  47. def show_pinned_statuses?
  48. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  49. end
  50. def filtered_pinned_statuses
  51. @account.pinned_statuses.where(visibility: [:public, :unlisted])
  52. end
  53. def filtered_statuses
  54. default_statuses.tap do |statuses|
  55. statuses.merge!(hashtag_scope) if tag_requested?
  56. statuses.merge!(only_media_scope) if media_requested?
  57. statuses.merge!(no_replies_scope) unless replies_requested?
  58. end
  59. end
  60. def default_statuses
  61. @account.statuses.where(visibility: [:public, :unlisted])
  62. end
  63. def only_media_scope
  64. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  65. end
  66. def no_replies_scope
  67. Status.without_replies
  68. end
  69. def hashtag_scope
  70. tag = Tag.find_normalized(params[:tag])
  71. if tag
  72. Status.tagged_with(tag.id)
  73. else
  74. Status.none
  75. end
  76. end
  77. def username_param
  78. params[:username]
  79. end
  80. def skip_temporary_suspension_response?
  81. request.format == :json
  82. end
  83. def rss_url
  84. if tag_requested?
  85. short_account_tag_url(@account, params[:tag], format: 'rss')
  86. else
  87. short_account_url(@account, format: 'rss')
  88. end
  89. end
  90. def older_url
  91. pagination_url(max_id: @statuses.last.id)
  92. end
  93. def newer_url
  94. pagination_url(min_id: @statuses.first.id)
  95. end
  96. def pagination_url(max_id: nil, min_id: nil)
  97. if tag_requested?
  98. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  99. elsif media_requested?
  100. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  101. elsif replies_requested?
  102. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  103. else
  104. short_account_url(@account, max_id: max_id, min_id: min_id)
  105. end
  106. end
  107. def media_requested?
  108. request.path.split('.').first.end_with?('/media') && !tag_requested?
  109. end
  110. def replies_requested?
  111. request.path.split('.').first.end_with?('/with_replies') && !tag_requested?
  112. end
  113. def tag_requested?
  114. request.path.split('.').first.end_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  115. end
  116. def cached_filtered_status_pins
  117. cache_collection(
  118. filtered_pinned_statuses,
  119. Status
  120. )
  121. end
  122. def cached_filtered_status_page
  123. cache_collection_paginated_by_id(
  124. filtered_statuses,
  125. Status,
  126. PAGE_SIZE,
  127. params_slice(:max_id, :min_id, :since_id)
  128. )
  129. end
  130. def params_slice(*keys)
  131. params.slice(*keys).permit(*keys)
  132. end
  133. end